Jump to content

Basic Commands: Difference between revisions

From Wiki
Created page with "==ls Command== <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. ls Type ls <code>/home/bob/Project</code> to see the contents of the directory Project. You can use options with the ls command fo..."
 
 
(13 intermediate revisions by the same user not shown)
Line 1: Line 1:
==ls Command==
==''ls'' Command==
<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.
By default, when used without any options, ls lists only the contents of the current directory in alphabetical order without any additional information.


ls
<syntaxhighlight lang="shell">
ls
</syntaxhighlight>


Type ls <code>/home/bob/Project</code> to see the contents of the directory Project.
Type ls <code>/home/bob/Project</code> to see the contents of the directory Project.
Line 10: Line 12:
You can use options with the ls command for variation in results:
You can use options with the ls command for variation in results:


ls -a lists hidden files along with other files and directories.
*<code>ls -a</code> lists hidden files along with other files and directories.
ls -al lists files and directories with file permissions, ownership, and size information.
*<code>ls -al</code> lists files and directories with file permissions, ownership, and size information.
ls -ltr 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 ls -al
 
The output of <code>ls -al</code>
 
<syntaxhighlight lang="shell">
-rw-r--r-- 1 bob developers 281 Apr 1 2020 /source/project/learn.py
</syntaxhighlight>


-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.
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.


2. cd Command
==''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.
<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 /home/bob/Documents directory and need to go to the Photos subdirectory, then type:
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
Use the absolute path to change directory, change to /home/bob/Pictures directory:
</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:
Some shortcuts that you can use for easy navigation:


cd .. moves one directory up.
*<code>cd ..</code> moves one directory up.
cd moves to the home folder.
*<code>cd</code> moves to the home folder.
cd - switch to the parent directory
<code>cd -</code> switch to the parent directory
3. mkdir Command
 
The mkdir stands for make directory. The mkdir command is used to create a new directory in Linux.
==''mkdir'' Command==
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:
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
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:
</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
4. cat Command
</syntaxhighlight>
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.
 
==''cat'' Command==
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.
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:
The cat command can also be used to perform some advanced operations, such as:


To create a new file named file1.txt, type:
To create a new file named <code>file1.txt</code>, type:


<syntaxhighlight lang="shell">
cat > file1.txt
cat > file1.txt
The existing contents of the department.txt file will be overwritten by the contents of users.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">
cat users.txt > department.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:
</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
5. df Command
</syntaxhighlight>
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:
==''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 <code>df</code> command to get variations in the output such as:
 
*<code>-a</code> displays all file systems.
*<code>-h</code> displays output in a human-readable format.
*<code>-T</code> displays file system type.
*<code>-m</code> displays output in mebibyte (MiB).
*<code>-g</code> displays output in gibibyte (GiB).


-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:
Check disk usage information of a particular file system, type:


<syntaxhighlight lang="shell">
df /dev/sda5
df /dev/sda5
Output
</syntaxhighlight>
Filesystem 1K-blocks Used       Available Use% Mounted on
 
'''Output'''
 
<syntaxhighlight lang="shell">
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/sda5      124595628 40714508  77505800  35% /
</syntaxhighlight>


/dev/sda5  40503552    9132528  29283856  24% /
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, /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 /.


6. du Command
==''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.
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.
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
7. cp Command
</syntaxhighlight>
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.
 
==''cp'' Command==
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 users.txt from the current working directory to the Documents/records directory.
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/
To create a copy of the file file2.txt with the name file2_backup.txt in your current working directory.
</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
8. mv Command
</syntaxhighlight>
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.
 
==''mv'' Command==
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 mv command moves the users.txt file to the Documents/records directory.
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
To rename the employees.txt file to users.txt:
</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
9. rm Command
</syntaxhighlight>
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.
 
==''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.


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


-i deletes files in interactive mode.
*<code>-i</code> deletes files in interactive mode.
-f forcefully removes the write-protected file.
*<code>-f</code> forcefully removes the write-protected file.
-r recursively deletes files, directories, and subdirectories in the specified parent directory.
*<code>-r</code> recursively deletes files, directories, and subdirectories in the specified parent directory.
-d deletes the specified empty directory.
*<code>-d</code> deletes the specified empty directory.
 
Removes the file named documents.txt from the current directory:
Removes the file named documents.txt from the current directory:


<syntaxhighlight lang="shell">
rm documents.txt
rm documents.txt
</syntaxhighlight>
To remove directories and their contents recursively, type:
To remove directories and their contents recursively, type:


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


<syntaxhighlight lang="shell">
rm -rf dir1
rm -rf dir1
The rm-rf command should be used with caution.
</syntaxhighlight>
 
