Useful and time saving bash commands in Linux
Last Updated :
19 Apr, 2025
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:

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.
Similar Reads
Basic Shell Commands in Linux: Complete List Anyone using Linux should become an expert in the essential shell commands, as they form the backbone of working with the Linux terminal. These commands enable you to navigate the system, manage files, handle processes, and configure settings effectively.The Linux shell serves as an interface for us
5 min read
Top Linux Command Line Tips and Tricks Linux is a great OS that offers multiple options for simplifying tasks. It has everything you need for working more efficiently, whether a command-line or a GUI. However, many people don't know the Linux command line's short tricks and end up being stuck in complex tasks. An interface (text-based) t
5 min read
Basic CentOS Linux Commands in linux CentOS is a free and open-source operating system that aims to provide a stable reliable, and community-supported platform for servers and other enterprise applications. In this article, we will be covering CentOS Linux basics commands and functions of CentOS and also we will look into the advanced
4 min read
Linux System Monitoring Commands and Tools Monitoring the performance and health of a Linux system is essential for maintaining its stability and optimizing resource utilization. Linux provides a variety of commands and tools to facilitate system monitoring, enabling administrators to track key metrics, identify potential issues, and ensure
6 min read
List of Useful Git Commands Git is a version control system that helps programmers and developers manage and track changes to their code at any time. It allows multiple people to work on a project simultaneously and also keeps track of who made what changes and when. Git stores these changes in a repository. List of Top Useful
6 min read
How to Build Your Own Commands in Linux? Linux is one of the most widely used open-source operating systems which supports both GUI as well as CLI. It has been widely used all over the world since its first distribution launched on September 17, 1991, by Linus Torvalds. It is widely known for its command-line operations. We have been using
6 min read
How to Display and Set Date and Time in Linux | date Command Unlock the full potential of the date command in Linuxâa versatile tool that does more than just show the current date and time. With this command, you can set your systemâs clock, synchronize time across networks, and even calculate past or future dates for tasks like scheduling or logging. In this
8 min read
Some useful Linux Hacks Linux is the best-known and most-used open source operating system. As an operating system, Linux is software that sits underneath all of the other software on a computer, receiving requests from those programs and relaying these requests to the computerâs hardware. For the purposes of this page, we
5 min read
Run command with time limit in Linux In Linux, some commands take a really long time to execute, and while we do wait for such commands to finish, sometimes we also want to terminate them after they have been running for a specified amount of time. This is what the 'timeout' command and the `timelimit` program permit us to do. Two meth
4 min read
Nice and Renice Command in Linux with Examples In Linux, managing process priority is crucial for optimizing system performance, especially in environments where multiple processes compete for CPU resources. The nice and renice commands allow users to adjust the scheduling priority of processes, influencing how the Linux kernel allocates CPU tim
3 min read