- Kloudnative
- Posts
- 5 Linux Command Tricks That Will Change Your Life
5 Linux Command Tricks That Will Change Your Life
Boost your productivity with these powerful Linux commands for developers!
Do you often find yourself repeatedly typing the same commands, wondering if there’s a more efficient way? For developers and system administrators, hours can be spent navigating the terminal, where every second counts. To some, the command line may seem like a cluttered toolbox, but for those who know its potential, it offers endless opportunities for optimization.
The power of Linux commands extends far beyond simple shortcuts. Mastering these commands can feel like uncovering hidden pathways in a familiar system — opening doors to faster, smarter, and more efficient ways of working. These tools aren't just conveniences; they are transformative solutions that can streamline workflows and reduce time spent on routine tasks.
Consider scenarios like setting up project environments or debugging server issues. The right commands can turn tasks that typically take an hour into solutions that take just minutes. These techniques are more than just helpful tricks — they represent fundamental shifts in the way users interact with their development environment.
In the following guide, discover five essential command-line techniques that many developers wish they had known earlier. These are not just optional tips but key skills that distinguish proficient Linux users from true command-line experts. Whether you're an experienced developer or just starting out, these insights can significantly enhance productivity and revolutionize daily workflows.
Ready to unlock the full potential of the terminal? Explore these powerful commands and see how they can transform development processes.
Kloudnative is committed to staying free for all our users. We kindly encourage you to explore our sponsors to help support us.
Unlock Windsurf Editor, by Codeium.
Introducing the Windsurf Editor, the first agentic IDE. All the features you know and love from Codeium’s extensions plus new capabilities such as Cascade that act as collaborative AI agents, combining the best of copilot and agent systems. This flow state of working with AI creates a step-change in AI capability that results in truly magical moments.
☝️ Support Kloudnative by clicking the link above to explore our sponsors!
Creating Multiple Combinations of Folders with mkdir
and Braces {}
Managing files and directories is a common task for developers, particularly when handling projects or setting up intricate directory structures. Typically, creating multiple directories requires repetitive typing. However, the mkdir
command offers a powerful feature using braces {}
that allows multiple directories to be created simultaneously.
The Problem
Imagine needing to create a folder structure for different environments — such as development, testing, and production — for multiple services in a project. Traditionally, this would mean creating each folder manually:
mkdir dev
mkdir test
mkdir prod
As the structure grows more complex, this approach becomes inefficient. For instance, if you need folders for both backend and frontend services in each environment, the manual process quickly becomes tedious and time-consuming.
The Solution: mkdir
with Braces {}
Instead of creating each directory manually, the mkdir
command with braces {}
allows you to generate multiple directories in one go. This method is efficient and saves time when setting up complex structures.
Example:
mkdir -p {dev,test,prod}/{backend,frontend}
Explanation:
The
-p
flag ensures parent directories are created as needed, preventing errors if they don’t already exist.The curly braces
{}
let you specify multiple options thatmkdir
will automatically combine.The first set
{dev,test,prod}
represents the different environments.The second set
{backend,frontend}
represents the different services.
This single command creates the following directory structure:
dev/backend
dev/frontend
test/backend
test/frontend
prod/backend
prod/frontend
What Happens Behind the Scenes
When the command executes, it creates the following folder structure:
dev/
├── backend/
└── frontend/
test/
├── backend/
└── frontend/
prod/
├── backend/
└── frontend/
This structure reflects each environment (dev
, test
, prod
) containing subdirectories for the services (backend
and frontend
). The use of braces {}
allows mkdir
to efficiently generate all these combinations in a single command.
Why This Trick is a Game-Changer
Using mkdir
with braces {}
offers several key advantages:
Saves Time: Instead of creating each directory individually, you can generate all necessary combinations with a single command.
Reduces Errors: Minimizes the chance of mistakes when manually typing out complex folder structures.
Simplifies Complexity: Makes it easy to set up large projects with intricate directory hierarchies in a clear and concise way.
This approach streamlines project setup and ensures consistency, making it an invaluable tool for developers managing multiple environments and services.
When working within deeply nested directories, it’s common to move through various subdirectories — for code, configuration files, or logs. Returning to your original directory by repeatedly typing cd ..
can be both tedious and error-prone.
Instead of manually navigating back step-by-step, the cd -
command offers a more efficient way to return to your previous directory. This simple trick saves time and reduces the frustration of working in complex directory structures.
The Problem
Consider working in a deeply nested directory like:
/home/user/projects/webapp/src/frontend/components
To return to the project root, you would typically type:
cd ..
cd ..
cd ..
cd ..
This means running four separate commands just to navigate back to /home/user/projects/
. This process is tedious and makes it easy to lose track of your original location.
The Solution: cd -
A quicker and more efficient approach is to use the cd -
command. This instantly takes you back to your previous directory, no matter how deeply nested you are.
Example:
Navigate to a deeply nested folder:
cd /home/user/projects/webapp/src/frontend/components
To return to the previous location, simply run:
cd -
This saves time and avoids the need for multiple cd ..
commands, making directory navigation much more streamlined and error-free.
How It Works
The cd -
command doesn't move up the directory tree like cd ..
. Instead, it takes you back to the last directory you visited, acting like a "back" button in a web browser.
Example:
Start in the project root directory:
cd /home/user/projects/webapp/
Move to a nested directory:
cd /home/user/projects/webapp/src/frontend/components
To return instantly to the project root, use:
cd -
This takes you back to
/home/user/projects/webapp/
.
What’s Happening Behind the Scenes
When cd -
is used, the shell keeps track of the previous directory in memory. When you run the command, it swaps your current location with the last one. This allows you to quickly switch between two directories without having to remember or type out long paths.
Practical Use Case
This method is particularly helpful when:
Debugging: Switching between logs and code directories.
Project Development: Moving between the project root and deeply nested folders for configuration, assets, or components.
Efficiency: Avoiding repeated manual navigation with
cd ..
or retyping lengthy paths.
By using cd -
, directory navigation becomes much faster and less error-prone, allowing for a smoother workflow.
Create Multiple Files Quickly with touch
and a Range of Numbers
Creating multiple files is a routine task for developers, especially when generating test files, placeholders, or logs. While you might create files one at a time, doing this manually for many files can quickly become tedious. Fortunately, Linux provides an efficient way to create multiple files in a single command using touch
and a range of numbers within braces {}
.
The Problem
Imagine you need to create 100 numbered files, such as test1.txt
, test2.txt
, ..., up to test100.txt
. Doing this manually with individual touch
commands would look something like:
touch test1.txt
touch test2.txt
touch test3.txt
...
touch test100.txt
As you can imagine, this process is time-consuming and cumbersome. But there’s a better way!
The Solution: touch
with a Number Range and Braces {}
Instead of creating each file one at a time, you can use the touch
command with a number range inside braces {}
to quickly generate all the files in one go. Here's how:
touch test{1..100}.txt
What’s Happening Here
The
touch
command is used to create new files.By adding a number range inside braces
{1..100}
, you're telling the shell to create files numbered from1
to100
.The result is the creation of
test1.txt
,test2.txt
, ..., up totest100.txt
—all generated in a single command.
Example
Running the following command:
touch test{1..10}.txt
Will create the following files:
test1.txt
test2.txt
test3.txt
...
test10.txt
Why This Trick is a Game-Changer
Time-Saving: Instantly creates multiple files without manual repetition.
Efficient: Particularly useful when you need placeholder files for testing, logging, or when automating processes that require multiple files as input.
Less Error-Prone: Eliminates the risk of errors when typing long filenames manually.
Practical Use Case
This trick is especially handy in scenarios like testing an application that processes large numbers of files. Instead of manually creating each file, you can generate them all with one command and focus your time on writing and testing your code. It’s perfect for scenarios where you need to quickly generate test files, log files, or batch files for processing.
Real-Time File Updates with tail -f
As a programmer, particularly when working with backend systems, monitoring log files for errors, system behavior, or debugging can be a frequent task. Manually checking log files repeatedly can be inefficient. Fortunately, the tail
command offers an option to monitor log files in real-time, providing continuous updates as new data is appended to the file.
The Problem
Let’s say you’re working on an application and need to monitor the error_file.log
to track any new errors being logged. Normally, you might use the tail
command to read the last few lines of the file like this:
tail error_file.log
While this shows the current state of the file, it doesn’t update in real-time. If the file changes, you’ll have to rerun the command to see the new entries. This is cumbersome, especially when troubleshooting live systems or monitoring logs for real-time events.
The Solution: tail -f
for Real-Time Updates
By using tail -f
, you can follow the changes in the log file as they happen, viewing new lines as they are added without needing to rerun the command.
tail -f error_file.log
What’s Happening Here
The
tail
command, by default, shows the last 10 lines of a file.The
-f
option stands for "follow" and keeps the terminal open, continuously displaying new lines as they are appended to the file.This is particularly helpful when monitoring log files during your application’s execution, allowing you to view error messages or output in real-time.
Example
For instance, you may want to monitor a web server’s log file to track incoming requests or errors:
tail -f /var/log/nginx/access.log
As new requests come in, they’ll be displayed live in your terminal. There's no need to refresh or re-run the command — it continuously follows the file for updates.
Practical Use Case
One of the most common use cases for tail -f
is during debugging. For example, if you're troubleshooting an intermittent issue in your application, you can follow the application's error log in real-time to spot error messages as they appear and immediately take action.
For instance, you might run:
tail -f /var/log/app/error.log
If your application crashes, the error message will pop up in real-time, allowing you to address the problem without delay.
Bonus Tip: Combine with grep
If you’re only interested in specific log entries, you can combine tail -f
with grep
to filter for relevant information. For example:
tail -f error_file.log | grep "ERROR"
This command will display only the lines that contain the word “ERROR,” helping you focus on critical log messages while ignoring irrelevant information.
Review Recent Commands with history 5
As developers, it's common to repeat certain commands during a work session, whether you’re compiling code, managing files, or restarting services. Remembering the exact syntax of a command you used earlier or retracing your steps can be difficult, especially after hours of working in the terminal.
Fortunately, Linux keeps track of every command you run in a session. The history
command allows you to view a list of previously executed commands, and using it efficiently can save you time and effort.
The Problem
Imagine you’ve been working for a while, and you ran a complex grep
or find
command that you now need to reuse. You can’t quite remember the exact syntax, and scrolling through the terminal manually to find it is cumbersome.
The Solution: history 5
to Recall Recent Commands
The history
command displays a list of commands you've executed during your current shell session. By default, it shows the entire history of your commands, but you can limit the output to a specific number of commands by specifying that number.
For example:
history 5
This will display the last five commands you executed, making it easier to quickly recall and reuse them.
What’s Happening Here
Running
history
alone will display your entire command history for the session.By adding a number (e.g.,
history 5
), you limit the output to the most recent five commands, helping you focus on the latest tasks you've performed.
For example, if your last five commands were:
123 ls
124 cd src/
125 mkdir newdir
126 touch newfile.txt
127 history 5
Running history 5
would show:
123 ls
124 cd src/
125 mkdir newdir
126 touch newfile.txt
127 history 5
Why This Trick is Useful
If you’ve forgotten how you executed a command, the history
command saves you from having to figure it out again. It's especially helpful for recalling complex commands, such as those with long flags or options that you don’t want to retype from memory.
Practical Use Case
Let’s say you’re debugging a program and running multiple commands to compile code, check logs, and restart services. Instead of scrolling back through the terminal or trying to remember each command, you can run:
history 5
You’ll get a list of your last five commands, making it easy to recall and rerun any of them with minimal effort.
Bonus Tip: Re-Executing Commands
You can quickly re-execute any command from your history by using the exclamation mark !
followed by the command number. For instance, if you want to rerun the mkdir newdir
command (command number 125), you would simply type:
!125
This saves you from having to retype the entire command and can be a huge time-saver, especially when working with long commands.
Conclusion
Mastering the Linux command-line interface is essential for developers, and the tricks we've covered here are powerful tools to add to your toolkit. From efficiently managing files and folders to navigating directories and recalling commands, these five tricks can significantly streamline your workflow and enhance your productivity:
mkdir
with braces: Create multiple folders in one go with a single command.cd -
: Quickly jump back to your previous directory, saving time on navigation.touch
with a range: Create multiple files at once without repetitive manual work.tail -f
: Follow log files in real-time, enabling live monitoring and debugging.history 5
: Recall and re-execute recent commands, speeding up repetitive tasks.
These are just a few examples of the power Linux commands offer. By incorporating these tricks into your daily workflow, you can focus more on coding and less on repetitive tasks, making you a more efficient and productive programmer.