The <code>rm-rf</code> command should be used with caution.


10. Man & help Command
==''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.
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.


<syntaxhighlight lang="shell">
man mkdir
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.


<syntaxhighlight lang="shell">
mkdir --help
mkdir --help
11. chmod command
</syntaxhighlight>
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.
==''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).


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


<syntaxhighlight lang="shell">
chmod +x myscript.sh
chmod +x myscript.sh
12. chown command
</syntaxhighlight>
The chown command is used to change file and directory ownership in Linux.
 
==''chown'' Command==
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:


<syntaxhighlight lang="shell">
chown ownername:groupname filename
chown ownername:groupname filename
You may use -R option to recursively set ownership for all files and directories in a given directory.
</syntaxhighlight>
 
You may use <code>-R</code> option to recursively set ownership for all files and directories in a given directory.


13. pwd Command
==''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.
<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.


<syntaxhighlight lang="shell">
pwd
pwd
Output
</syntaxhighlight>
/home/linuxopsys
 
In the example /home/linuxopsys is the current directory.
'''Output'''
 
<syntaxhighlight lang="shell">
/home/linuxops
</syntaxhighlight>
 
In the example <code>/home/kagtain</code> is the current directory.


14. uname Command
==''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.
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.
 
*<code>-a</code> display all information about your computer.
*<code>-s</code> display Linux kernel name.
*<code>-m</code> displays machine information.
*<code>-i</code> display hardware platform information.


-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:
The output of uname -a command on my machine:


<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
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.


15. who Command
==''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:
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:
 
*<code>-a</code> displays all available information about logged-in users.
*<code>-b</code> displays boot time.
*<code>-H</code> displays header names.


-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:
The output of who command looks like this:


