top of page

Top 15 Linux Command Line Tricks

Updated: Jun 22, 2023

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

Top 15 Linux Command Line Tricks

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.

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:

SHORTCUT

DESCRIPTION

Ctrl + A

Move cursor to the beginning of the line

Ctrl + E

Move cursor to the end of the line

Ctrl + B

Move cursor backward one character

Ctrl + F

Move cursor forward one character

Alt + B

​Move cursor backward one word

Alt + F

Move cursor forward one word

Ctrl + U

Cut (delete) the line before the cursor position

Ctrl + K

Cut (delete) the line after the cursor position

Ctrl + Y

Paste (yank) the previously cut text

Ctrl + W

Delete the word before the cursor

Alt + D

Delete the word after the cursor

Ctrl + D

Exit the current shell or end input (at the beginning of a line)

Ctrl + C

Terminate the currently running command

Ctrl + L

Clear the screen

Ctrl + R

Search the command history

Tab

Auto-complete commands, filenames, or directories

Up arrow

Recall previous command from history

Down arrow

​Navigate forward through command history

Fish Shell Shortcuts:

SHORTCUT

DESCRIPTION

Alt + F

Move cursor forward one word

Alt + B

Move cursor backward one word

Alt + D

Delete the word after the cursor

Alt + W

Cut (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 + C

Terminate the currently running command or process

Ctrl + L

Clear the screen

Ctrl + D

Exit the current shell or end input

Ctrl + F

Move cursor forward one character

Ctrl + B

Move cursor backward one character

Ctrl + U

Cut (delete) the line before the cursor position

Ctrl + K

Cut (delete) the line after the cursor position

Ctrl + W

Delete the word before the cursor

Ctrl + Y

Paste (yank) the previously cut text

Ctrl + T

Swap the current character with the previous character

Ctrl + H

Delete the character before the cursor

Ctrl + E

Move cursor to the end of the line

Ctrl + A

Move cursor to the beginning of the line

Ctrl + R

Search the command history

Tab

Auto-complete commands, arguments, and file paths

Up arrow

Recall previous command from history

Down arrow

Navigate forward through command history

Zsh Shell Shortcuts:

SHORTCUT

DESCRIPTION

Alt + F

Move cursor forward one word

Alt + B

Move cursor backward one word

Alt + D

Delete the word after the cursor

Alt + W

Cut (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 + C

Terminate the currently running command or process

Ctrl + L

Clear the screen

Ctrl + D

Exit the current shell or end input

Ctrl + F

Move cursor forward one character

Ctrl + B

Move cursor backward one character

Ctrl + U

Cut (delete) the line before the cursor position

Ctrl + K

Cut (delete) the line after the cursor position

Ctrl + W

Delete the word before the cursor

Ctrl + T

Paste (yank) the previously cut text

Ctrl + H

Swap the current character with the previous character

Ctrl + E

Delete the character before the cursor

Ctrl + A

Move cursor to the end of the line

Ctrl + R

Move cursor to the beginning of the line

Ctrl + C

Search the command history

Tab

Auto-complete commands, arguments, and file paths

Up arrow

Recall previous command from history

Down arrow

Navigate forward through command history

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.

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.

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."

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.

23 views0 comments

Related Posts

See All
Stationary photo

Be the first to know

Subscribe to our newsletter to receive news and updates.

Thanks for submitting!

Follow us
bottom of page