Daily life Linux Commands - GeeksforGeeks

06 Aug, 2020
Clear the Terminal : In our daily life, we use to work on Terminal if we are using LINUX. Continuous working on terminal makes the terminal screen full of commands and for removing them and making our screen totally free of character, we often use clear command. Key combination ‘Ctrl+l‘ has the same effect as ‘clear‘ command. So from next time use ctrl+l to clear your Linux Command Line Interface.Note: Since ctrl+l is a key combination, so we can not use it inside a script. If we need to clear screen inside a shell script, we just have to call command ‘clear’.Run command and get back to the directory, together:  This is also an amazing hack not known to many people. We may run command no matter what it is and then return back to the current directory. For this, all we need to do is to run the command in parentheses i.e., in between ( and ).For Example :Input :cd /home/shivam/Downloads/ && ls -lOutput :-rw-r----- 1 shivam shivam    54272 May 3 18:37 text1.txt
-rw-r----- 1 shivam shivam    54272 May 3 18:37 text2.txt
-rw-r----- 1 shivam shivam    54272 May 3 18:37 text3.txt
Explanation : In the above command it first changed the current directory to Downloads and then list the content of that directory before returning back to the current directory.Shortcut to Directories: You can create a shortcut to frequently accessed directories by adding them to the CDPATH environment variable. So, say If you frequently access “/var/www/html/”.Instead of typing “cd /var/www/html”, you can add /var/www/ to CDPATH and then you have to type “cd html” only.shivam:~> export CDPATH=$CDPATH:/var/www/
shivam:~> cd html
shivam:~:html>
Replacing words or characters:If you are working with any text file then to replace every instance of any word say “version” with “story” in myfile.txt, you can use sed command as:# sed 's/version/story/g' myfile.txt
 Additionally, if you want to ignore character case then you may use gi instead of g as:# sed 's/version/story/gi' myfile.txt
Here are some useful shortcuts which you may use while working on terminal:Cursor Movement Control:Ctrl-a: Move cursor to the start of a lineCtrl-e: Move cursor to the end of a lineCtrl-Left/Right: Navigate word by word (may not work in all terminals)Modify Text:Ctrl-w: Delete the whole word to the left of the cursorCtrl-k: Erase to end of lineCtrl-u: Erase to beginning of lineRun top in batch mode: ‘top’ is a handy utility for monitoring the utilization of your system. It is invoked from the command line and it works by displaying lots of useful information, including CPU and memory usage, the number of running processes, load, the top resource hitters, and other useful bits. By default, top refreshes its report every 3 seconds.Mostly we run ‘top’ inside the terminal, look on the statistics for a few seconds and then graciously quit and continue our work.Better yet, if we wants to run such a utility only for a given period of time, without any user interaction:There are many possible answers:You could schedule a job via cron.You could run a shell script that runs ps every X secondsInstead of going wild about trying to patch a script, there’s a much, much simpler solution:top -b -d 10 -n 3 >> top-file
We have top running in batch mode (-b). It’s going to refresh every 10 seconds, as specified by the delay (-d) flag, for a total count of 3 iterations (-n). The output will be sent to a file.Here is a screenshots of outut:Duplicate pipe content: ‘tee’ is a very useful utility that duplicates pipe content. Now, what makes tee really useful is that it can append data to existing files, making it ideal for writing periodic log information to multiple files at once.ps | tee file1 file2 file3
We’re sending the output of the ps command to three different files! Or as many as we want. As you can see in the screenshots below, all three files were created at the same time and they all contain the same data.export: The ‘export’ command is one of the bash shell BUILTINS commands.It has three available command options. In general, it marks an environment variable to be exported with any newly forked child processes and thus it allows a child process to inherit all marked variables.Frequently Used Options with ‘export’-p : List of all names that are exported in the current shell-n: Remove names from export list-f : Names are exported as functionsExample :command without ‘export’:$ a = geeksforgeeks.org
$ echo $a
geeksforgeeks.org
$ bash
$ echo $a
From the above we can see that any new child process forked from a parent process by default does not inherit parent’s variables. This is where the export command comes handy.$ a = geeksforgeeks.org
$ echo $a
geeksforgeeks.org
$ export a
$ bash
$ echo $a
geeksforgeeks.org
$On line 3, we now have used the export command to make the variable “a” to be exported when a new child process is created. As a result the variable “a” still contains the string “geeksforgeeks.org” even after a new bash shell was created.basename – Strips directory and suffix from filenames. basename prints NAME with any leading directory components removed. If Suffix is specified, it will also remove a trailing SUFFIX. For example: To get the name of the file present in test folder$ basename test/gfg.txt
gfg.txtgrep: grep searches files for a given character string or pattern and can replace the string with another. This is one method of searching for files within Linux.grep [option(s)] pattern [file(s)]Search number of files: grep can search any number of files simultaneously. Thus, for example, the following would search the three files file1, file2 and file3 for any line that contains the string GfGgrep GfG file1 file2 file3Search text in all files: To search all text files in the current directory (i.e., the directory in which the user is currently working), if there is a phrase “Linux is” grep 'Linux is' *There are lot of more options which can be tried using grep command.
Clear the Terminal : In our daily life, we use to work on Terminal if we are using LINUX. Continuous working on terminal makes the terminal screen full of commands and for removing them and making our screen totally free of character, we often use clear command. Key combination ‘Ctrl+l‘ has the same effect as ‘clear‘ command. So from next time use ctrl+l to clear your Linux Command Line Interface.Note: Since ctrl+l is a key combination, so we can not use it inside a script. If we need to clear screen inside a shell script, we just have to call command ‘clear’.
Run command and get back to the directory, together:  This is also an amazing hack not known to many people. We may run command no matter what it is and then return back to the current directory. For this, all we need to do is to run the command in parentheses i.e., in between ( and ).For Example :Input :cd /home/shivam/Downloads/ && ls -lOutput :-rw-r----- 1 shivam shivam    54272 May 3 18:37 text1.txt
-rw-r----- 1 shivam shivam    54272 May 3 18:37 text2.txt
-rw-r----- 1 shivam shivam    54272 May 3 18:37 text3.txt
Explanation : In the above command it first changed the current directory to Downloads and then list the content of that directory before returning back to the current directory.
cd /home/shivam/Downloads/ && ls -l
Output :
-rw-r----- 1 shivam shivam    54272 May 3 18:37 text1.txt
-rw-r----- 1 shivam shivam    54272 May 3 18:37 text2.txt
-rw-r----- 1 shivam shivam    54272 May 3 18:37 text3.txt

Explanation : In the above command it first changed the current directory to Downloads and then list the content of that directory before returning back to the current directory.
Shortcut to Directories: You can create a shortcut to frequently accessed directories by adding them to the CDPATH environment variable. So, say If you frequently access “/var/www/html/”.Instead of typing “cd /var/www/html”, you can add /var/www/ to CDPATH and then you have to type “cd html” only.shivam:~> export CDPATH=$CDPATH:/var/www/
shivam:~> cd html
shivam:~:html>

shivam:~> export CDPATH=$CDPATH:/var/www/
shivam:~> cd html
shivam:~:html>

Replacing words or characters:If you are working with any text file then to replace every instance of any word say “version” with “story” in myfile.txt, you can use sed command as:# sed 's/version/story/g' myfile.txt
 Additionally, if you want to ignore character case then you may use gi instead of g as:# sed 's/version/story/gi' myfile.txt

If you are working with any text file then to replace every instance of any word say “version” with “story” in myfile.txt, you can use sed command as:# sed 's/version/story/g' myfile.txt

# sed 's/version/story/g' myfile.txt

 Additionally, if you want to ignore character case then you may use gi instead of g as:# sed 's/version/story/gi' myfile.txt

# sed 's/version/story/gi' myfile.txt

Here are some useful shortcuts which you may use while working on terminal:Cursor Movement Control:Ctrl-a: Move cursor to the start of a lineCtrl-e: Move cursor to the end of a lineCtrl-Left/Right: Navigate word by word (may not work in all terminals)Modify Text:Ctrl-w: Delete the whole word to the left of the cursorCtrl-k: Erase to end of lineCtrl-u: Erase to beginning of line
Ctrl-a: Move cursor to the start of a line
Ctrl-e: Move cursor to the end of a line
Ctrl-Left/Right: Navigate word by word (may not work in all terminals)Modify Text:
Ctrl-w: Delete the whole word to the left of the cursor
Ctrl-k: Erase to end of line
Ctrl-u: Erase to beginning of line
Run top in batch mode: ‘top’ is a handy utility for monitoring the utilization of your system. It is invoked from the command line and it works by displaying lots of useful information, including CPU and memory usage, the number of running processes, load, the top resource hitters, and other useful bits. By default, top refreshes its report every 3 seconds.Mostly we run ‘top’ inside the terminal, look on the statistics for a few seconds and then graciously quit and continue our work.Better yet, if we wants to run such a utility only for a given period of time, without any user interaction:There are many possible answers:You could schedule a job via cron.You could run a shell script that runs ps every X secondsInstead of going wild about trying to patch a script, there’s a much, much simpler solution:top -b -d 10 -n 3 >> top-file
We have top running in batch mode (-b). It’s going to refresh every 10 seconds, as specified by the delay (-d) flag, for a total count of 3 iterations (-n). The output will be sent to a file.Here is a screenshots of outut:
You could schedule a job via cron.
You could run a shell script that runs ps every X seconds
Instead of going wild about trying to patch a script, there’s a much, much simpler solution:
top -b -d 10 -n 3 >> top-file

