20 interesting and extremely helpful Linux command line tricks

By | 29/04/2013

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.

NOTEAll 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 is 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’.

Here is an 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.

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/himanshu/practice/logfile 
touch: cannot touch `new': Permission denied
$ cat /home/himanshu/practice/logfile 
$

So we see that error was not redirected to the log file.

Now, to correct this, do something like :

$ touch new > /home/himanshu/practice/logfile 2>&1
$ cat /home/himanshu/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 history command.

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

 

Well, that was all from my side. I would really appreciate if you can drop in your comments related to these tricks. Also, it would be great if you can provide any useful Linux command line trick that is not listed here.

WANT TO READ MORE? Read best of MyLinuxBook on FunnyLinuxCommands, Linux netcat command, Bash scripting Part-I, Bash scripting Part-II and Linux strace command.

Category: CommandLineTips Linux administration

About Himanshu Arora

Himanshu Arora is a software programmer, open source enthusiast and Linux researcher. He writes technical articles for various websites and blogs. Some of his articles have been featured on IBM developerworks, ComputerWorld and in Linux Journal. He is the administrator of the blog and also contributes useful posts on the blog. Visit his google+ profile or mail him at himanshuz.chd[at]MAILSERVER[dot]com (where MAILSERVER=gmail)

25 thoughts on “20 interesting and extremely helpful Linux command line tricks

  1. uGa1aetu

    Number 19 mentions the ‘&?‘ variable. Of course this should be ‘$?‘

    Reply
  2. chris

    About pushd/popd: if you’re already in the directory you want to come back to, then

    pushd .

    is even shorter/faster.

    Reply
    1. CheShA

      You’re both doing it wrong; pushd saves the current working dir onto the directory stack, and changes to a new directory:

      $ pwd
      /home/chesha
      $ pushd /etc
      $ pwd
      /etc
      $ pushd /usr/bin
      $ pwd
      /usr/bin
      $ popd
      $ pwd
      /etc
      $ popd
      $ pwd
      /home/chesha

      Reply
  3. BAReFOOt

    The folowing command in number 6 obviously can’t work:

    $ rm !(*.c|*.py)

    ! invokes the history, and () would invoke subshell, running *.c and piping it to *.py, if possible

    Reply
    1. cal

      I don’t know if/how it works in other shells but this is from the bash man page:

      If the extglob shell option is enabled using the shopt builtin, several extended pattern matching operators are recognized.
      .
      .
      .
      !(pattern-list)
      Matches anything except one of the given patterns

      Reply
  4. BAReFOOt

    grep -R
    is much better, since it’s recursive too!

    Reply
  5. Alexander

    Nice compilation of useful tricks. Thanks for that – had something new for me as well, especially all those ! things.

    Some nitpicks…

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

    Another trick: prepend a “./” for the current directory to the filename. Ie. instead of

    rm -1mpFile.out

    do:

    rm ./-1mpFile.out

    Advantage: Works everywhere. Not just in GNU userland.

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

    find * -xdev -exec rm -f ‘{}’ ‘;’

    Now, that’s a pet peeve of mine ;) You used a “;” at the end. This tells find to invoke “rm -f” MANY MANY times, each time with just one argument.

    Better:

    find * -xdev -exec rm -f ‘{}’ +

    The “+” tells find to invoke “rm -f” with as many arguments as possible. So rm would only be called two or three times, or so. This makes a noticeable difference.

    However, I doubt that “find * -xdev -exec rm -f ‘{}’ ‘;'” would work in the first place. If “rm -f *” doesn’t work because of “Argument list too long”, then it will also be too long for “find”. And if there are directories, you’ll get loads of errors, because “rm -f” cannot delete a directory.

    Better and actually working:

    find . -xdev -exec rm -f ‘{}’ +

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

    $ > [complete file path]

    This only works on bash. It does not work on zsh.

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

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

    Well… Depends on how the bash HISTCONTROL variable is set. If it’s set to include ignorespace or ignoreboth, then this works. Else, it doesn’t.

    Other than those small (besides the find thing) nitpicks – good list :)

    Reply
    1. Alexander

      Rob,

      Actually, a lot of those tips are Linux specific, since they either are bash specific or depend on GNUisms.
      Now, while it is in theory true that you might be able to install GNU userland everywhere, more often than not you find that you cannot do so. And even if one would be able to install GNU tools, then those tips still would not be POSIX compliant and thus not real Unix tips.

      Having said that: It is still a nice collection of Linux tips.

      Reply
  6. dru8274

    Good post. I would also add that…

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

    The command “man -k keyword” lists manpages where the keyword is found in the manpage’s short description. But if you have something truly obscure to find, the -K option will search the entire body of all manpages, looking for manpages that mention the keyword – “man -K keyword”

    14. Make a command not to show up in the ‘history’?

    You can also use the HISTIGNORE environment var to set some patterns for commands to be ignored by the history.

    Reply
  7. Nishant

    Hey Himanshu,
    Some pretty epic command usage here. Thanks for the post, bookmarked!

    Reply
  8. Pingback: 20 interesting and extremely helpful Linux command line tricks

  9. JoeSmo

    I would recommend NOT using the advanced command history stuff. Granted, I use the bash shell a lot, and I use the up arrow and down arrow to view my prior commands… no biggie. What I find too scary is to repeat previous commands ( !! | ) automatically. For example, if I am running the command ” rm -rf * ” and then later I would do one of those sexy run a prior command, I might just re-run the “rm -rf * ” command again before I catch the mistake. Too much automation is a bad thing.

    Reply
    1. Himanshu Post author

      This might very well happen specially if you are doing multiple tasks in parallel. There are chances that you forget last run command and use !! for a wrong command. :-)

      Reply
  10. susenj

    Why History command?

    Just do a recursive search using “ctrl+r” , type any “combination of letters” you remember from that command which you ran in past, it will show up.

    Reply
  11. Veijo Ryhänen

    Hello,

    thank You for using Your time to admin this interesting and useful website!

    I would like to watch these useful command line tricks once at a time every time when I start a new gnome-console, instead of this default, which is defined in my ~/.bashrc:

    “fortune | cowsay -f $(ls /usr/share/cowsay/cows/ | shuf -n1)”

    ~$ fortune -f
    100.00% /usr/share/games/fortunes
    2.79% wisdom
    2.21% linux
    1.35% law
    0.97% sports
    7.92% definitions
    0.54% tao
    1.06% ethnic
    4.15% work
    1.49% startrek
    1.30% food
    1.87% disclaimer
    3.83% men-women
    3.29% platitudes
    0.35% news
    1.34% education
    1.30% humorists
    1.37% drugs
    4.29% miscellaneous
    0.34% pets
    0.99% love
    0.20% magic
    0.99% kids
    0.47% paradoxum
    1.80% perl
    0.68% linuxcookie
    6.80% computers
    4.74% songs-poems
    8.19% people
    3.56% knghtbrd
    7.46% cookie
    0.49% medicine
    2.84% fortunes
    0.36% goedel
    0.84% riddles
    4.12% science
    0.56% debian
    0.07% ascii-art
    4.62% politics
    3.61% zippy
    3.06% art
    0.08% translate-me
    1.73% literature

    So, is it possible to get a fortune compatible file which name is “20-interesting-and-extremely-helpful-linux-command-line-tricks” and install it to to /usr/share/games/fortunes -directory ? And, after that I would like to change my .bashrc

    fortune 20-interesting-and-extremely-helpful-linux-command-line-tricks

    Sincerely,
    Veijo Ryhänen, Finland

    Reply
  12. saravanan.k

    Really useful commands. I really enjoyed those commands.

    Reply
  13. tudorbin

    Well, you can use Long Path Tool for such issues………

    Reply
  14. Abdul

    Hi Himanshu,

    Is there any way/command to check the history of .sh files executed manually using ./xxxx.sh file. Some from our team has execueted a shell script file in linux box which is not allowed.

    TIA.
    Abdul

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *