Open In App

Useful and time saving bash commands in Linux

Last Updated : 19 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

There are multiple helpful Bash scripts that will make your Linux terminal experience fast and seamless. In Linux, you can automate and execute numerous day-to-day tasks using plain commands — without laying hands on the GUI. These bash script examples assist you in automating file operations, list management, and even chaining commands, thereby saving time and effort.

1. 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 the 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.

2. Listing the directories according to LAST EDITED DATE:

Go to the directory in which you want to list the files according to the 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 lexicographically 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:

3. Listing the names of directories/files chronologically sorted:

Go to the directory in which you want to list the files according to the last edited date. In the terminal, write the following script:

ls --sort=time

Example: The same directory GeeksForGeeks_directory containing 4 directories in it named as test, test1, test2, test3 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:

sort-files-in-chronological-order

4. 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 the 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.

Conclusion

Whether you're counting files in a directory, sorting folders by date modified, or stringing several commands in a single line, these handy and time-saving bash scripts allow you complete access from the terminal.

Mastering simple Linux commands like ls | wc -w, ls -Rt, and ls | sort is not for power users alone — everyone can learn from using them. These are easy to learn and can be used in everyday applications such as directory management, structuring codebases, or tracking system modifications.


Next Article
Article Tags :
Practice Tags :

Similar Reads