10 interesting and extremely helpful Linux command line tricks




As you start spending more and more time working on Linux command line, you tend to learn some cool tricks that make your life easy and save you lot of time. I have been working on Linux command line for many years now and I have learned a lot of Linux command line tricks. Here in this article, I will discuss some Linux command line tricks that I find worth using in my day to day command line activities.

NOTE – All the examples in this article are tested on bash shell.


Linux command line tricks


1. How to switch between directories efficiently?

Working on Linux command line means switching between lot of directories. You are in a directory ‘A’, then you move to directory ‘B’. Now you want to come back to directory ‘A’. Typing the complete directory path for ‘A’ can be cumbersome sometimes. For this you can use ‘cd -’ short cut.
Here's an Example:
$ pwd
/home/himanshu
$ cd /usr/local/bin/
$ cd -
/home/himanshu
So we see that it’s easy to switch between two directories using cd- .
But, ‘cd -’ resolves only a partial problem. It can only switch you back to last directory only. What if you switch between multiple directories and then want to come back to the first or some other desired directory? I mean, suppose you are in a directory ‘A’, then you switch to directories ‘B’ -> ‘C’ -> ‘D’ -> ‘E’ and then you want to again go back to directory ‘A’.
Well, for this, you can use the combination of ‘pushd’ and ‘popd’.
Example:
$ pwd
/home/himanshu
$ pushd /home/himanshu
~ ~
$ cd /usr
$ cd /tmp
$ cd /proc
$ popd
~
$ pwd
/home/himanshu
As you can see, first you pass the desired directory (to which you want to come back eventually) as argument to ‘pushd’ and then through ‘popd’ you can actually trigger a directory switch to that directory from anywhere on the command prompt.

2. How to make efficient use of Linux command line history using !! and ! ?

