Jump to content

Basic Commands: Difference between revisions

From Wiki
 
(5 intermediate revisions by the same user not shown)
Line 2: Line 2:
<code>ls</code> is one of the most frequently used basic command in [[Linux]]. The ls command-line utility is used to list files and directories that exist inside a directory.
<code>ls</code> is one of the most frequently used basic command in [[Linux]]. The ls command-line utility is used to list files and directories that exist inside a directory.


By default, when used without any options, ls lists only the contents of the current directory in alphabetical order without any additional information.<syntaxhighlight lang="shell">
By default, when used without any options, ls lists only the contents of the current directory in alphabetical order without any additional information.
 
<syntaxhighlight lang="shell">
ls
ls
</syntaxhighlight>Type ls <code>/home/bob/Project</code> to see the contents of the directory Project.
</syntaxhighlight>
 
Type ls <code>/home/bob/Project</code> to see the contents of the directory Project.


You can use options with the ls command for variation in results:
You can use options with the ls command for variation in results:
Line 12: Line 16:
*<code>ls -ltr</code> list file names in the last modification time in reverse order.
*<code>ls -ltr</code> list file names in the last modification time in reverse order.


The output of <code>ls -al</code><syntaxhighlight lang="shell">
The output of <code>ls -al</code>
 
<syntaxhighlight lang="shell">
-rw-r--r-- 1 bob developers 281 Apr 1 2020 /source/project/learn.py
-rw-r--r-- 1 bob developers 281 Apr 1 2020 /source/project/learn.py
</syntaxhighlight>The owner (ie bob) has read and write permissions, other members of the group have read permissions, and the rest of the world has read permissions on the file. The file is owned by the user named bob and belongs to the developers group. The total size of the file is 281 bytes.
</syntaxhighlight>
 
The owner (ie bob) has read and write permissions, other members of the group have read permissions, and the rest of the world has read permissions on the file. The file is owned by the user named bob and belongs to the developers group. The total size of the file is 281 bytes.


==''cd'' Command==
==''cd'' Command==
<code>cd</code> stands for change directory, which is used to change the current working directory. The cd command is used to navigate through [[Linux]] files and directories. You need to specify either the full path of the target directory or the directory name you want to change to.
<code>cd</code> stands for change directory, which is used to change the current working directory. The cd command is used to navigate through [[Linux]] files and directories. You need to specify either the full path of the target directory or the directory name you want to change to.


If you are in the <code>/home/bob/Documents</code> directory and need to go to the Photos subdirectory, then type:<syntaxhighlight lang="shell">
If you are in the <code>/home/bob/Documents</code> directory and need to go to the Photos subdirectory, then type:
 
<syntaxhighlight lang="shell">
cd Photos
cd Photos
</syntaxhighlight>Use the absolute path to change directory, change to <code>/home/bob/Pictures</code> directory:<syntaxhighlight lang="shell">
</syntaxhighlight>
 
Use the absolute path to change directory, change to <code>/home/bob/Pictures</code> directory:
 
<syntaxhighlight lang="shell">
cd /home/bob/Pictures
cd /home/bob/Pictures
</syntaxhighlight>Some shortcuts that you can use for easy navigation:
</syntaxhighlight>
 
Some shortcuts that you can use for easy navigation:


*<code>cd ..</code> moves one directory up.
*<code>cd ..</code> moves one directory up.
Line 32: Line 48:
The <code>mkdir</code> stands for make directory. The <code>mkdir</code> command is used to create a new directory in [[Linux]].
The <code>mkdir</code> stands for make directory. The <code>mkdir</code> command is used to create a new directory in [[Linux]].


When you type mkdir Pop, it will create a directory named Pop in the current directory. Whereas, the following Linux command will create a directory named Source inside the Documents directory:<syntaxhighlight lang="shell">
When you type mkdir Pop, it will create a directory named Pop in the current directory. Whereas, the following Linux command will create a directory named Source inside the Documents directory:
 
<syntaxhighlight lang="shell">
mkdir Documents/Source
mkdir Documents/Source
</syntaxhighlight>Use <code>-p</code> option to create the entire directory structure. For example to create a directory <code>2020</code> and its sub-directory Source in the existing <code>Documents</code> directory, type:<syntaxhighlight lang="shell">
</syntaxhighlight>
 
Use <code>-p</code> option to create the entire directory structure. For example to create a directory <code>2020</code> and its sub-directory Source in the existing <code>Documents</code> directory, type:
 
<syntaxhighlight lang="shell">
mkdir -p Documents/2020/Source
mkdir -p Documents/2020/Source
</syntaxhighlight>
</syntaxhighlight>
Line 41: Line 63:
The <code>cat</code> stands for concatenate. It is one of the basic command in the [[Linux]] operating system. The cat command is used to concatenate files, view the content of a file, create a new file, or redirect output in files.
The <code>cat</code> stands for concatenate. It is one of the basic command in the [[Linux]] operating system. The cat command is used to concatenate files, view the content of a file, create a new file, or redirect output in files.


Type cat command followed by a file name to see the contents of the file.<syntaxhighlight lang="shell">
Type cat command followed by a file name to see the contents of the file.
 
<syntaxhighlight lang="shell">
cat users.txt
cat users.txt
</syntaxhighlight>The cat command can also be used to perform some advanced operations, such as:
</syntaxhighlight>
 
The cat command can also be used to perform some advanced operations, such as:


To create a new file named <code>file1.txt</code>, type:<syntaxhighlight lang="shell">
To create a new file named <code>file1.txt</code>, type:
 
<syntaxhighlight lang="shell">
cat > file1.txt
cat > file1.txt
</syntaxhighlight>The existing contents of the <code>department.txt</code> file will be overwritten by the contents of <code>users.txt</code>:<syntaxhighlight lang="shell">
</syntaxhighlight>
 
The existing contents of the <code>department.txt</code> file will be overwritten by the contents of <code>users.txt</code>:
 
<syntaxhighlight lang="shell">
cat users.txt > department.txt
cat users.txt > department.txt
</syntaxhighlight>To convert the content of file <code>users.txt</code> from lowercase to uppercase and store it in <code>output.txt</code>, use the following command:<syntaxhighlight lang="shell">
</syntaxhighlight>
 
To convert the content of file <code>users.txt</code> from lowercase to uppercase and store it in <code>output.txt</code>, use the following command:
 
<syntaxhighlight lang="shell">
cat users.txt | tr a-z A-Z > output.txt
cat users.txt | tr a-z A-Z > output.txt
</syntaxhighlight>
</syntaxhighlight>
Line 64: Line 100:
*<code>-g</code> displays output in gibibyte (GiB).
*<code>-g</code> displays output in gibibyte (GiB).


Check disk usage information of a particular file system, type:<syntaxhighlight lang="shell">
Check disk usage information of a particular file system, type:
 
<syntaxhighlight lang="shell">
df /dev/sda5
df /dev/sda5
</syntaxhighlight>'''Output'''<syntaxhighlight lang="shell">
</syntaxhighlight>
 
