Useful and Time-Saving Bash Commands in Linux


Linux, an open-source operating system, is widely recognized for its robustness, security, and flexibility. While it offers a variety of graphical user interfaces, Linux's command-line interface, powered by the Bash shell, remains a favorite among power users and system administrators. Mastering Bash commands can significantly enhance your productivity and efficiency in a Linux environment.

From simplifying complex tasks to automating repetitive operations, understanding and utilizing useful and time-saving Bash commands can unlock a world of possibilities. In this article, we will explore a range of essential Bash commands that will empower you to navigate directories, manipulate files, process text, manage processes, and handle packages more effectively.

Whether you are a beginner or an experienced user, this guide will equip you with valuable knowledge to streamline your workflow and optimize your Linux experience. So, let's dive into the world of Bash and discover the tools that can revolutionize your command-line journey.

Directory Navigation

One of the most basic command line operations is directory navigation. Here are some crucial commands to ensure seamless navigation −

  • cd − The current working directory can be changed using the cd command. Cd Documents, for instance, will switch the current directory to "Documents."

  • pwd  To view the current working directory, type pwd into your terminal. You can use this command to check where you are in the file system.

  • ls  The ls command displays a directory's contents in a list. You can get more specific information about the files and directories by adding options like -l (long format) or -a (including hidden files).

Example

$ cd Documents               # Changes the current directory to "Documents"
$ pwd                        # Displays the present working directory
/home/user/Documents

$ ls                         # Lists the contents of the current directory
file1.txt   folder1   file2.txt   folder2

File Manipulation

There are numerous commands available in Bash to manage files effectively. Let's examine some fundamental commands:

  • touch  A blank file is created by the touch command. To create a file called "example.txt" in the current directory, use the command "touch example.txt."

  • cp  In order to copy files or directories, use the cp command. For instance, the command cp file.txt newfile.txt will duplicate "file.txt" into "newfile.txt."

  • mv  Files can be moved or renamed using the mv command. Use the command mv file.txt /path/to/destination/ to move a file to a different directory. Give the new name as the destination to rename a file.

Example

$ touch example.txt          # Creates a new empty file
$ cp file.txt newfile.txt    # Copies "file.txt" and creates "newfile.txt"
$ mv file.txt /path/to/destination/    # Moves "file.txt" to a different directory
$ mv file.txt newname.txt    # Renames "file.txt" to "newname.txt"

Text Manipulation

Effective text manipulation is essential for a variety of tasks. Several commands are available in Bash to aid in text processing −

  • cat  The cat command shows a file's contents. For instance, the terminal will display the contents of "file.txt" if you type cat file.txt.

  • grep  To look for specific patterns in files, use grep. For instance, running the command grep "keyword" file.txt will show all lines that contain the given keyword.

  • sed  Text in files can be edited using the robust stream editor sed. Many operations, including search and replace, are supported. For instance, the command sed's/old/new/g' file.txt replaces all instances of "old" in "file.txt" with "new."

Example

$ cat file.txt               # Displays the contents of "file.txt"
Hello World!!

$ grep "congue" file.txt     # Searches for lines containing "congue"
Sed ultricies nibh nec ligula congue, ac congue odio fermentum.

$ sed 's/ultricies/sed/g' file.txt   # Replaces "ultricies" with "sed"
This is the example code of text manipulation

Process Management

Process management effectiveness is crucial for system administration. Listed below are a few helpful commands −

  • ps  The ps command shows details about currently active processes. Information about every process is provided in detail by adding options like -aux.

  • kill  Apply the kill command to stop any active processes. You can choose which processes to terminate by providing the process ID (PID). For instance, kill 1234 will end the process identified by ID 1234.

Example

$ ps                         # Displays information about running processes
  PID TTY          TIME CMD
 1234 pts/0    00:00:00 bash
 5678 pts/0    00:00:01 firefox

$ kill 5678                  # Terminates the process with ID 5678

Package Management

Package managers are used by Linux distributions to manage and install software. The commands for two well-known package managers are listed below −

  • apt-get  Distributions based on Debian, such as Ubuntu, manage packages using this command. Apt-get install package-name, for instance, installs the desired package.

  • yum  Red Hat-based distributions like CentOS use the yum command. Yum install package-name installs the desired package similarly to apt-get.

Example

$ apt-get install package-name   # Installs the specified package
$ yum install package-name       # Installs the desired package

Along with the commands mentioned above, here are a few extra hints and techniques to improve your use of Bash in Linux −

Command History

The commands you've run during the current session are recorded by Bash. The arrow keys can be used to move through the command history. To access previous commands and to advance, press the Up arrow key and the Down arrow key, respectively. advance, press the Up and Down arrow keysnumber to run a specific command again from history.

Example

$ history                    # Displays the command history
  1  cd Documents
  2  ls
  3  touch example.txt
  4  cp file.txt newfile.txt
  5  mv file.txt newname.txt

$ !3                         # Re-executes the command from line 3 (touch example.txt)

Conclusion

Your productivity and efficiency in Linux can be greatly increased by becoming a command-line whiz. A solid foundation for working with directories, files, text, processes, and packages is provided by the commands covered in this article. As you explore these commands and experiment with their various options, your abilities in the Linux command-line environment will advance. Use Bash's features and these useful, time-saving commands to streamline your processes and become a more skilled Linux user.

Updated on: 28-Jul-2023

118 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements