Home Linux Tips & Tricks Linux Command Line Tricks For Efficient And Faster Workflow

Linux Command Line Tricks For Efficient And Faster Workflow

Discover useful Linux command line tricks and terminal tips to enhance your productivity and efficiency.

By sk
Published: Updated: 43K views

In the world of Linux, mastering the command line is essential for developers, system administrators, and power users. With the right command line tricks, you can significantly enhance your productivity and streamline your workflow. In this guide, we will explore a collection of Linux command line tricks that will help you work more efficiently and accomplish tasks with speed and precision.

15 Linux Command Line Tricks and Terminal Tips

Here I am presenting a compilation of 15 Linux Command Line Tricks and Terminal Tips, perfect for both Linux beginners and power users. While not exhaustive, these tricks are sufficient to boost your efficiency and productivity in the Linux terminal.

1. Take Advantage of Tab Completion:

One of the most powerful features of the Linux command line is tab completion. By pressing the Tab key, the terminal can automatically complete commands, file names, and directory paths. Utilize this feature to save time, reduce typos, and navigate through complex directory structures effortlessly.

Example:

$ cd /path/to/long_directory_name

By typing cd /p and pressing Tab, the command line completes the rest of the directory path.

You can also use Tab completion to speed up your typing when entering variable names or filenames. This avoids errors and saves time.

Example:

$ my_script.sh myfile.txt

By typing my and pressing Tab, the filename myfile.txt is completed.

What if multiple directories have common letters in their name? For example, both the Documents and Downloads directories start with 'Do' in their names. How to use TAB completion in such case?

In cases where multiple directories start with the same letters and you want to use the cd command to navigate to a specific directory, you can utilize the following methods:

A). Use Tab Completion: Type the common starting letters of the directory name and press Tab. The terminal will attempt to autocomplete the directory name. If there are multiple options, pressing Tab twice will display a list of possible matches. You can then continue typing additional characters to make the selection unique.

Example:

$ cd Do<Tab><Tab>

Pressing Tab twice will show a list of directories that start with "Do". You can then type more characters to narrow down the selection, such as "Doc" or "Down".

B). Provide More Characters: Add more characters from the directory name to make it more specific and distinguish it from others. By including additional letters, you can ensure a unique match.

Example:

$ cd Docume

Adding the additional "cume" narrows down the selection to the "Documents" directory.

C). Use the Relative or Absolute Path: If the directories have different parent directories or if you know the exact path, you can use the relative or absolute path to navigate directly to the desired directory.

Example:

$ cd /path/to/Documents

Using the absolute path ensures you go directly to the "Documents" directory without any ambiguity.

By applying these methods, you can effectively navigate to the desired directory even when multiple directories have similar starting letters in their names.

2. Use Command History and Shortcuts:

The command history allows you to recall and reuse previously executed commands. Press the Up arrow key to cycle through your command history or use the history command to view a list of recent commands. Additionally, leverage shortcuts like Ctrl + R to search and repeat commands based on keywords.

Example:

$ history
$ !25  # Executes the command with history number 25

3. Employ Output Redirection:

Redirecting command output to files or other commands is a powerful technique. The > operator allows you to redirect standard output to a file, while | (pipe) sends the output of one command as input to another. Leverage these techniques to save command output, filter data, or perform complex operations.

Example:

$ ls > file.txt  # Redirects the ls command output to a file
$ cat file.txt | grep "keyword"  # Filters the content of file.txt for a specific keyword

4. Utilize Command Substitution:

Command substitution lets you use the output of one command as an argument or input for another command. Enclose the command in backticks (`) or use the `$(...)` syntax to substitute the output. This allows for dynamic and flexible command execution.. This allows for dynamic and flexible command execution.

Example:

$ echo "Today is $(date)"

5. Take Advantage of Keyboard Shortcuts:

Each Linux Shell (E.g. Bash, Fish, Zsh etc.) offers a range of keyboard shortcuts to enhance your command line experience. Familiarize yourself with essential shortcuts to save time and improve your overall efficiency.

Example:

$ cp /path/to/source/file.txt /path/to/destination/Alt + F

Using Alt + F, the cursor jumps forward one word at a time.

Here's a list of commonly used Bash shortcuts:

ShortcutDescription
Ctrl + AMove cursor to the beginning of the line
Ctrl + EMove cursor to the end of the line
Ctrl + BMove cursor backward one character
Ctrl + FMove cursor forward one character
Alt + BMove cursor backward one word
Alt + FMove cursor forward one word
Ctrl + UCut (delete) the line before the cursor position
Ctrl + KCut (delete) the line after the cursor position
Ctrl + YPaste (yank) the previously cut text
Ctrl + WDelete the word before the cursor
Alt + DDelete the word after the cursor
Ctrl + DExit the current shell or end input (at the beginning of a line)
Ctrl + CTerminate the currently running command
Ctrl + LClear the screen
Ctrl + RSearch the command history
TabAuto-complete commands, filenames, or directories
Up arrowRecall previous command from history
Down arrowNavigate forward through command history
Bash Shortcuts

Fish Shell Shortcuts:

ShortcutDescription
Alt + FMove cursor forward one word
Alt + BMove cursor backward one word
Alt + DDelete the word after the cursor
Alt + WCut (delete) the word before the cursor
Alt + .Insert the last argument of the previous command
Alt + /Auto-complete from history
Alt + ?List possible completions
Ctrl + CTerminate the currently running command or process
Ctrl + LClear the screen
Ctrl + DExit the current shell or end input
Ctrl + FMove cursor forward one character
Ctrl + BMove cursor backward one character
Ctrl + UCut (delete) the line before the cursor position
Ctrl + KCut (delete) the line after the cursor position
Ctrl + WDelete the word before the cursor
Ctrl + YPaste (yank) the previously cut text
Ctrl + TSwap the current character with the previous character
Ctrl + HDelete the character before the cursor
Ctrl + EMove cursor to the end of the line
Ctrl + AMove cursor to the beginning of the line
Ctrl + RSearch the command history
TabAuto-complete commands, arguments, and file paths
Up arrowRecall previous command from history
Down arrowNavigate forward through command history
Fish Shortcuts

Zsh Shell Shortcuts:

ShortcutDescription
Alt + FMove cursor forward one word
Alt + BMove cursor backward one word
Alt + DDelete the word after the cursor
Alt + WCut (delete) the word before the cursor
Alt + .Insert the last argument of the previous command
Alt + /Auto-complete from history
Alt + ?List possible completions
Ctrl + CTerminate the currently running command or process
Ctrl + LClear the screen
Ctrl + DExit the current shell or end input
Ctrl + FMove cursor forward one character
Ctrl + BMove cursor backward one character
Ctrl + UCut (delete) the line before the cursor position
Ctrl + KCut (delete) the line after the cursor position
Ctrl + WDelete the word before the cursor
Ctrl + YPaste (yank) the previously cut text
Ctrl + TSwap the current character with the previous character
Ctrl + HDelete the character before the cursor
Ctrl + EMove cursor to the end of the line
Ctrl + AMove cursor to the beginning of the line
Ctrl + RSearch the command history
TabAuto-complete commands, arguments, and file paths
Up arrowRecall previous command from history
Down arrowNavigate forward through command history
Zsh Shortcuts

These shortcuts can greatly enhance your productivity and efficiency when working in the Bash shell. Make use of these shortcuts to navigate, edit, and manipulate commands and text more efficiently.

6. Use Wildcards and Regular Expressions:

Wildcards and regular expressions provide powerful pattern matching capabilities in the command line. Employ characters like * (matches any characters) and ? (matches a single character) to perform advanced file and text manipulation tasks.

Example:

$ ls *.txt  # Lists all files with the .txt extension
$ grep "^Hello" file.txt  # Searches for lines starting with "Hello" in file.txt

7. Use aliases:

Create custom shortcuts for frequently used commands by setting up aliases. This allows you to execute complex or lengthy commands with just a few keystrokes.

Example:

$ alias ll='ls -l'

Now, you can simply type ll instead of ls -l to list files in long format.

Related Read: The alias And unalias Commands Explained With Examples

8. Use the !! Command:

Re-execute the previous command instantly using !!. This is especially useful when you need to repeat a command with elevated privileges (using sudo !!).

Example:

$ ls
$ sudo !!

The second command executes the previous command (ls) with sudo privileges.

Related Read: Different Ways To Repeat Your Last Command In Linux

9. Utilize the tee Command:

The tee command allows you to read input from standard input and write it to both standard output and one or more files simultaneously. i.e. Capture command output and display it on the screen while simultaneously saving it to a file or redirecting it to multiple files.

Example:

$ ls -l | tee file.txt

This command lists the files and directories in the current directory with detailed information and saves that output to a file called "file.txt".

This trick is particularly useful when you want to capture the output of a command while still displaying it on the screen. It allows you to save the output to a file for future reference or further processing, without losing the real-time display in the terminal.

Additionally, you can use the tee command to redirect and append output to multiple files at once, offering flexibility in managing command output.

10. Utilize Background and Foreground Tasks:

Execute long-running commands or tasks in the background using & at the end of the command. This allows you to continue working in the foreground.

Example:

$ long_running_command &

The command runs in the background, freeing up your terminal for further work.

To bring a background running task to the foreground in the Linux command line, you can use the fg command. Here's how:

1. First, check the list of currently running background tasks by using the jobs command.

Example:

$ jobs
[1]   Running                 command1 &
[2]-  Running                 command2 &

In the above example, the jobs command lists two running background tasks with their job numbers.

2. Identify the task you want to bring to the foreground. Each job will have an associated job number.

3. To bring a specific job to the foreground, use the fg command followed by the job number.

$ fg %1

By using fg %1, the first job is brought to the foreground.

Once the task is in the foreground, you can interact with it as you would with any foreground process. It will receive input and display output in the terminal session.

11. Use the Ctrl + Z Shortcut:

Suspend a currently running process and return to the shell prompt. You can later bring the process back to the foreground or send it to the background as needed.

Example:

$ long_running_process
Ctrl + Z  # Suspends the process
$ bg  # Runs the process in the background

12. Recall Command Arguments

Use shortcuts like Alt + . to recall the last argument of the previous command.

Example:

$ mkdir path/to/my/directory
$ cd Alt + .  # The last argument 'directory' is automatically recalled.

13. Use the watch Command:

Continuously monitor the output of a command by using the watch command. This is useful for tracking changes or updates in real-time.

Example:

$ watch -n 1 ls -l

The above command displays the output of ls -l every 1 second.

14. Utilize the apropos Command for Keyword-based Manual Page Search:

The apropos command allows you to search for manual pages based on keywords. It helps you quickly find relevant manual pages related to a specific topic or command.

Example:

$ apropos network

This command will provide a list of manual pages related to the keyword "network," such as "ifconfig," "ping," or "netstat."

Related Read: How To Easily Recall Forgotten Linux Commands Using Apropos

15. Utilize the Ctrl + S and Ctrl + Q Shortcuts for Terminal Flow Control:

In the terminal, pressing Ctrl + S freezes the screen (XOFF) and Ctrl + Q resumes it (XON). These shortcuts are useful when you want to pause the output or scroll back to review information.

Example:

$ long_running_command
Ctrl + S # Freezes the output
Ctrl + Q # Resumes the output

Using these shortcuts, you can control the flow of information displayed in the terminal.

Conclusion

By incorporating these Linux command line tricks into your workflow, you can boost productivity, save time, and become more efficient in your daily tasks. Experiment with these techniques, explore further command line tools and options, and continue to expand your command line proficiency.


Related Read:


You May Also Like

2 comments

Edjar May 30, 2023 - 6:19 am

Finally a useful article for us users that are not beginners but not yet advance users. Thank you

Reply
Pellicle June 2, 2023 - 2:05 am

I’d like to second this. I’m a developer who’s used Linux since it was distributed on floppy discs at my University. In the last 20 I’ve worked on bigger systems which are maintained by a dedicated team (for at the least security reasons). This means my knowledge of Linux is more as a tool to do my job, not as a sysadmin. So stuff that’s not “Linux for real dummies” but not like reading a man page is welcome. Thanks

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More