<syntaxhighlight lang="shell">
root    pts/0        2022-04-28 01:30 (192.168.1.10)
root    pts/0        2022-04-28 01:30 (192.168.1.10)
bob      pts/1        2022-04-28 02:40 (192.168.1.22)
bob      pts/1        2022-04-28 02:40 (192.168.1.22)
linuxopsys pts/2        2022-04-28 02:05 (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.


16. 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 /proc/uptime 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:


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


Display host name and domain name, type:
Display host name and domain name, type:


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


<syntaxhighlight lang="shell">
sudo hostname NEW_HOSTNAME
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.


18. top, htop Command
==''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.
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.


<syntaxhighlight lang="shell">
top
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.


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.
<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.


19. touch Command
==''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.
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:


<syntaxhighlight lang="bash">
touch install.doc
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.


<syntaxhighlight lang="bash">
touch -m updates.txt
touch -m updates.txt
20. zip and unzip Command
</syntaxhighlight>
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'' 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:
<syntaxhighlight lang="bash">
zip users.zip output.txt install.doc newfiles.txt updates.txt
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:  


<syntaxhighlight lang="bash">
unzip users.zip
unzip users.zip
21. tar Command
</syntaxhighlight>
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.
 
==''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.


The tar command provides multiple options to create a tarball:
The tar command provides multiple options to create a tarball:
*<code>-c</code> creates an archive.
*<code>-x</code> extracts the archive.
*<code>-f</code> assigns filename to the archive.
*<code>-t</code> displays files in an archive.


-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.
To create an uncompressed archive file named users.tar of the specified files.


<syntaxhighlight lang="bash">
tar cf users.tar updates.txt newfiles.txt
tar cf users.tar updates.txt newfiles.txt
22. ln Command
</syntaxhighlight>
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.
 
==''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.


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:


<syntaxhighlight lang="bash">
ln -s updates.txt newitems.txt
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:


<syntaxhighlight lang="bash">
ls -l newitems.txt
ls -l newitems.txt
lrwxrwxrwx 1 linuxopsys linuxopsys 11 Apr 14 13:53 newitems.txt -> updates.txt
lrwxrwxrwx 1 linuxopsys linuxopsys 11 Apr 14 13:53 newitems.txt -> updates.txt
23. sudo Command
</syntaxhighlight>
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.
 
==''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.


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:


<syntaxhighlight lang="bash">
$ sudo useradd bob
$ sudo useradd bob
24. apt Command
</syntaxhighlight>
Use the apt (stands for advanced package tool) command to install, update, or delete packages on Ubuntu and Debian Linux distributions.
 
==''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.


Some of the most commonly used apt commands are apt update, apt upgrade, apt install, apt remove, apt list, and apt search.
Some of the most commonly used apt commands are apt update, apt upgrade, apt install, apt remove, apt list, and apt search.
Line 264: Line 393:
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:


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


To install the latest updates to all Linux packages:<syntaxhighlight lang="bash">
sudo apt upgrade
sudo apt upgrade
Let's use apt command to install a package. To install the htop application, type:
</syntaxhighlight>Let's use apt command to install a package. To install the htop application, type:<syntaxhighlight lang="bash">
 
sudo apt install htop
sudo apt install htop
25. dnf Command
</syntaxhighlight>
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:
==''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:<syntaxhighlight lang="bash">
sudo dnf upgrade
sudo dnf upgrade
To remove the package epel-release:
</syntaxhighlight>To remove the package epel-release:<syntaxhighlight lang="bash">
sudo dnf remove epel-release
</syntaxhighlight>


sudo dnf remove epel-release
==''useradd'' Command==
26. useradd Command
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.
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==
27. groupadd Command
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.
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:
To create a new group named docs that has GID 1018:
sudo groupadd -g 1018 docs


sudo groupadd -g 1018 docs
==''usermod'' Command==
28. usermod 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.
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:
To add the user steve to sudo group, type:
sudo usermod -aG sudo steve


sudo usermod -aG sudo steve
==''ps'' Command==
29. ps Command
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.
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:
To show all running processes in the default output format:
ps


ps
To display all running processes in full format:  
To display all running processes in full format:  
ps -af


ps -af
==''kill'' Command==
30. kill Command
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.
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:
To display all available signals:
kill -l


kill -l
To kill the PID 257:
To kill the PID 257:
kill 257


kill 257
==''ping'' Command==
31. 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.


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


ping -c 5 172.168.1.1
==''vi'' and ''nano'' Command==
32. vi and nano Command
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.
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:
To open the existing file or to create a new file:
 
vi updates.txt
vi updates.txt
or
or
nano filename.txt
nano filename.txt
33. 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:
history 5


history 5
To remove the history of event number 1525:
To remove the history of event number 1525:
history -d 1525
<blockquote>'''''Note:''' history command behavior may change depending on the shell you are being used.''</blockquote>


history -d 1525
==''passwd'' Command==
Note: history command behavior may change depending on the shell you are being used.
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.
 
34. 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==
35. which Command
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:
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:
To display executable file locations for useradd command, type:
which useradd /usr/sbin/useradd


which useradd /usr/sbin/useradd
==''less'' Command==
36. less Command
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.
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.
To display the contents of the updates.txt file one screen at a time.
less updates.txt


less updates.txt
==''tail'' Command==
37. tail Command
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.
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:
To display the last 10 lines of the file updates.txt:
tail updates.txt


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


tail -n 15 access.log
==''head'' Command==
38. head Command
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.
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:
To display the first 10 lines of the file updates.txt:
head updates.txt


head updates.txt
==''grep'' Command==
39. grep Command
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.
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:
To search for the string 'welcome' in the file log.txt:
grep welcome log.txt


grep welcome log.txt
==''diff'' Command==
40. diff Command
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.
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
To show differences between two files named source.java and newsource.java, type
diff /dir1/source.java /dir2/newsource.java


diff /dir1/source.java /dir2/newsource.java
To show differences between Documents and Docs directories.
To show differences between Documents and Docs directories.
diff Documents/ Docs


diff Documents/ Docs
==''find'' Command==
41. find Command
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.
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:
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
To search for files with given permissions in the current directory:
To search for files with given permissions in the current directory:
find . -perm 664


find . -perm 664
==''echo'' Command==
42. echo Command
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.
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:
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:


echo $PATH
To print the value of the <code>PATH</code> environment variable:
echo $PATH
 
Output:
Output:
/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==
43. shutdown, reboot Command
Use the <code>shutdown</code> command to gracefully and securely shut down your [[Linux]] computer.
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.   
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:
shutdown -r +5


shutdown -r +5
To immediately restart your computer:
To immediately restart your computer:
reboot


reboot
==''alias'' Command==
44. Alias
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
<blockquote>'''''Note:''' Alias behavior may change depending on the shell you are being used. It is traditionally used in bash shell.''</blockquote>


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


45. exit
==''wget'' Command==
The exit command ends a shell session and closes the terminal you are using
The <code>wget</code> command is used to retrieve or download content from the internet.


exit
To download [[WordPress]] using <code>wget</code>, type:
46. wget command
wget https://wordpress.org/latest.tar.gz
The wget command is used to retrieve or download content from the internet.


To download WordPress using wget, type:
The <code>latest.tar.gz</code> file will be downloaded to the current directory.


wget https://wordpress.org/latest.tar.gz
==''clear'' Command==
The latest.tar.gz file will be downloaded to the current directory.
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
47. 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==
==Source==
*[https://linuxopsys.com/topics/basic-linux-commands linuxopsys.com]
*[https://linuxopsys.com/topics/basic-linux-commands linuxopsys.com]
[[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