Double exclamation ie ‘!!’ represents the last run command on the shell. Here is an example :
$ uname -a
Linux himanshu-Inspiron-1525 3.2.0-36-generic-pae #57-Ubuntu SMP Tue Jan 8 22:01:06 UTC 2013 i686 i686 i386 GNU/Linux
$ !!
uname -a
Linux himanshu-Inspiron-1525 3.2.0-36-generic-pae #57-Ubuntu SMP Tue Jan 8 22:01:06 UTC 2013 i686 i686 i386 GNU/Linux
So what best can we do with !! ?
Well, firstly, you can extend the command easily. Here is an example :
$ !! | grep Linux
uname -a | grep Linux
Linux himanshu-Inspiron-1525 3.2.0-36-generic-pae #57-Ubuntu SMP Tue Jan 8 22:01:06 UTC 2013 i686 i686 i386 GNU/Linux
Also, it so happens many times that you run a command and you get an error that the command requires root privileges. Then you press the ‘up arrow’ key + home key + write ‘sudo’ . Well all this can be avoided using !!.
Here is an example :
$ touch new_binary
touch: cannot touch `new_binary': Permission denied
$ sudo !!
sudo touch new_binary
[sudo] password for himanshu:
$ ls new_binary 
new_binary
Sometimes you would like to append a command to existing shell script or would like to create a new shell script, then you can use ‘!!’ to the task easily. Here is an example :
$ ls -lart /home/himanshu/practice/*.py
-rw-rw-r-- 1 himanshu himanshu 50 Mar  1 00:23 /home/himanshu/practice/firstPYProgram.py
$ echo !! > script.sh 
echo ls -lart /home/himanshu/practice/*.py > script.sh
$ cat script.sh 
ls -lart /home/himanshu/practice/firstPYProgram.py
So we see that this way !! proves to be easy and time saving.
Now, lets come to single exclamation ie ‘!’ . Unlike double exclamation ie ‘!!’, through single exclamation ‘!’, we can access any previously run command that exists in command line history. Here are some examples :
Use serial number from output of history command to run a particular command
$ history
...
...
...
2039  uname -a | grep Linux
 2040  dmesg
 2041  clear
 2042  cd bin
 2043  clear
 2044  pwd
 2045  touch new_binary
 2046  sudo touch new_binary
 2047  ls new_binary 
 2048  history
$ !2039
uname -a | grep Linux
Linux himanshu-Inspiron-1525 3.2.0-36-generic-pae #57-Ubuntu SMP Tue Jan 8 22:01:06 UTC 2013 i686 i686 i386 GNU/Linux
So we see that command number 2039 was run through single exclamation ‘!’ without having to type or copy paste the command again.
You can use negative integer values with ‘!’ to run second last command, third last command, fourth last command…and so on.
Here is an example :
 $history
...
...
...
 2049  ! 2039
 2050  uname -a | grep Linux
 2051  history
$ !-2
uname -a | grep Linux
Linux himanshu-Inspiron-1525 3.2.0-36-generic-pae #57-Ubuntu SMP Tue Jan 8 22:01:06 UTC 2013 i686 i686 i386 GNU/Linux
Run a new command with argument of previous command
Here is an example :
$ ls /home/himanshu/practice/*.py
/home/himanshu/practice/firstPYProgram.py

$ ls -lart !$
ls -lart /home/himanshu/practice/*.py
-rw-rw-r-- 1 himanshu himanshu 50 Mar  1 00:23 /home/himanshu/practice/firstPYProgram.py
So we see that ‘!$’ can be used to fetch argument from previous command and use it with the current command.
In case of two arguments, use carrot ‘!^’ to access first argument
Here is an example :
$ ls /home/himanshu/practice/*.py /home/himanshu/practice/*.txt
/home/himanshu/practice/file.txt           /home/himanshu/practice/output.txt  /home/himanshu/practice/sort.txt
/home/himanshu/practice/firstPYProgram.py  /home/himanshu/practice/sort1.txt   /home/himanshu/practice/test.txt
/home/himanshu/practice/input.txt          /home/himanshu/practice/sort2.txt
$ ls -lart !^
ls -lart /home/himanshu/practice/*.py
-rw-rw-r-- 1 himanshu himanshu 50 Mar  1 00:23 /home/himanshu/practice/firstPYProgram.py
So we see that through ‘!^’ we can access the first argument of the previous run command.
To access the any other argument (of previous run command) in current command, ‘![prev command name]:[argument number]‘ can be used.
Here is an example :
$ ls !ls:2
ls /home/himanshu/practice/*.txt
/home/himanshu/practice/file.txt    /home/himanshu/practice/sort1.txt  /home/himanshu/practice/test.txt
/home/himanshu/practice/input.txt   /home/himanshu/practice/sort2.txt
/home/himanshu/practice/output.txt  /home/himanshu/practice/sort.txt
So this way, the second argument (of the previous command) was accessed.
To access all the arguments of a previously run command, use ‘!*’
Here is an example :
$ ls -lart !*
ls -lart /home/himanshu/practice/*.py /home/himanshu/practice/*.txt
-r--r--r-- 1 himanshu himanshu 50 Oct 24  2012 /home/himanshu/practice/output.txt
-r--r--r-- 1 himanshu himanshu  7 Nov 10 13:46 /home/himanshu/practice/input.txt
-r--r--r-- 1 himanshu himanshu  8 Dec  7 20:38 /home/himanshu/practice/sort1.txt
-r--r--r-- 1 himanshu himanshu  8 Dec  7 20:39 /home/himanshu/practice/sort2.txt
-r--r--r-- 1 himanshu himanshu 14 Dec 14 20:45 /home/himanshu/practice/file.txt
-r--r--r-- 1 himanshu himanshu 41 Jan 23 20:42 /home/himanshu/practice/sort.txt
-rw-rw-r-- 1 himanshu himanshu 50 Mar  1 00:23 /home/himanshu/practice/firstPYProgram.py
-rw-rw-r-- 1 himanshu himanshu  0 Mar 10 15:31 /home/himanshu/practice/test.txt
Use ‘![keyword]‘ to run the last command starting with [keyword]
Here is an example :
$ !ls
ls -lart /home/himanshu/practice/*.py
-rw-rw-r-- 1 himanshu himanshu 50 Mar  1 00:23 /home/himanshu/practice/firstPYProgram.py
So we see that the last ls command was executed. This way you can just write the first keyword of the command (which is command name usually) and you do not need to write the complete command. Single exclamation ‘!’ will do it for you.

3. Use comma ‘,’ operator wherever it makes life easy

As already said, the comma operator can make life easy for you on Linux command line. Here are some examples :
Convert to lower case
Comma operator can be used to convert the whole string or only the first letter to lower case.
Here is an example :
$ words="Example of comma OPERATOR"
$ echo ${words,}
example of comma OPERATOR
${words,,}
example of comma operator
So we see that through a single comma only the first letter was converted to lower case while through double comma, complete string was converted to lower case.
Use comma with file names
Comma operator can be used with file names. A couple of examples are shown below.
$ touch new_file{1,2,3}
himanshu@himanshu-Inspiron-1525:~/practice$ ls new_file*
new_file1  new_file2  new_file3
So we see that comma operator helped in creating three files easily.
Similarly, for one of the very popular work people do on command line is to rename files by adding .old or .new temporarily. You can save the time by doing something like :
mv my_filename.{old,new}
This will rename my_filename.old to myfilename.new.

4. How to delete files with leading or trailing spaces?

You might find yourself struggling with deleting files with leading or trailing spaces through ‘rm’ command on Linux command line.
For example :
$ rm tempFile
rm: cannot remove `tempFile': No such file or directory
So we see that rm command says that this file does not exist. But you are pretty confident that file with such name exists. Then the only thing could be that this file name would be having leading or trailing spaces.
You can use double quotes to avoid this problem :
$ rm "tempFile "
The above command worked in my case.
Note that if you do not want to use double quotes then ‘\ ‘ can be used. Here is an example :
$ rm tempFile\
Remember to add a space after back slash.

