Useful and time saving bash commands in Linux
There are many useful bash scripts which can make things easier. Many things can be done just using the terminal. Here is a list of some of such useful bash scripts through which we can do things which cannot be done manually.
-
Counting the number of files(or directory) in a given directory : Go to the directory in which
you want to count the number of files(or directory) using terminal .In the terminal, write the
following script:ls | wc -w
Example: The image below shows a directory named GeeksForGeeks_directory containing 4
directories in it named as C, C++, Java, Python.

Now, open the terminal and go to the directory GeeksForGeeks_directory and type the above
command and see the output. It will show 4, as there are 4 directories.
-
Listing the directories according to LAST EDITED DATE : Go to the directory in which
you want to list the files according to last edited date .In the terminal, write the
following script:ls -Rt
Example: The same directory GeeksForGeeks_directory containing 4
directories in it named as C, C++, Java, Python is used here .Now, open the terminal and go to the directory GeeksForGeeks_directory and type the above
command and see the output. It will show the output as shown below:
-
Listing the names of directories/files lexcographically sorted : Go to the directory in which
you want to list the files according to last edited date .In the terminal, write the
following script:ls | sort
Example: The same directory GeeksForGeeks_directory containing 4
directories in it named as C, C++, Java, Python is used here .Now, open the terminal and go to the directory GeeksForGeeks_directory and type the above
command and see the output. It will show the output as shown below:
-
Running multiple commands in just one line script: Open the terminal and write the
following script:command1 ; command2;
Note: Any number of commands can be written.
Example: The same directory GeeksForGeeks_directory containing 4
directories in it named as C, C++, Java, Python is used here . Let us write a command which shows the
contents of a directory and moves to previous directory after showing the contents.Now, open the terminal and go to the directory GeeksForGeeks_directory and type the following
command and see the output.ls ; cd ..
This will show the contents in the GeeksForGeeks_directory and switch to the previous directory
(Here, the previous directory is Desktop).
It will show the output as shown below: The current directory is changed from GeeksForGeeks_directory to Desktop.

Note : What if the previous command i.e. command1 was not successful. It will not be executed but the remaining command will be executed. If one wants to run further commands only if all the previous commands run successfully, instead of using ; , one can use && operator for that purpose.
The image below will show how this works:

Note: Here, as lq is not a valid command, so && separator does not execute any command further to it.
Reference: Linux Man-pages
Recommended Posts:
- Linux Commands
- Fun Commands in Linux
- Bash Script to get Low Battery Alert in Linux
- Basic Shell Commands in Linux
- Essential Linux/Unix Commands
- Internal and External Commands in Linux
- Basic Linux Commands for day to day life
- Custom commands for linux terminal
- Daily life Linux Commands
- Python | Execute and parse Linux commands
- halt, poweroff and reboot Commands in Linux
- Network configuration and trouble shooting commands in Linux
- Important Linux Commands (leave, diff, cal, ncal, locate and ln)
- time command in Linux with examples
- How to find time taken by a command/program on Linux Shell?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



