Robert Johns | 18 Dec, 2024
Fact checked by Jim Markus

10 Linux Command Line Hacks Every User Should Know

The Linux command line can become an even more powerful tool when you use it efficiently.

That's why I want to cover 10 essential hacks that every Linux user should know to save you time, reduce frustration, and transform the command line into a seamless extension of your workflow.

Let's dive in!

10 Linux Command Line Hacks Every User Should Know

Fixing sudo Command Errors

Have you ever run a command only to realize you forgot to add sudo in front of it?

Instead of typing the command again, a simple hack can save you time: the !! command.

After executing a command that fails due to missing sudo, simply type sudo !! to rerun the previous command with sudo.

This neat trick is a lifesaver when dealing with complex commands.

Reverse Search in Command History

Navigating through a long command history to find a command can be tedious.

With the reverse search feature, you can swiftly locate past commands.

By pressing Ctrl + R, you can enter the reverse search mode.

Start typing any part of a previous command, and it will surface for you to rerun. This is particularly useful for those intricate commands you only half-remember.

Repeating Commands Quickly

If you frequently reuse commands, the !! command also allows you to quickly repeat the last command without adding sudo.

This ability can be a real time-saver, especially during troubleshooting or repeated testing, as it minimizes keystrokes and enhances efficiency.

Managing Background and Foreground Jobs

Effectively controlling background and foreground processes is vital.

By using Ctrl + Z, you can suspend a running process and then use bg to send it to the background.

To bring it back to the foreground, use fg.

This flexibility empowers you to multitask within the terminal, which is essential for advanced users.

Creating Command Aliases

Typing the same lengthy command repeatedly can be tedious.

This is where the alias command becomes invaluable. Create simple shortcuts for complex commands, like using ll as an alias for ls -alh, or gs for git status.

These types of alias shortcuts save keystrokes, streamline workflows, and reduce errors from typing frequently used commands.

Searching Command History with Grep

Instead of scrolling through your entire command history, utilize the history command with grep to find specific commands you executed days ago.

For example, by searching for apt using history | grep apt, you can instantly view all recent commands involving apt.

This is an awesome way to locate commands when you can only partly remember what you ran.

Substituting Mistakes in Commands

Quickly correct typos in your previous commands with the substitution syntax.

For instance, if you typed ping google.cm instead of ping google.com, terminate the process and use !!:s/cm/com/ to fix and rerun the command.

This is perfect for avoiding the hassle of re-typing long commands.

Using Command Substitution

The great thing about command substitution is that it lets you embed the output of one command within another command.

For example, print the current date by using echo "Today is $(date)".

Embedding outputs can be especially powerful in scripting, allowing dynamic content generation based on real-time data.

Checking Disk Usage

Quick disk usage checks are vital. Use df -h for a comprehensive list of all disks and their usage.

For more detailed information in your current directory, du -sh * provides usage statistics for each item.

These commands should be a go-to for insights into storage usage or for managing your system's disk space effectively.

Using the tee Command for Logging

The tee command is perfect for logging command outputs to a file while displaying them on the screen.

For instance, use ls | tee fileslist.txt to write the directory listing to fileslist.txt while still seeing it onscreen.

This is the kind of thing that's crucial for system administrators who need to retain logs during real-time monitoring.

Wrapping Up

Linux productivity hacks can make the command line experience faster, simpler, and more enjoyable.

From reusing commands to effective multitasking, each of the tips I've covered here can really enhance your command line proficiency.

My advice, start applying these hacks today! By making them a part of your everyday command set, you'll see results in no time.

By Robert Johns

Technical Editor for Hackr.io | 15+ Years in Python, Java, SQL, C++, C#, JavaScript, Ruby, PHP, .NET, MATLAB, HTML & CSS, and more... 10+ Years in Networking, Cloud, APIs, Linux | 5+ Years in Data Science | 2x PhDs in Structural & Blast Engineering

View all post by the author

Subscribe to our Newsletter for Articles, News, & Jobs.

I accept the Terms and Conditions.

Disclosure: Hackr.io is supported by its audience. When you purchase through links on our site, we may earn an affiliate commission.

Learn More

Please login to leave comments