There are many useful bash scripts that 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 that cannot be done manually.
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:

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.
Recommended Posts:
- Some useful Linux Hacks
- Network configuration and trouble shooting commands in Linux
- Important Linux Commands (leave, diff, cal, ncal, locate and ln)
- halt, poweroff and reboot Commands in Linux
- Python | Execute and parse Linux commands
- Daily life Linux Commands
- Basic Linux Commands for day to day life
- Essential Linux/Unix Commands
- Custom commands for linux terminal
- Fun Commands in Linux
- Basic Shell Commands in Linux
- Linux Commands
- How to Access All Users in Linux Using Different Commands?
- How to Build Your Own Commands in Linux?
- dos2unix and unix2dos commands
- Commands in Unix when things go wrong
- Run commands as root with sudo
- time command in Linux with examples
- Reset a lost Linux administrative password and Explanation
- mindepth and maxdepth in Linux find() command for limiting search to a specific directory.
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.
Improved By : manav014

