Useful and time saving bash commands in Linux

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:

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.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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

Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.