5. How to delete files with names beginning with hyphen (-) ?

Sometimes you may find yourself stuck with a situation like this :
You have to delete a file named -1mpFile.out
$ ls
-1mpFile.out                          CPPfile.o             libCfile.so      mylinuxbook_new  prog          split
But, when you try using rm command, following error is produced :
$ rm -1mpFile.out
rm: invalid option -- '1'
Try `rm ./-1mpFile.out' to remove the file `-1mpFile.out'.
Try `rm --help' for more information.
Even if you use double quotes, you get the following error :
$ rm "-1mpFile.out"
rm: invalid option -- '1'
Try `rm ./-1mpFile.out' to remove the file `-1mpFile.out'.
Try `rm --help' for more information.
So, rm command considers the hyphen ‘-’ as an indicator that some command line option will follow and so it treats ’1mpFile.out’ as an option. Hence the error.
Now, to tell ‘rm’ that the word beginning with hyphen is file name, you need to pass double hyphen (–) first. Here is an example :
$ rm -- -1mpFile.out
So this should remove the file successfully.
Since this problem is generic ie you will observe this problem even while creating this file using ‘touch’ command etc. Double hyphen can be used with other commands too for the same purpose.
Here is another example of double hyphen but this time with touch and ls commands :
$ touch -1mpFile.out
touch: invalid option -- '1'
Try `touch --help' for more information.
$ touch -- -1mpFile.out
$ ls — -1mpFile.out -1mpFile.out
So we can safely use double hyphen (–) in a generic sense with different Linux commands.

6. How to delete all files in a directory except some (with particular extensions) ?

Suppose you have a directory with lot of files and you want to delete all the files except some of them (with particular file extensions). This can be done in following way :
Here is a directory containing lot of files :
$ ls
a.out         Cfile.c  file.c             macro.c     my_printf.c   orig_file.orig  stacksmash.c
bfrovrflw.c   cmd.c    firstPYProgram.py  main.c      new_printf.c  orig_file.rej   test_strace.c
bufrovrflw.c  env.c    helloworld.c       my_fopen.c  new.txt       prog.c          virtual_func.c
Now, you want to delete all the files except .c and .py files.
Here is what you can do :
$ rm !(*.c|*.py)

$ ls
bfrovrflw.c   Cfile.c  env.c   firstPYProgram.py  macro.c  my_fopen.c   new_printf.c  stacksmash.c   virtual_func.c
bufrovrflw.c  cmd.c    file.c  helloworld.c       main.c   my_printf.c  prog.c        test_strace.c
So you can see that files with all other extensions got deleted.

7. How to create customized backup using touch and find commands?

Touch command in association with find command can be used to create customized backups.
Suppose you want to create a backup of files that you created or changed in a directory between 9am and 5pm. For this, the very first step is to create two files temp1 and temp2 with timestamps as 9am and 5pm respectively.
$ touch -d "9am" temp1
$ touch -d "5pm" temp2
These commands will create two files temp1 and temp2 with access and modification timestamps as 9am and 5pm respectively.
Let’s cross check these by using stat command:
$ stat temp1
 File: `temp1'
 Size: 0 Blocks: 0 IO Block: 4096 regular empty file
 Device: 806h/2054d Inode: 528534 Links: 1
 Access: (0664/-rw-rw-r--) Uid: ( 1000/himanshu) Gid: ( 1000/himanshu)
 Access: 2013-04-28 09:00:00.000000000 +0530
 Modify: 2013-04-28 09:00:00.000000000 +0530
 Change: 2013-04-28 19:06:05.982909491 +0530
 Birth: -
$ stat temp2
 File: `temp2'
 Size: 0 Blocks: 0 IO Block: 4096 regular empty file
 Device: 806h/2054d Inode: 529476 Links: 1
 Access: (0664/-rw-rw-r--) Uid: ( 1000/himanshu) Gid: ( 1000/himanshu)
 Access: 2013-04-28 17:00:00.000000000 +0530
 Modify: 2013-04-28 17:00:00.000000000 +0530
 Change: 2013-04-28 19:06:12.090939793 +0530
 Birth: -
So we see that timestamps was as expected. Now move to the directory where you want to create the backup of files. Here are the contents of the directory in my case :
$ ls
bfrovrflw.c   Cfile.c  env.c   firstPYProgram.py  macro.c  my_fopen.c   new_printf.c  stacksmash.c   virtual_func.c
bufrovrflw.c  cmd.c    file.c  helloworld.c       main.c   my_printf.c  prog.c        test_strace.c
Now, I create a directory named ‘bkup’ and run the following command :
$ find . -newer ../temp1 ! -newer ../temp2 -exec cp '{}' ./bkup/ ';'
The -newer and ! -newer options in command above will first find all the files with modification time between 9am and 5pm. Then the -exec option makes sure that the cp command is run for every result (‘{}’) of find command and the file is copied to ./bkup/ folder. The terminating ‘;‘ is the indication that cp command terminates here.
Now, if you see the ‘bkup’ directroy, you’ll find all the backed up files there. Here is what I saw in my case :
$ cd bkup/
$ ls
bfrovrflw.c   Cfile.c  env.c   firstPYProgram.py  macro.c  my_fopen.c   new_printf.c  stacksmash.c   virtual_func.c
bufrovrflw.c  cmd.c    file.c  helloworld.c       main.c   my_printf.c  prog.c        test_strace.c
As all the files were created between 9am and 5pm so all of them were backed up.

8. Why rm command fails with error ‘Argument list too long’?

This usually happens when you have a directory containing huge number of files. When you do a rm -rf over it, you get something like :
-bash: /bin/rm: Argument list too long
This issue can be resolved using following command (please switch over to the desired directory before running this command):
find * -xdev -exec rm -f '{}' ';'
The find command above will supply input to rm command in batches that it can process. This is one of the fastest method to delete files.

9. How to search for all the files in a directory containing a particular string?

This can be easily achieved using grep command.
Here are a couple of examples :
$ grep -l "printf" *.c
bfrovrflw.c
bufrovrflw.c
Cfile.c
cmd.c
env.c
file.c
helloworld.c
macro.c
main.c
my_fopen.c
my_printf.c
new_printf.c
prog.c
stacksmash.c
test_strace.c
$ grep -l "buff" *.c
bfrovrflw.c
If it is desired to view the lines where the string is used in the file, then ‘find’ command can be used with ‘xargs’ and ‘grep’ command in the following way :
$ find ./ -name "*.c" | xargs grep "buff"
./bfrovrflw.c:    char buff[15];
./bfrovrflw.c:    gets(buff);
./bfrovrflw.c:    if(strcmp(buff, "MyLinuxBook"))
So we see that even the lines containing the string “buff” were displayed in the output.

10. How to Empty a file using ‘>’ operator ?

Suppose you want to empty a file from command line.
Here is how easily you can do it :
$ > [complete file path]
For example :
$ > ./logfile
This will delete all the contents of the file ‘logfile’ and empty it.

SOURCE:Linuxbook

0 comments: