15 Cool Bash Tricks


If you use linux you must have updated your profile/environment to make your life easier. We have listed some basic tricks that might be useful to remember.

1. Anti-Aliasing the Commands When Running Them

A few command aliases are by default defined and saved in bash startup script and/or user login script so as to make relevant command listings more legible.

For example, by default the output listing of the command ls is displayed in “color mode”. To get the output listing of the command ls in “black-and-white mode”, run:

$ \ls

 

Another example: When we run the command vi, vim is invoked on some systems due to the existing alias definition. So, in such a situation, if we want to run vanilla vi, we should run:

$ \vi

 


2. Keyboard Shortcut to Clear Terminal Screen

POSIX-compliant command to clear terminal screen is tput clear. A shorter command is clear. An even smarter method is the key combination:

Ctrl + L

 


3. Keyboard Shortcut to Move Insertion Point to the Beginning of Line

Suppose we have just listed a file with ls command and now we want to cat that file. Or we want to add sudo to the previously run command. Click Up Arrow key to get the previous command line ready to be edited and run the next time. To move the insertion point to the beginning of the command line being edited, enter the key combination:

Ctrl + A

 


4. To Capitalize the Letter under Insertion Point in Command Line

When the insertion point is on the letter to be capitalized, use the key combination:

Alt + C

It also moves the insertion point to the end of the word (the character position just next to the last character in the word) whose letter has been capitalized.

 


5. Use CDPATH to Set Base Directories for Quick Switching

For example, consider you do a lot of cd into the subdirectories under a few directories.

In order to avert typing or pasting long directory paths every now and then while working with disparate directories, set CDPATH to these base directories and access their subdirectories as if they were all under some virtual working directory:

$ CDPATH='/home/dan/jdk1.8.0_201'

To navigate to ‘/home/dan/jdk1.8.0_201/bin’, run:

$ cd bin # you don't have to provide the full path

 


6. To Use dir to List Files

$ alias dir='ls' # for black-and-white output listing

$ alias dir='ls --color=auto' # for colored output listing

In place of dir here, any other word is also good to go with if that is neither an existing alias name nor an existing command. On the other hand, any other command can also be aliased like ls here.

 


7. To List Select Files Having a Common File Extension within a Directory

$ ls {aa,b1,c2,ad}.sh

When submitted to Bash, this command is evaluated to:

$ ls aa.sh b1.sh c2.sh ad.sh

 


8. To Discard the Output Listing of a Command and Capture Only Its Error Listing

$ find -R / > /dev/null 2> err.log

Here /dev/null is a special device file which and does not save or direct to any physical device the data stream received by it. So, directing the output stream of a command to /dev/null, we are not bound to fill the disk space up by writing the listing to some temporary file. We avert seeing the listing in standard output as well as we save on disk operations and disk space.

 


9. To Compare a Pair of Files with Matching Names but with Different Extensions

$ diff prog1.c{-bak,}

This expands to:

$ diff prog1.c-bak prog1.c

This trick is very useful and is frequently adopted when we are working with file backups or source code repositories.

 


10. To Replace Only the Command and Run the Newer Command

To run another command with the same set of arguments as those of the previous command, use ^prevcommand^nextcommand. For example, to first cat files a1 and b1 and then ls the files, run these commands in succession:

cat a1 b1

^cat^ls

The command ^cat^ls here evaluates to-

ls a1 b1

This trick is very useful, almost a life saver, when you write such a script wherein first you check whether a particular file exists and then read or execute that file if it does exist.

 


11. Cleaner Approach to Command Substitution

Use $() to perform command substitution:

$(command)

$() is a far cleaner approach as compared to backquoting with . See the example below:

#using ` pair, \` pair and \\\` pair for escaping

echo `echo \`echo \\\`echo inside\\\`\“

#using $( and ) for nesting

echo $(echo $(echo $(echo inside)))

 


12. To Treat the Output of a Command as a File, Use <()

The output from <() can be consumed by a command where a file is expected. For example:  The command diff takes two files as arguments. We can supply the output listings of two ls command executions to diff to compare:

$ diff <(ls java7/bin) <(ls java8/bin)

 


13. Run Second Command Only If the First Command Succeeds

Such conditionals are very helpful to write decision points in scripts based on the outcomes of finished commands. Examples include:

To change to a directory only if the directory exists and avoid No such file or directory error, run:

$ mkdir /tmp/abc1 && cd /tmp/abc1

To cat a file only if the file exists and avoid No such file or directory error, run:

$ [ -f filename ] && cat filename

The command above first checks (using []) whether the file filename exists and displays its contents if it does exist.

 


14. To Watch Logs While They are Being Generated

If we want to have a glance at the last few lines of a log file while new logs are being written to it, we may run the following command:

$ tail -f /var/log/messages