Simple useful Bash shell commands

22 Apr 2020

This article is more like a self note for me to list down the basic useful shell commands, which I have used recentlyg

1. ls -lah

This is one of the most used commands in the shell. However if you want the file size printed by ls command in a human readable format, add the option -h to it. Then it will print the file sizes in formats such as Mb, Gb which is easier to read.

2. watch -d -n <time_in_seconds> <command>

watch command helps to repeatedly execute a particular command with a specific interval. This is useful in cases such as monitoring the contents of a folder or checking whether the output of a command is changing periodically. The -i option helps to specify the interval in terms of seconds and -d option will highlight the change in the output if it changes.

3. tail -f <filename>

tail command helps to print the trailing content of a file continuously on to the console. This is helpful in cases where you are monitoring a log file for a particular scenario.

4. cat <filename> | less

The reason why simple cat command is present here is because many a times, I have seen people using vi command to analyze the contents of a file. And often they will accidentally modify the contents of the file, by inadvertently switching between command mode and editor mode. We can avoid this by using cat command and piping its output to less. This will output the file content according to the screen size and you can navigate the content just like you do using a vi editor. If you want to search a string, just use the / option, exactly as in vi. The advantage here is that you can avoid accidentally modifying the file content.

5. tree

tree command helps you to print the contents of a folder like a directory tree structure. This is helpful in identifying the structure of an interesting folder.