Linux

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 is an example :
$ pwd
/home/username
$ cd /usr/local/bin/
$ cd –
/home/username
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’.

Here is an example :

$ pwd
/home/username
$ pushd /home/username
~ ~
$ cd /usr
$ cd /tmp
$ cd /proc
$ popd
~
$ pwd
/home/username
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 username-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 username-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 username-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 username:
$ 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/username/practice/*.py
-rw-rw-r– 1 username username 50 Mar 1 00:23 /home/username/practice/firstPYProgram.py
$ echo !! > script.sh
echo ls -lart /home/username/practice/*.py > script.sh
$ cat script.sh
ls -lart /home/username/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 username-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 username-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/username/practice/*.py
/home/username/practice/firstPYProgram.py

$ ls -lart !$
ls -lart /home/username/practice/*.py
-rw-rw-r– 1 username username 50 Mar 1 00:23 /home/username/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/username/practice/*.py /home/username/practice/*.txt
/home/username/practice/file.txt /home/username/practice/output.txt /home/username/practice/sort.txt
/home/username/practice/firstPYProgram.py /home/username/practice/sort1.txt /home/username/practice/test.txt
/home/username/practice/input.txt /home/username/practice/sort2.txt
$ ls -lart !^
ls -lart /home/username/practice/*.py
-rw-rw-r– 1 username username 50 Mar 1 00:23 /home/username/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/username/practice/*.txt
/home/username/practice/file.txt /home/username/practice/sort1.txt /home/username/practice/test.txt
/home/username/practice/input.txt /home/username/practice/sort2.txt
/home/username/practice/output.txt /home/username/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/username/practice/*.py /home/username/practice/*.txt
-r–r–r– 1 username username 50 Oct 24 2012 /home/username/practice/output.txt
-r–r–r– 1 username username 7 Nov 10 13:46 /home/username/practice/input.txt
-r–r–r– 1 username username 8 Dec 7 20:38 /home/username/practice/sort1.txt
-r–r–r– 1 username username 8 Dec 7 20:39 /home/username/practice/sort2.txt
-r–r–r– 1 username username 14 Dec 14 20:45 /home/username/practice/file.txt
-r–r–r– 1 username username 41 Jan 23 20:42 /home/username/practice/sort.txt
-rw-rw-r– 1 username username 50 Mar 1 00:23 /home/username/practice/firstPYProgram.py
-rw-rw-r– 1 username username 0 Mar 10 15:31 /home/username/practice/test.txt
Use ‘![keyword]‘ to run the last command starting with [keyword]

Here is an example :

$ !ls
ls -lart /home/username/practice/*.py
-rw-rw-r– 1 username username 50 Mar 1 00:23 /home/username/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}
username@username-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/username) Gid: ( 1000/username)
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/username) Gid: ( 1000/username)
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.

11. How to search man pages for a particular string?

You might have used Linux man pages to understand more about a command, function etc. But, what if you want to know which man pages discuss about a particular topic. For example, what if I want to know that which man pages discuss about ‘login’?

Well, there exists an option -k through which you can do this. Here is an example :

$ man -k login
access.conf (5) – the login access control table file
add-shell (8) – add shells to the list of valid login shells
chsh (1) – change login shell
faillog (5) – login failure logging file
faillog (8) – display faillog records or set login failure limits
getlogin (3) – get username
getlogin_r (3) – get username
gnome-session-properties (1) – Configure applications to start on login
hotot (7) – lightweight & opensource microbloging client
issue (5) – prelogin message and identification file
lastlog (8) – reports the most recent login of all users or of a given user
login (1) – begin session on the system
login (3) – write utmp and wtmp entries
login.defs (5) – shadow password suite configuration
login_tty (3) – tty utility functions
logname (1) – print user’s login name



So we see that all the man pages that discuss about ‘login’ were displayed in the output.

12. How to redirect stderr output messages to a file?

It so happens mostly that standard commands/programs/services stream normal log messages to stdout while error log messages to stderr stream. Now, if you just do something like :

$ [some-command] > logfile
Then only the messages that were directed to stdout would be redirected to the file ‘logfile’ but no message that was directed to stderr would be redirected to the file.

Here is an example :

$ touch new > /home/username/practice/logfile
touch: cannot touch `new’: Permission denied
$ cat /home/username/practice/logfile
$
So we see that error was not redirected to the log file.

Now, to correct this, do something like :

$ touch new > /home/username/practice/logfile 2>&1
$ cat /home/username/practice/logfile
touch: cannot touch `new’: Permission denied
So we see that this time the error was redirected to the file successfully. Please note that 2>&1 combines both stdout and stderr streams to stdout stream only.

13. How to follow multiple log files on the go?

If it is required to follow multiple log files as they are being updated then this can be done through tail command.

Suppose I want to monitor two log files ‘logfile’ and ‘logfile1′ simultaneously then I will use the tail command as follows :

$ tail -f logfile logfile1
==> logfile <==

==> logfile1 <==

==> logfile <==
hi

==> logfile1 <==
hello
So you can see that dynamic updates to these log files can easily be monitored through tail command.

14. How to make a command not to show up in the output of ‘history’ command?

Well, sometimes you would want to run a command but do not want it to appear in the output of Linux historycommand.

You can achieve this by inserting a space before you type the command on prompt.

Here is an example :

$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda6 29640780 6174904 21960188 22% /
udev 1536752 4 1536748 1% /dev
tmpfs 617620 892 616728 1% /run
none 5120 0 5120 0% /run/lock
none 1544040 156 1543884 1% /run/shm
Note that there is a space between ‘$’ and ‘df’.

Now, let’s confirm whether this command appears in the output of ‘history’ :

$ history | grep df
1633 ls *.pdf
1634 mv LinuxCommandsPart1.pdf LinuxCommandsPart1
2245 history | grep df
The df command was not captured in the output of history command.

15. How to simulate on-screen typing just like you see in movies?

Well, to simulate typing just like you see in movies, use ‘pv’ command.

Try this out :

echo “You can simulate on-screen typing just like in the movies” | pv -qL 10
Check the output for yourself. 🙂

16. How to escape the aliased version of a command?

The alias command, as we all know is used to create aliases of the commands that act as short cuts and save time.

For example, I have created alias of ls command such that whenever I execute ls, its ‘ls -lart’ that gets executed.

$ alias ls=’ls -lart’
Now, if I ever intend to escape the alias and want to execute only ls, then I can do this by beginning the command with a backslash ie ‘\’.

Here is an example :

$ \ls
1 CPPfile.o libCPPfile.so mylinuxbook_new prog.c stacksmash.c
So we see that the original ls command was executed.

NOTE – If you want to suppress an alias for a whole login session, you can use ‘unalias’ command for that.

17. How to get rid of that unknown process that forbids you to delete a file?

There are situations where you want to delete a file but you get an error like ‘file is already in use’. You try to find which process in using this file but all your effort goes in vain. What would you do in this case?

Well, you can use the ‘fuser’ command. It tell you the process that is using a particular file. You can use ‘fuser -k [filename]‘ command to kill that process.

Read our article on fuser command to know more about it.

18. How to monitor and redirect logs to file simultaneously?

If you want to redirect logs to a file but also want to monitor them in parallel, you can use the Linux ‘tee’ command.

Here is an example :

./program | tee logfile
The command above will execute the ‘program’. All the logs will be redirected to ‘logfile’ while you can simultaneously view them on stdout (command line in most of the cases) also.

19. How to check if a command succeeded or failed?

Well this can be done through ‘$?‘ environment variable. It holds 0 if last run command was success else it holds a non zero value in case of failure.

Here is an example :

$ touch abc
touch: cannot touch `abc’: Permission denied
$ echo $?
1
As the last command failed, so the output was ’1′.

20. How to copy paste on command line through keyboard?

You can copy the text by selecting it first and then by pressing :

CTRL + SHIFT + C
and paste it using :

CTRL + SHIFT + V
________________________________________________________________________________________________________________________________

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s