Open In App

Daily life Linux Commands

  1. 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’.
  2. 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 -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.

  3. 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
  4. 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
  5. 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 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
  6. 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 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:

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

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

    Example :

    • command without ‘export’:
      $ a =
      $ echo $a
      $ 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 =
      $ echo $a
      $ export a
      $ bash
      $ echo $a

      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 “” even after a new bash shell was created.

  9. 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
  10. 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 GfG
        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' *

    There are lot of more options which can be tried using grep command.

Article Tags :