'''Output'''
 
<syntaxhighlight lang="shell">
Filesystem    1K-blocks    Used Available Use% Mounted on
Filesystem    1K-blocks    Used Available Use% Mounted on
/dev/sda5      124595628 40714508  77505800  35% /
/dev/sda5      124595628 40714508  77505800  35% /
</syntaxhighlight>


</syntaxhighlight>Here, <code>/dev/sda5</code> has a total size of 40503552, the used size is 9132528, and the available size is 29283856. The file system has used 24% of the total allocated space and it is mounted on <code>/</code>.
Here, <code>/dev/sda5</code> has a total size of 40503552, the used size is 9132528, and the available size is 29283856. The file system has used 24% of the total allocated space and it is mounted on <code>/</code>.


==''du'' Command==
==''du'' Command==
Use the <code>du</code> ''(stands for disk usage)'' command to gain disk usage information of a particular file or directory on your [[Linux]] [[Komputer|computer]]. By default, the du command displays disk usage information in the number of blocks used by a file. Use the <code>-h</code> option with this command to display output in a human-readable format.
Use the <code>du</code> ''(stands for disk usage)'' command to gain disk usage information of a particular file or directory on your [[Linux]] [[Komputer|computer]]. By default, the du command displays disk usage information in the number of blocks used by a file. Use the <code>-h</code> option with this command to display output in a human-readable format.


Specify the file name or directory name to display its disk usage.<syntaxhighlight lang="shell" line="1">
Specify the file name or directory name to display its disk usage.
 
<syntaxhighlight lang="shell" line="1">
du -h /var/log/apache/access.log
du -h /var/log/apache/access.log
du -h /var/log
du -h /var/log
Line 83: Line 128:
The <code>cp</code> command is used to copy files and directories from the source directory to another location. By default, it copies only the given file or directory, but you can use the <code>-r</code> option to copy a directory along with its subdirectories.
The <code>cp</code> command is used to copy files and directories from the source directory to another location. By default, it copies only the given file or directory, but you can use the <code>-r</code> option to copy a directory along with its subdirectories.


The following command copy the file <code>users.txt</code> from the current working directory to the <code>Documents/records</code> directory.<syntaxhighlight lang="shell">
The following command copy the file <code>users.txt</code> from the current working directory to the <code>Documents/records</code> directory.
 
<syntaxhighlight lang="shell">
cp users.txt Documents/records/
cp users.txt Documents/records/
</syntaxhighlight>To create a copy of the file <code>file2.txt</code> with the name <code>file2_backup.txt</code> in your current working directory.<syntaxhighlight lang="shell">
</syntaxhighlight>
 
To create a copy of the file <code>file2.txt</code> with the name <code>file2_backup.txt</code> in your current working directory.
 
<syntaxhighlight lang="shell">
cp file2.txt file2_backup.txt
cp file2.txt file2_backup.txt
</syntaxhighlight>
</syntaxhighlight>
Line 92: Line 143:
Use the <code>mv</code> ''(stands for move)'' command to move files or directories from the source to the destination directory. It can be used to rename a file/directory.
Use the <code>mv</code> ''(stands for move)'' command to move files or directories from the source to the destination directory. It can be used to rename a file/directory.


The following <code>mv</code> command moves the <code>users.txt</code> file to the <code>Documents/records</code> directory.<syntaxhighlight lang="shell">
The following <code>mv</code> command moves the <code>users.txt</code> file to the <code>Documents/records</code> directory.
 
<syntaxhighlight lang="shell">
mv users.txt Documents/records
mv users.txt Documents/records
</syntaxhighlight>To rename the employees.txt file to <code>users.txt</code>:<syntaxhighlight lang="shell">
</syntaxhighlight>
 
To rename the employees.txt file to <code>users.txt</code>:
 
<syntaxhighlight lang="shell">
mv employees.txt users.txt
mv employees.txt users.txt
</syntaxhighlight>
</syntaxhighlight>


==rm Command==
==''rm'' Command==
Use the <code>rm</code> (stands for remove) command to remove the given file, multiple files, or a group/type of files. By default, the <code>rm</code> command does not require user confirmation to delete a file, but you can enable user confirmation using the <code>-i</code> option.
Use the <code>rm</code> (stands for remove) command to remove the given file, multiple files, or a group/type of files. By default, the <code>rm</code> command does not require user confirmation to delete a file, but you can enable user confirmation using the <code>-i</code> option.


Line 109: Line 166:


Removes the file named documents.txt from the current directory:
Removes the file named documents.txt from the current directory:
rm documents.txt
 
<syntaxhighlight lang="shell">
rm documents.txt
</syntaxhighlight>


To remove directories and their contents recursively, type:
To remove directories and their contents recursively, type:
rm -r Documents
 
<syntaxhighlight lang="shell">
rm -r Documents
</syntaxhighlight>


To remove the directory without being prompted, type:
To remove the directory without being prompted, type:
rm -rf dir1
 
<syntaxhighlight lang="shell">
rm -rf dir1
</syntaxhighlight>


The <code>rm-rf</code> command should be used with caution.
The <code>rm-rf</code> command should be used with caution.


==Man & help Command==
==''man'' & ''help'' Command==
Use the <code>man</code> command to display the user manual of the [[Linux]] command. Almost all the Linux commands have man pages, which is a kind of documentation that is displayed on the terminal. The Linux commands manual page explains what a particular command does, syntax, and accepted arguments.
Use the <code>man</code> command to display the user manual of the [[Linux]] command. Almost all the Linux commands have man pages, which is a kind of documentation that is displayed on the terminal. The Linux commands manual page explains what a particular command does, syntax, and accepted arguments.


Type man followed by the command name to display the command user manual page.
Type man followed by the command name to display the command user manual page.
man mkdir
 
<syntaxhighlight lang="shell">
man mkdir
</syntaxhighlight>


Similarly, you can also use the –-help option to display the help pages of a particular command.
Similarly, you can also use the –-help option to display the help pages of a particular command.
mkdir --help


==chmod Command==
<syntaxhighlight lang="shell">
mkdir --help
</syntaxhighlight>
 
==''chmod'' Command==
The <code>chmod</code> command is used to change the mode (permission) of a file. The basic permissions a file can have are <code>r</code>(read), <code>w</code>(write), and <code>x</code>(execute).
The <code>chmod</code> command is used to change the mode (permission) of a file. The basic permissions a file can have are <code>r</code>(read), <code>w</code>(write), and <code>x</code>(execute).


Line 134: Line 206:


For example to give <code>file1.txt</code> owner and group read and write permissions and only read permission to all others, type:
For example to give <code>file1.txt</code> owner and group read and write permissions and only read permission to all others, type:
chmod 664 file1.txt
 
<syntaxhighlight lang="shell">
chmod 664 file1.txt
</syntaxhighlight>


You may use -R option to recursively set permissions for all files and directories in a given directory.
You may use -R option to recursively set permissions for all files and directories in a given directory.