We have top running in batch mode (-b). It’s going to refresh every 10 seconds, as specified by the delay (-d) flag, for a total count of 3 iterations (-n). The output will be sent to a file.Here is a screenshots of outut:
Duplicate pipe content: ‘tee’ is a very useful utility that duplicates pipe content. Now, what makes tee really useful is that it can append data to existing files, making it ideal for writing periodic log information to multiple files at once.ps | tee file1 file2 file3
We’re sending the output of the ps command to three different files! Or as many as we want. As you can see in the screenshots below, all three files were created at the same time and they all contain the same data.
ps | tee file1 file2 file3

We’re sending the output of the ps command to three different files! Or as many as we want. As you can see in the screenshots below, all three files were created at the same time and they all contain the same data.
export: The ‘export’ command is one of the bash shell BUILTINS commands.It has three available command options. In general, it marks an environment variable to be exported with any newly forked child processes and thus it allows a child process to inherit all marked variables.Frequently Used Options with ‘export’-p : List of all names that are exported in the current shell-n: Remove names from export list-f : Names are exported as functionsExample :command without ‘export’:$ a = geeksforgeeks.org
$ echo $a
geeksforgeeks.org
$ bash
$ echo $a
From the above we can see that any new child process forked from a parent process by default does not inherit parent’s variables. This is where the export command comes handy.$ a = geeksforgeeks.org
$ echo $a
geeksforgeeks.org
$ export a
$ bash
$ echo $a
geeksforgeeks.org
$On line 3, we now have used the export command to make the variable “a” to be exported when a new child process is created. As a result the variable “a” still contains the string “geeksforgeeks.org” even after a new bash shell was created.
-p : List of all names that are exported in the current shell
-n: Remove names from export list
-f : Names are exported as functions
Example :
command without ‘export’:$ a = geeksforgeeks.org
$ echo $a
geeksforgeeks.org
$ bash
$ echo $a
From the above we can see that any new child process forked from a parent process by default does not inherit parent’s variables. This is where the export command comes handy.$ a = geeksforgeeks.org
$ echo $a
geeksforgeeks.org
$ export a
$ bash
$ echo $a
geeksforgeeks.org
$On line 3, we now have used the export command to make the variable “a” to be exported when a new child process is created. As a result the variable “a” still contains the string “geeksforgeeks.org” even after a new bash shell was created.
$ a = geeksforgeeks.org
$ echo $a
geeksforgeeks.org
$ bash
$ echo $a

From the above we can see that any new child process forked from a parent process by default does not inherit parent’s variables. This is where the export command comes handy.
$ a = geeksforgeeks.org
$ echo $a
geeksforgeeks.org
$ export a
$ bash
$ echo $a
geeksforgeeks.org
$
On line 3, we now have used the export command to make the variable “a” to be exported when a new child process is created. As a result the variable “a” still contains the string “geeksforgeeks.org” even after a new bash shell was created.
basename – Strips directory and suffix from filenames. basename prints NAME with any leading directory components removed. If Suffix is specified, it will also remove a trailing SUFFIX. For example: To get the name of the file present in test folder$ basename test/gfg.txt
gfg.txt
$ basename test/gfg.txt
gfg.txt
grep: grep searches files for a given character string or pattern and can replace the string with another. This is one method of searching for files within Linux.grep [option(s)] pattern [file(s)]Search number of files: grep can search any number of files simultaneously. Thus, for example, the following would search the three files file1, file2 and file3 for any line that contains the string GfGgrep GfG file1 file2 file3Search text in all files: To search all text files in the current directory (i.e., the directory in which the user is currently working), if there is a phrase “Linux is” grep 'Linux is' *There are lot of more options which can be tried using grep command.
grep [option(s)] pattern [file(s)]
Search number of files: grep can search any number of files simultaneously. Thus, for example, the following would search the three files file1, file2 and file3 for any line that contains the string GfGgrep GfG file1 file2 file3
grep GfG file1 file2 file3
Search text in all files: To search all text files in the current directory (i.e., the directory in which the user is currently working), if there is a phrase “Linux is” grep 'Linux is' *
 grep 'Linux is' *
There are lot of more options which can be tried using grep command.
This article is contributed by Shivam Pradhan (anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
manav014
linux-command
Linux-Unix
TechTips
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
TCP Server-Client implementation in C
tar command in Linux with examples
curl command in Linux with Examples
UDP Server-Client implementation in C
'crontab' in Linux with Examples
How to Find the Wi-Fi Password Using CMD in Windows?
How to Run a Python Script using Docker?
Docker - COPY Instruction
Running Python script on GPU.
Setting up the environment in Java