Basic Commands: Difference between revisions

m Kangtain moved page Linux/Basic Commands to Basic Commands
 
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>
Line 108: Line 165:
*<code>-d</code> deletes the specified empty directory.
*<code>-d</code> deletes the specified empty directory.


Removes the file named documents.txt from the current directory:<syntaxhighlight lang="shell">
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:<syntaxhighlight lang="shell">
</syntaxhighlight>
 
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:<syntaxhighlight lang="shell">
</syntaxhighlight>
 
To remove the directory without being prompted, type:
 
<syntaxhighlight lang="shell">
rm -rf dir1
rm -rf dir1
</syntaxhighlight>The <code>rm-rf</code> command should be used with caution.
</syntaxhighlight>
 
The <code>rm-rf</code> command should be used with caution.


==''man'' & ''help'' Command==
==''man'' & ''help'' Command==
Line 250: Line 319:
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:<syntaxhighlight lang="bash">
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.<syntaxhighlight lang="bash">
</syntaxhighlight>
 
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
</syntaxhighlight>
</syntaxhighlight>
Line 259: Line 334:
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.
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">
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: <syntaxhighlight lang="bash">
</syntaxhighlight>
 
To extract the contents of the users.zip archive to the current directory:  
 
<syntaxhighlight lang="bash">
unzip users.zip
unzip users.zip
</syntaxhighlight>
</syntaxhighlight>
Line 274: Line 355:
*<code>-t</code> displays files in an archive.
*<code>-t</code> displays files in an archive.


To create an uncompressed archive file named users.tar of the specified files.<syntaxhighlight lang="bash">
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
</syntaxhighlight>
</syntaxhighlight>
Line 281: Line 364:
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:<syntaxhighlight lang="bash">
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:<syntaxhighlight lang="bash">
</syntaxhighlight>
 
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
Line 291: Line 380:
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:<syntaxhighlight lang="bash">
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
</syntaxhighlight>
</syntaxhighlight>
Line 300: Line 391:
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.


To update the package index by pulling the latest updates from the APT repository:<syntaxhighlight lang="bash">
To update the package index by pulling the latest updates from the APT repository:
 
<syntaxhighlight lang="bash">
sudo apt update
sudo apt update
</syntaxhighlight>To install the latest updates to all Linux packages:<syntaxhighlight lang="bash">
</syntaxhighlight>
 
To install the latest updates to all Linux packages:<syntaxhighlight lang="bash">
sudo apt upgrade
sudo apt upgrade
</syntaxhighlight>Let's use apt command to install a package. To install the htop application, type:<syntaxhighlight lang="bash">
</syntaxhighlight>Let's use apt command to install a package. To install the htop application, type:<syntaxhighlight lang="bash">
Line 317: Line 412:
</syntaxhighlight>
</syntaxhighlight>


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


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 344: 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 353: 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 359: 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 366: 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 374: 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 404: 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 425: 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 434: 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 448: 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 459: 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