To make the file executable, type:
To make the file executable, type:
chmod +x myscript.sh


==chown Command==
<syntaxhighlight lang="shell">
chmod +x myscript.sh
</syntaxhighlight>
 
==''chown'' Command==
The <code>chown</code> command is used to change file and directory ownership in Linux.
The <code>chown</code> command is used to change file and directory ownership in Linux.


To change both owner and group of file, type:
To change both owner and group of file, type:
chown ownername:groupname filename
 
<syntaxhighlight lang="shell">
chown ownername:groupname filename
</syntaxhighlight>


You may use <code>-R</code> option to recursively set ownership for all files and directories in a given directory.
You may use <code>-R</code> option to recursively set ownership for all files and directories in a given directory.


==pwd Command==
==''pwd'' Command==
<code>pwd</code> stands for present working directory. The pwd command prints the absolute path of the current directory on your Linux terminal. The present working directory is the directory in which you are currently working.
<code>pwd</code> stands for present working directory. The pwd command prints the absolute path of the current directory on your Linux terminal. The present working directory is the directory in which you are currently working.
pwd
 
<syntaxhighlight lang="shell">
pwd
</syntaxhighlight>


'''Output'''
'''Output'''
/home/linuxopsys
 
<syntaxhighlight lang="shell">
/home/linuxops
</syntaxhighlight>


In the example <code>/home/kagtain</code> is the current directory.
In the example <code>/home/kagtain</code> is the current directory.


==uname Command==
==''uname'' Command==
Use the <code>uname</code> (stands for UNIX name) command to display fundamental information about your [[Linux]] computer. By default, this command prints only the type of the operating system, such as Linux. You can use different options with this command to print other information about your computer such as OS, kernel version, machine name, network, and so on.
Use the <code>uname</code> (stands for UNIX name) command to display fundamental information about your [[Linux]] computer. By default, this command prints only the type of the operating system, such as Linux. You can use different options with this command to print other information about your computer such as OS, kernel version, machine name, network, and so on.


Line 167: Line 254:


The output of uname -a command on my machine:
The output of uname -a command on my machine:
Linux linux 5.13.0-37-generic #42~20.04.1-Ubuntu SMP Tue Mar 15 15:44:28 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
 
<syntaxhighlight lang="shell">
Linux linux 5.13.0-37-generic #42~20.04.1-Ubuntu SMP Tue Mar 15 15:44:28 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
</syntaxhighlight>


All system information including OS type, name, user name, kernel release, and hardware platform is displayed.
All system information including OS type, name, user name, kernel release, and hardware platform is displayed.


==who Command==
==''who'' Command==
Use the <code>who</code> command to list currently logged-in users on your Linux computer. This command also supports few options to display specific information, such as:
Use the <code>who</code> command to list currently logged-in users on your Linux computer. This command also supports few options to display specific information, such as:


Line 179: Line 269:


The output of who command looks like this:
The output of who command looks like this:
root    pts/0        2022-04-28 01:30 (192.168.1.10)
 
bob      pts/1        2022-04-28 02:40 (192.168.1.22)
<syntaxhighlight lang="shell">
kangtain pts/2        2022-04-28 02:05 (192.168.1.22)
root    pts/0        2022-04-28 01:30 (192.168.1.10)
bob      pts/1        2022-04-28 02:40 (192.168.1.22)
kangtain pts/2        2022-04-28 02:05 (192.168.1.22)
</syntaxhighlight>


It shows 3 users with their logged in date and time.
It shows 3 users with their logged in date and time.


==uptime Command==
==''uptime'' Command==
Use the uptime command to display the current system time, the duration for which the system has been up, the number of logged-in users, and average load time for 1, 5, and 15-minute intervals. This Linux command retrieves information from the <code>/proc/uptime</code> file.
Use the uptime command to display the current system time, the duration for which the system has been up, the number of logged-in users, and average load time for 1, 5, and 15-minute intervals. This Linux command retrieves information from the <code>/proc/uptime</code> file.


The output of uptime command from my system:
The output of uptime command from my system:
17:11:28 up 6 days,  3:23,  1 user,  load average: 0.00, 0.01, 0.00


==hostname Command==
<syntaxhighlight lang="shell">
17:11:28 up 6 days,  3:23,  1 user,  load average: 0.00, 0.01, 0.00
</syntaxhighlight>
 
==''hostname'' Command==
Use <code>hostname</code> command to display the Domain Name System (DNS) name and set the hostname or Network Information System (NIS) of your system.
Use <code>hostname</code> command to display the Domain Name System (DNS) name and set the hostname or Network Information System (NIS) of your system.


Display host name and domain name, type:
Display host name and domain name, type:
hostname
 
kangtain
<syntaxhighlight lang="shell">
hostname
kangtain
</syntaxhighlight>


Some distributions allow you to set hostname for your machine, type:
Some distributions allow you to set hostname for your machine, type:
sudo hostname NEW_HOSTNAME
 
<syntaxhighlight lang="shell">
sudo hostname NEW_HOSTNAME
</syntaxhighlight>


You can replace NEW_HOSTNAME with your actual machine name.
You can replace NEW_HOSTNAME with your actual machine name.


==top, htop Command==
==''top'', ''htop'' Command==
Use the <code>top</code> or <code>htop</code> commands to monitor the server’s processes or vital system resources in real-time. Both these commands are used for the same purpose, but their output is different. The default output displays information about top CPU-consuming processes along with RAM usage.
Use the <code>top</code> or <code>htop</code> commands to monitor the server’s processes or vital system resources in real-time. Both these commands are used for the same purpose, but their output is different. The default output displays information about top CPU-consuming processes along with RAM usage.
top
 
<syntaxhighlight lang="shell">
top
</syntaxhighlight>


The command also displays system up time, load average, number of logged in users, memory, total tasks, and running tasks.
The command also displays system up time, load average, number of logged in users, memory, total tasks, and running tasks.
Line 211: Line 316:
<code>Htop</code> is an improved version of the <code>top</code> command. It display colorful text and allows for output scrolling. Htop doesnt comes preinstalled on most Linux distros.
<code>Htop</code> is an improved version of the <code>top</code> command. It display colorful text and allows for output scrolling. Htop doesnt comes preinstalled on most Linux distros.


==touch Command==
==''touch'' Command==
Use the <code>touch</code> command to create an empty file on your Linux computer, or to modify the timestamp of a file. The primary function of this command is to update file timestamps, but it is most commonly used to create a file. The touch command can update access and modification timestamps of the specified file, and create the file if it does not already exist.
Use the <code>touch</code> command to create an empty file on your Linux computer, or to modify the timestamp of a file. The primary function of this command is to update file timestamps, but it is most commonly used to create a file. The touch command can update access and modification timestamps of the specified file, and create the file if it does not already exist.


To create a new empty file named install.doc, type:
To create a new empty file named install.doc, type:
touch install.doc
 
<syntaxhighlight lang="bash">
touch install.doc
</syntaxhighlight>


In this example, the modification time of the file updates.txt is updated to the current time.
In this example, the modification time of the file updates.txt is updated to the current time.
touch -m updates.txt


==zip and unzip Command==
<syntaxhighlight lang="bash">
Use the <code>zip</code> command-line utility to create an archive of files, and the unzip command for uncompressing a zip archive. The <code>zip</code> command can also be used to modify a zip archive.
touch -m updates.txt
</syntaxhighlight>
 
==''zip'' and ''unzip'' Command==
Use the <code>zip</code> command-line utility to create an archive of files, and the <code>unzip</code> command for uncompressing a <code>zip</code> archive. The <code>zip</code> command can also be used to modify a zip archive.


To create a zip archive named <code>users.zip</code> from the specified files:
To create a zip archive named <code>users.zip</code> from the specified files:
zip users.zip output.txt install.doc newfiles.txt updates.txt
 
<syntaxhighlight lang="bash">
zip users.zip output.txt install.doc newfiles.txt updates.txt
</syntaxhighlight>


To extract the contents of the users.zip archive to the current directory:  
To extract the contents of the users.zip archive to the current directory:  
unzip users.zip


==tar Command==
<syntaxhighlight lang="bash">
unzip users.zip
</syntaxhighlight>
 
==''tar'' Command==
Use the <code>tar</code> command to create a compressed or uncompressed archive of specified files. You can also use this command to modify, maintain, or extract tar archives. Tar archives can combine multiple files or directories into a single file.
Use the <code>tar</code> command to create a compressed or uncompressed archive of specified files. You can also use this command to modify, maintain, or extract tar archives. Tar archives can combine multiple files or directories into a single file.


Line 239: Line 356:


To create an uncompressed archive file named users.tar of the specified files.
To create an uncompressed archive file named users.tar of the specified files.
tar cf users.tar updates.txt newfiles.txt


==ln Command==
<syntaxhighlight lang="bash">
tar cf users.tar updates.txt newfiles.txt
</syntaxhighlight>
 
==''ln'' Command==
Use the <code>ln</code> command for creating hard and soft links to existing files or directories. By default, this command creates hard links. Specify the -s option to create symbolic or soft links.
Use the <code>ln</code> command for creating hard and soft links to existing files or directories. By default, this command creates hard links. Specify the -s option to create symbolic or soft links.


To create a symbolic link from the updates.txt file to the newitems.txt file:
To create a symbolic link from the updates.txt file to the newitems.txt file:
ln -s updates.txt newitems.txt
 
<syntaxhighlight lang="bash">
ln -s updates.txt newitems.txt
</syntaxhighlight>


Here we can see the symlink that we created in the previous example:
Here we can see the symlink that we created in the previous example:
ls -l newitems.txt
lrwxrwxrwx 1 linuxopsys linuxopsys 11 Apr 14 13:53 newitems.txt -> updates.txt


==sudo Command==
<syntaxhighlight lang="bash">
ls -l newitems.txt
lrwxrwxrwx 1 linuxopsys linuxopsys 11 Apr 14 13:53 newitems.txt -> updates.txt
</syntaxhighlight>
 
==''sudo'' Command==
Use the <code>sudo</code> command to execute commands as root or another Linux user, as specified in your security policy. This command allows you to execute commands that otherwise require a root password.
Use the <code>sudo</code> command to execute commands as root or another Linux user, as specified in your security policy. This command allows you to execute commands that otherwise require a root password.


Specify the command that requires root permissions after the sudo command. To run the useradd command as sudo to create a new user named bob:
Specify the command that requires root permissions after the sudo command. To run the useradd command as sudo to create a new user named bob:
$ sudo useradd bob


==apt Command==
<syntaxhighlight lang="bash">
$ sudo useradd bob
</syntaxhighlight>
 
==''apt'' Command==
Use the <code>apt</code> (''stands for advanced package tool'') command to install, update, or delete packages on [[Ubuntu]] and [[Debian]] [[Linux]] distributions.
Use the <code>apt</code> (''stands for advanced package tool'') command to install, update, or delete packages on [[Ubuntu]] and [[Debian]] [[Linux]] distributions.


Line 263: Line 392:


To update the package index by pulling the latest updates from the APT repository:
To update the package index by pulling the latest updates from the APT repository:
sudo apt update


To install the latest updates to all Linux packages:
<syntaxhighlight lang="bash">
sudo apt upgrade
sudo apt update
</syntaxhighlight>


Let's use apt command to install a package. To install the htop application, type:
To install the latest updates to all Linux packages:<syntaxhighlight lang="bash">
sudo apt install htop
sudo apt upgrade
</syntaxhighlight>Let's use apt command to install a package. To install the htop application, type:<syntaxhighlight lang="bash">
sudo apt install htop
</syntaxhighlight>


==dnf Command==
==''dnf'' Command==
'''DNF''' is a software package manager and is the successor to YUM (Yellow-Dog Updater Modified). Use the dnf (stand for dandified yum) command to install, update, or delete packages on Red Hat-based Linux distributions.
'''DNF''' is a software package manager and is the successor to YUM (Yellow-Dog Updater Modified). Use the dnf (stand for dandified yum) command to install, update, or delete packages on Red Hat-based Linux distributions.


To install the latest updates to all Red Hat Linux packages:
To install the latest updates to all [[Red Hat]] [[Linux]] packages:<syntaxhighlight lang="bash">
sudo dnf upgrade
sudo dnf upgrade
</syntaxhighlight>To remove the package epel-release:<syntaxhighlight lang="bash">
sudo dnf remove epel-release
</syntaxhighlight>


To remove the package epel-release:
==''useradd'' Command==
sudo dnf remove epel-release
Use the <code>useradd</code> command to add new users to your Linux system. This command enables you to add a new user with specific group preferences, user ID (UID), and login shell. When you run this command without any option, it creates a user using the default account settings specified in the /etc/default/useradd file.
 
==useradd Command==
Use the useradd command to add new users to your Linux system. This command enables you to add a new user with specific group preferences, user ID (UID), and login shell. When you run this command without any option, it creates a user using the default account settings specified in the /etc/default/useradd file.


To create a new user named steve that has GID 1000, and UID 1021, and will expire on 31st May 2022:
To create a new user named steve that has GID 1000, and UID 1021, and will expire on 31st May 2022:
  sudo useradd -g 1000 -u 1021 -e 2022-05-31 steve
  sudo useradd -g 1000 -u 1021 -e 2022-05-31 steve


==groupadd Command==
==''groupadd'' Command==
Use the groupadd command to add a new group to your Linux system. This command enables you to add a new group with specific group preferences and override default /etc/login.def default values. When you create a new user group using this command, it adds a new entry in the /etc/group file.
Use the <code>groupadd</code> command to add a new group to your Linux system. This command enables you to add a new group with specific group preferences and override default /etc/login.def default values. When you create a new user group using this command, it adds a new entry in the /etc/group file.


To create a new group named docs that has GID 1018:
To create a new group named docs that has GID 1018:
  sudo groupadd -g 1018 docs
  sudo groupadd -g 1018 docs


==usermod Command==
==''usermod'' Command==
Use the usermod command to change the properties of existing users in Linux. Using this command, you can modify the password, primary group, login directory, expiry date, and other user attributes. You can also add a user to an existing group using this command.
Use the <code>usermod</code> command to change the properties of existing users in Linux. Using this command, you can modify the password, primary group, login directory, expiry date, and other user attributes. You can also add a user to an existing group using this command.


To add the user steve to sudo group, type:
To add the user steve to sudo group, type:
  sudo usermod -aG sudo steve
  sudo usermod -aG sudo steve


==ps Command==
==''ps'' Command==
Use the ps command to check the status of the active processes on your Linux system, and display information about these processes. Administrators can use this information to kill the processes or to set process priorities.
Use the <code>ps</code> command to check the status of the active processes on your Linux system, and display information about these processes. Administrators can use this information to kill the processes or to set process priorities.


To show all running processes in the default output format:
To show all running processes in the default output format:
Line 307: Line 439:
  ps -af
  ps -af


==kill Command==
==''kill'' Command==
Use the kill command to manually terminate a given process. It is located in /bin/kill and it sends signals to another process that terminates the specified process.
Use the <code>kill</code> command to manually terminate a given process. It is located in /bin/kill and it sends signals to another process that terminates the specified process.


To display all available signals:
To display all available signals:
Line 316: Line 448:
  kill 257
  kill 257


==ping Command==
==''ping'' Command==
Use the ping, also known as the Packet Internet Groper, command to check network connectivity between two computers. You can use this command to troubleshoot connectivity, name resolution, and reachability. It accepts an IP address or an URL as an input.
Use the ping, also known as the Packet Internet Groper, command to check network connectivity between two computers. You can use this command to troubleshoot connectivity, name resolution, and reachability. It accepts an IP address or an URL as an input.


Line 322: Line 454:
  ping -c 5 172.168.1.1
  ping -c 5 172.168.1.1


==vi and nano Command==
==''vi'' and ''nano'' Command==
Use the vi and nano commands to create a new file, read a file, or edit an existing file. Vi is the default text editor for several Linux distributions and it is very simple to use. Nano is a terminal-based text editor that is ideal for making some changes to any files or creating basic plain text files.
Use the <code>vi</code> and <code>nano</code> commands to create a new file, read a file, or edit an existing file. Vi is the default text editor for several Linux distributions and it is very simple to use. Nano is a terminal-based text editor that is ideal for making some changes to any files or creating basic plain text files.


To open the existing file or to create a new file:
To open the existing file or to create a new file:
Line 329: Line 461:
or
or
  nano filename.txt
  nano filename.txt
==history Command==
==''history'' Command==
Use the history command to view the history of all the previously executed terminal commands that you run. The maximum number of entries that BASH should store can be defined in your .bashrc file. Linux commands that you execute are considered events and every event is associated with an event number. You can recall, remove, or change commands using their event numbers.
Use the <code>history</code> command to view the history of all the previously executed terminal commands that you run. The maximum number of entries that BASH should store can be defined in your .bashrc file. Linux commands that you execute are considered events and every event is associated with an event number. You can recall, remove, or change commands using their event numbers.


To list the last 5 commands, including itself:
To list the last 5 commands, including itself:
Line 337: Line 469:
To remove the history of event number 1525:
To remove the history of event number 1525:
  history -d 1525
  history -d 1525
<blockquote>'''''Note:''' history command behavior may change depending on the shell you are being used.''</blockquote>


Note: history command behavior may change depending on the shell you are being used.
==''passwd'' Command==
 
Use the <code>passwd</code> command to change the user account password. A normal user can change only their own password, but the root user or user with sudo privileges can change the password of any user account. This command can also be used to delete or expire an account password.
==passwd Command==
Use the passwd command to change the user account password. A normal user can change only their own password, but the root user or user with sudo privileges can change the password of any user account. This command can also be used to delete or expire an account password.


To change the password for the user bob:
To change the password for the user bob:
  sudo passwd bob
  sudo passwd bob


==which Command==
==''which'' Command==
Use the which command to locate the executable file of the specified command. This command searches for the executable file location in the $PATH environment variable. The which command returns any of the following status:
Use the <code>which</code> command to locate the executable file of the specified command. This command searches for the executable file location in the <code>$PATH</code> environment variable. The which command returns any of the following status:


To display executable file locations for useradd command, type:
To display executable file locations for useradd command, type:
  which useradd /usr/sbin/useradd
  which useradd /usr/sbin/useradd


==less Command==
==''less'' Command==
Use the less command to view the contents of a file one screen at a time, starting at the beginning. This command provides the capability of both backward and forward navigation. The less command has quick file access because it does not access the entire file if the file is large.
Use the <code>less</code> command to view the contents of a file one screen at a time, starting at the beginning. This command provides the capability of both backward and forward navigation. The less command has quick file access because it does not access the entire file if the file is large.


To display the contents of the updates.txt file one screen at a time.
To display the contents of the updates.txt file one screen at a time.
  less updates.txt
  less updates.txt


==tail Command==
==''tail'' Command==
Use the tail command to view the contents of a file, starting at the bottom. By default, it displays the last 10 lines of the file, but you can change this default behavior. The tail command can also be used to monitor the file. When you add new lines to the file, then the display is updated.
Use the <code>tail</code> command to view the contents of a file, starting at the bottom. By default, it displays the last 10 lines of the file, but you can change this default behavior. The tail command can also be used to monitor the file. When you add new lines to the file, then the display is updated.


To display the last 10 lines of the file updates.txt:
To display the last 10 lines of the file updates.txt:
Line 367: Line 498:
  tail -n 15 access.log
  tail -n 15 access.log


==head Command==
==''head'' Command==
Use the head command to display the contents of a file, starting at the beginning. By default, it displays the first 10 lines of the file, but you can change this default behavior.
Use the <code>head</code> command to display the contents of a file, starting at the beginning. By default, it displays the first 10 lines of the file, but you can change this default behavior.


To display the first 10 lines of the file updates.txt:
To display the first 10 lines of the file updates.txt:
  head updates.txt
  head updates.txt


==grep Command==
==''grep'' Command==
Use the grep, also known as global regular expression print, the command to search for a word or string of characters in a file or directory. The search pattern is termed regular expression. When the grep command finds a matching string in the file, then this command prints the line that contains the string.
Use the <code>grep</code>, also known as global regular expression print, the command to search for a word or string of characters in a file or directory. The search pattern is termed regular expression. When the grep command finds a matching string in the file, then this command prints the line that contains the string.


To search for the string 'welcome' in the file log.txt:
To search for the string 'welcome' in the file log.txt:
  grep welcome log.txt
  grep welcome log.txt


==diff Command==
==''diff'' Command==
Use the diff command to do a line-by-line comparison of two files and display their differences. You can also use this command to compare the contents of Linux directories.
Use the <code>diff</code> command to do a line-by-line comparison of two files and display their differences. You can also use this command to compare the contents of Linux directories.


To show differences between two files named source.java and newsource.java, type
To show differences between two files named source.java and newsource.java, type
Line 388: Line 519:
  diff Documents/ Docs
  diff Documents/ Docs


==find Command==
==''find'' Command==
Use the find command to search and locate the files and directories that match the specified conditions. This command enables you to search using file or folder name, creation date, modification date, permissions, and owner.
Use the <code>find</code> command to search and locate the files and directories that match the specified conditions. This command enables you to search using file or folder name, creation date, modification date, permissions, and owner.


To search the file named program.py in the /home/linuxopsys/projects directory, type:
To search the file named program.py in the <code>/home/linuxopsys/projects</code> directory, type:
  find /home/linuxopsys/projects -type f -name program.py
  find /home/linuxopsys/projects -type f -name program.py


Line 397: Line 528:
  find . -perm 664
  find . -perm 664


==echo Command==
==''echo'' Command==
Use the echo command to display the specified text or string on the terminal. This command is most commonly used in batch files and scripting languages to display standard text output. You can also use this command to display the values of an environment variable.
Use the <code>echo</code> command to display the specified text or string on the terminal. This command is most commonly used in batch files and scripting languages to display standard text output. You can also use this command to display the values of an environment variable.


To print the string “This is a sample string” on the terminal:
To print the string <code>This is a sample string</code> on the terminal:
  echo "This is a sample string"
  echo "This is a sample string"


This is a sample string
This is a sample string


To print the value of the PATH environment variable:
To print the value of the <code>PATH</code> environment variable:
  echo $PATH
  echo $PATH


Line 411: Line 542:
  /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
  /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin


==shutdown, reboot Command==
==''shutdown'', ''reboot'' Command==
Use the shutdown command to gracefully and securely shut down your Linux computer.
Use the <code>shutdown</code> command to gracefully and securely shut down your [[Linux]] computer.


Use the reboot command to restart your Linux computer. You can also use the shutdown -r command to restart your computer using the shutdown command.   
Use the <code>reboot</code> command to restart your Linux computer. You can also use the shutdown -r command to restart your computer using the shutdown command.   


To restart your system after 5 minutes:
To restart your system after 5 minutes:
Line 422: Line 553:
  reboot
  reboot


==Alias==
==''alias'' Command==
The Alias command allows you to define temporary aliases for current sessions. It allows you to execute one or more commands.
The Alias command allows you to define temporary aliases for current sessions. It allows you to execute one or more commands.


Create a new alias named 'lsall', type:
Create a new alias named <code>lsall</code>, type:
  alais lsall="ls -a"
  alais lsall="ls -a"


Lists all aliases for the current session:
Lists all aliases for the current session:
  alias
  alias
<blockquote>'''''Note:''' Alias behavior may change depending on the shell you are being used. It is traditionally used in bash shell.''</blockquote>


Note: Alias behavior may change depending on the shell you are being used. It is traditionally used in bash shell.
==''exit'' Command==
 
The <code>exit</code> command ends a shell session and closes the terminal you are using
==exit==
The exit command ends a shell session and closes the terminal you are using
  exit
  exit


==wget command==
==''wget'' Command==
The wget command is used to retrieve or download content from the internet.
The <code>wget</code> command is used to retrieve or download content from the internet.


To download WordPress using wget, type:
To download [[WordPress]] using <code>wget</code>, type:
  wget https://wordpress.org/latest.tar.gz
  wget https://wordpress.org/latest.tar.gz


The latest.tar.gz file will be downloaded to the current directory.
The <code>latest.tar.gz</code> file will be downloaded to the current directory.


==clear command==
==''clear'' Command==
The clear command is used in Linux to clear the terminal screen. This is similar to cls command in other operating systems.
The <code>clear</code> command is used in Linux to clear the terminal screen. This is similar to cls command in other operating systems.
  clear
  clear


Line 452: Line 582:
*[https://linuxopsys.com/topics/basic-linux-commands linuxopsys.com]
*[https://linuxopsys.com/topics/basic-linux-commands linuxopsys.com]


{{DISPLAYTITLE:Linux Basic Commands}}
[[Category:Linux]]
[[Category:Tutorial]]

Latest revision as of 20:50, 2 February 2023

ls Command

ls is one of the most frequently used basic command in Linux. The ls command-line utility is used to list files and directories that exist inside a directory.

By default, when used without any options, ls lists only the contents of the current directory in alphabetical order without any additional information.

ls

Type ls /home/bob/Project to see the contents of the directory Project.

You can use options with the ls command for variation in results:

  • ls -a lists hidden files along with other files and directories.
  • ls -al lists files and directories with file permissions, ownership, and size information.
  • ls -ltr list file names in the last modification time in reverse order.

The output of ls -al

-rw-r--r-- 1 bob developers 281 Apr 1 2020 /source/project/learn.py

The owner (ie bob) has read and write permissions, other members of the group have read permissions, and the rest of the world has read permissions on the file. The file is owned by the user named bob and belongs to the developers group. The total size of the file is 281 bytes.

cd Command

cd stands for change directory, which is used to change the current working directory. The cd command is used to navigate through Linux files and directories. You need to specify either the full path of the target directory or the directory name you want to change to.

If you are in the /home/bob/Documents directory and need to go to the Photos subdirectory, then type:

cd Photos

Use the absolute path to change directory, change to /home/bob/Pictures directory:

cd /home/bob/Pictures

Some shortcuts that you can use for easy navigation:

  • cd .. moves one directory up.
  • cd moves to the home folder.

cd - switch to the parent directory

mkdir Command

The mkdir stands for make directory. The mkdir command is used to create a new directory in Linux.

When you type mkdir Pop, it will create a directory named Pop in the current directory. Whereas, the following Linux command will create a directory named Source inside the Documents directory:

mkdir Documents/Source

Use -p option to create the entire directory structure. For example to create a directory 2020 and its sub-directory Source in the existing Documents directory, type:

mkdir -p Documents/2020/Source

cat Command

The cat stands for concatenate. It is one of the basic command in the Linux operating system. The cat command is used to concatenate files, view the content of a file, create a new file, or redirect output in files.

Type cat command followed by a file name to see the contents of the file.

cat users.txt

The cat command can also be used to perform some advanced operations, such as:

To create a new file named file1.txt, type:

cat > file1.txt

The existing contents of the department.txt file will be overwritten by the contents of users.txt:

cat users.txt > department.txt

To convert the content of file users.txt from lowercase to uppercase and store it in output.txt, use the following command:

cat users.txt | tr a-z A-Z > output.txt

df Command

The df stands for disk filesystem. It is used to get a complete summary of used and available disk space of the file system in your Linux computer.

Options can be used with the df command to get variations in the output such as:

  • -a displays all file systems.
  • -h displays output in a human-readable format.
  • -T displays file system type.
  • -m displays output in mebibyte (MiB).
  • -g displays output in gibibyte (GiB).

Check disk usage information of a particular file system, type:

df /dev/sda5

Output

Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/sda5      124595628 40714508  77505800  35% /

Here, /dev/sda5 has a total size of 40503552, the used size is 9132528, and the available size is 29283856. The file system has used 24% of the total allocated space and it is mounted on /.

du Command

Use the du (stands for disk usage) command to gain disk usage information of a particular file or directory on your Linux computer. By default, the du command displays disk usage information in the number of blocks used by a file. Use the -h option with this command to display output in a human-readable format.

Specify the file name or directory name to display its disk usage.

du -h /var/log/apache/access.log
du -h /var/log

cp Command

The cp command is used to copy files and directories from the source directory to another location. By default, it copies only the given file or directory, but you can use the -r option to copy a directory along with its subdirectories.

The following command copy the file users.txt from the current working directory to the Documents/records directory.

cp users.txt Documents/records/

To create a copy of the file file2.txt with the name file2_backup.txt in your current working directory.

cp file2.txt file2_backup.txt

mv Command

Use the mv (stands for move) command to move files or directories from the source to the destination directory. It can be used to rename a file/directory.

The following mv command moves the users.txt file to the Documents/records directory.

mv users.txt Documents/records

To rename the employees.txt file to users.txt:

mv employees.txt users.txt

rm Command

Use the rm (stands for remove) command to remove the given file, multiple files, or a group/type of files. By default, the rm command does not require user confirmation to delete a file, but you can enable user confirmation using the -i option.

Options can be used with the rm command for the following needs:

  • -i deletes files in interactive mode.
  • -f forcefully removes the write-protected file.
  • -r recursively deletes files, directories, and subdirectories in the specified parent directory.
  • -d deletes the specified empty directory.

Removes the file named documents.txt from the current directory:

rm documents.txt

To remove directories and their contents recursively, type:

rm -r Documents

To remove the directory without being prompted, type:

rm -rf dir1

The rm-rf command should be used with caution.

man & help Command

Use the man command to display the user manual of the Linux command. Almost all the Linux commands have man pages, which is a kind of documentation that is displayed on the terminal. The Linux commands manual page explains what a particular command does, syntax, and accepted arguments.

Type man followed by the command name to display the command user manual page.

man mkdir

Similarly, you can also use the –-help option to display the help pages of a particular command.

mkdir --help

chmod Command

The chmod command is used to change the mode (permission) of a file. The basic permissions a file can have are r(read), w(write), and x(execute).

Using numeric mode you can set read (value is 4), write (value is 2 and execute (value is 1) permissions for owner, group and all others.

For example to give file1.txt owner and group read and write permissions and only read permission to all others, type:

chmod 664 file1.txt

You may use -R option to recursively set permissions for all files and directories in a given directory.

To make the file executable, type:

chmod +x myscript.sh

chown Command

The chown command is used to change file and directory ownership in Linux.

To change both owner and group of file, type:

chown ownername:groupname filename

You may use -R option to recursively set ownership for all files and directories in a given directory.

pwd Command

pwd stands for present working directory. The pwd command prints the absolute path of the current directory on your Linux terminal. The present working directory is the directory in which you are currently working.

pwd

Output

/home/linuxops

In the example /home/kagtain is the current directory.

uname Command

Use the uname (stands for UNIX name) command to display fundamental information about your Linux computer. By default, this command prints only the type of the operating system, such as Linux. You can use different options with this command to print other information about your computer such as OS, kernel version, machine name, network, and so on.

  • -a display all information about your computer.
  • -s display Linux kernel name.
  • -m displays machine information.
  • -i display hardware platform information.

The output of uname -a command on my machine:

Linux linux 5.13.0-37-generic #42~20.04.1-Ubuntu SMP Tue Mar 15 15:44:28 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

All system information including OS type, name, user name, kernel release, and hardware platform is displayed.

who Command

Use the who command to list currently logged-in users on your Linux computer. This command also supports few options to display specific information, such as:

  • -a displays all available information about logged-in users.
  • -b displays boot time.
  • -H displays header names.

The output of who command looks like this:

root     pts/0        2022-04-28 01:30 (192.168.1.10)
bob      pts/1        2022-04-28 02:40 (192.168.1.22)
kangtain pts/2        2022-04-28 02:05 (192.168.1.22)

It shows 3 users with their logged in date and time.

uptime Command

Use the uptime command to display the current system time, the duration for which the system has been up, the number of logged-in users, and average load time for 1, 5, and 15-minute intervals. This Linux command retrieves information from the /proc/uptime file.

The output of uptime command from my system:

17:11:28 up 6 days,  3:23,  1 user,  load average: 0.00, 0.01, 0.00

hostname Command

Use hostname command to display the Domain Name System (DNS) name and set the hostname or Network Information System (NIS) of your system.

Display host name and domain name, type:

hostname
kangtain

Some distributions allow you to set hostname for your machine, type:

sudo hostname NEW_HOSTNAME

You can replace NEW_HOSTNAME with your actual machine name.

top, htop Command

Use the top or htop commands to monitor the server’s processes or vital system resources in real-time. Both these commands are used for the same purpose, but their output is different. The default output displays information about top CPU-consuming processes along with RAM usage.

top

The command also displays system up time, load average, number of logged in users, memory, total tasks, and running tasks.

Htop is an improved version of the top command. It display colorful text and allows for output scrolling. Htop doesnt comes preinstalled on most Linux distros.

touch Command

Use the touch command to create an empty file on your Linux computer, or to modify the timestamp of a file. The primary function of this command is to update file timestamps, but it is most commonly used to create a file. The touch command can update access and modification timestamps of the specified file, and create the file if it does not already exist.

To create a new empty file named install.doc, type:

touch install.doc

In this example, the modification time of the file updates.txt is updated to the current time.

touch -m updates.txt

zip and unzip Command

Use the zip command-line utility to create an archive of files, and the unzip command for uncompressing a zip archive. The zip command can also be used to modify a zip archive.

To create a zip archive named users.zip from the specified files:

zip users.zip output.txt install.doc newfiles.txt updates.txt

To extract the contents of the users.zip archive to the current directory:

unzip users.zip

tar Command

Use the tar command to create a compressed or uncompressed archive of specified files. You can also use this command to modify, maintain, or extract tar archives. Tar archives can combine multiple files or directories into a single file.

The tar command provides multiple options to create a tarball:

  • -c creates an archive.
  • -x extracts the archive.
  • -f assigns filename to the archive.
  • -t displays files in an archive.

To create an uncompressed archive file named users.tar of the specified files.

tar cf users.tar updates.txt newfiles.txt

ln Command

Use the ln command for creating hard and soft links to existing files or directories. By default, this command creates hard links. Specify the -s option to create symbolic or soft links.

To create a symbolic link from the updates.txt file to the newitems.txt file:

ln -s updates.txt newitems.txt

Here we can see the symlink that we created in the previous example:

ls -l newitems.txt
lrwxrwxrwx 1 linuxopsys linuxopsys 11 Apr 14 13:53 newitems.txt -> updates.txt

sudo Command

Use the sudo command to execute commands as root or another Linux user, as specified in your security policy. This command allows you to execute commands that otherwise require a root password.

Specify the command that requires root permissions after the sudo command. To run the useradd command as sudo to create a new user named bob:

$ sudo useradd bob

apt Command

Use the apt (stands for advanced package tool) command to install, update, or delete packages on Ubuntu and Debian Linux distributions.

Some of the most commonly used apt commands are apt update, apt upgrade, apt install, apt remove, apt list, and apt search.

To update the package index by pulling the latest updates from the APT repository:

sudo apt update

To install the latest updates to all Linux packages:

sudo apt upgrade

Let's use apt command to install a package. To install the htop application, type:

sudo apt install htop

dnf Command

DNF is a software package manager and is the successor to YUM (Yellow-Dog Updater Modified). Use the dnf (stand for dandified yum) command to install, update, or delete packages on Red Hat-based Linux distributions.

To install the latest updates to all Red Hat Linux packages:

sudo dnf upgrade

To remove the package epel-release:

sudo dnf remove epel-release

useradd Command

Use the useradd command to add new users to your Linux system. This command enables you to add a new user with specific group preferences, user ID (UID), and login shell. When you run this command without any option, it creates a user using the default account settings specified in the /etc/default/useradd file.

To create a new user named steve that has GID 1000, and UID 1021, and will expire on 31st May 2022:

sudo useradd -g 1000 -u 1021 -e 2022-05-31 steve

groupadd Command

Use the groupadd command to add a new group to your Linux system. This command enables you to add a new group with specific group preferences and override default /etc/login.def default values. When you create a new user group using this command, it adds a new entry in the /etc/group file.

To create a new group named docs that has GID 1018:

sudo groupadd -g 1018 docs

usermod Command

Use the usermod command to change the properties of existing users in Linux. Using this command, you can modify the password, primary group, login directory, expiry date, and other user attributes. You can also add a user to an existing group using this command.

To add the user steve to sudo group, type:

sudo usermod -aG sudo steve

ps Command

Use the ps command to check the status of the active processes on your Linux system, and display information about these processes. Administrators can use this information to kill the processes or to set process priorities.

To show all running processes in the default output format:

ps

To display all running processes in full format:

ps -af

kill Command

Use the kill command to manually terminate a given process. It is located in /bin/kill and it sends signals to another process that terminates the specified process.

To display all available signals:

kill -l

To kill the PID 257:

kill 257

ping Command

Use the ping, also known as the Packet Internet Groper, command to check network connectivity between two computers. You can use this command to troubleshoot connectivity, name resolution, and reachability. It accepts an IP address or an URL as an input.

To check network connectivity to 172.168.1.1 IP address and send 5 data packets:

ping -c 5 172.168.1.1

vi and nano Command

Use the vi and nano commands to create a new file, read a file, or edit an existing file. Vi is the default text editor for several Linux distributions and it is very simple to use. Nano is a terminal-based text editor that is ideal for making some changes to any files or creating basic plain text files.

To open the existing file or to create a new file:

vi updates.txt

or

nano filename.txt

history Command

Use the history command to view the history of all the previously executed terminal commands that you run. The maximum number of entries that BASH should store can be defined in your .bashrc file. Linux commands that you execute are considered events and every event is associated with an event number. You can recall, remove, or change commands using their event numbers.

To list the last 5 commands, including itself:

history 5

To remove the history of event number 1525:

history -d 1525

Note: history command behavior may change depending on the shell you are being used.

passwd Command

Use the passwd command to change the user account password. A normal user can change only their own password, but the root user or user with sudo privileges can change the password of any user account. This command can also be used to delete or expire an account password.

To change the password for the user bob:

sudo passwd bob

which Command

Use the which command to locate the executable file of the specified command. This command searches for the executable file location in the $PATH environment variable. The which command returns any of the following status:

To display executable file locations for useradd command, type:

which useradd /usr/sbin/useradd

less Command

Use the less command to view the contents of a file one screen at a time, starting at the beginning. This command provides the capability of both backward and forward navigation. The less command has quick file access because it does not access the entire file if the file is large.

To display the contents of the updates.txt file one screen at a time.

less updates.txt

tail Command

Use the tail command to view the contents of a file, starting at the bottom. By default, it displays the last 10 lines of the file, but you can change this default behavior. The tail command can also be used to monitor the file. When you add new lines to the file, then the display is updated.

To display the last 10 lines of the file updates.txt:

tail updates.txt

To display the last 15 lines of the file access.log:

tail -n 15 access.log

head Command

Use the head command to display the contents of a file, starting at the beginning. By default, it displays the first 10 lines of the file, but you can change this default behavior.

To display the first 10 lines of the file updates.txt:

head updates.txt

grep Command

Use the grep, also known as global regular expression print, the command to search for a word or string of characters in a file or directory. The search pattern is termed regular expression. When the grep command finds a matching string in the file, then this command prints the line that contains the string.

To search for the string 'welcome' in the file log.txt:

grep welcome log.txt

diff Command

Use the diff command to do a line-by-line comparison of two files and display their differences. You can also use this command to compare the contents of Linux directories.

To show differences between two files named source.java and newsource.java, type

diff /dir1/source.java /dir2/newsource.java

To show differences between Documents and Docs directories.

diff Documents/ Docs

find Command

Use the find command to search and locate the files and directories that match the specified conditions. This command enables you to search using file or folder name, creation date, modification date, permissions, and owner.

To search the file named program.py in the /home/linuxopsys/projects directory, type:

find /home/linuxopsys/projects -type f -name program.py

To search for files with given permissions in the current directory:

find . -perm 664

echo Command

Use the echo command to display the specified text or string on the terminal. This command is most commonly used in batch files and scripting languages to display standard text output. You can also use this command to display the values of an environment variable.

To print the string This is a sample string on the terminal:

echo "This is a sample string"

This is a sample string

To print the value of the PATH environment variable:

echo $PATH

Output:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

shutdown, reboot Command

Use the shutdown command to gracefully and securely shut down your Linux computer.

Use the reboot command to restart your Linux computer. You can also use the shutdown -r command to restart your computer using the shutdown command.

To restart your system after 5 minutes:

shutdown -r +5

To immediately restart your computer:

reboot

alias Command

The Alias command allows you to define temporary aliases for current sessions. It allows you to execute one or more commands.

Create a new alias named lsall, type:

alais lsall="ls -a"

Lists all aliases for the current session:

alias

Note: Alias behavior may change depending on the shell you are being used. It is traditionally used in bash shell.

exit Command

The exit command ends a shell session and closes the terminal you are using

exit

wget Command

The wget command is used to retrieve or download content from the internet.

To download WordPress using wget, type:

wget https://wordpress.org/latest.tar.gz

The latest.tar.gz file will be downloaded to the current directory.

clear Command

The clear command is used in Linux to clear the terminal screen. This is similar to cls command in other operating systems.

clear

Source