18 KiB
title, source, author, published, created, description, tags
| title | source | author | published | created | description | tags | ||
|---|---|---|---|---|---|---|---|---|
| 5 Linux Command Tricks That Will Change Your Life as a Programmer | https://levelup.gitconnected.com/5-linux-command-tricks-that-will-change-your-life-as-a-programmer-825a4524218a |
|
2024-10-10 | 2024-10-29 | Discover five must-know Linux command-line tricks to enhance your workflow. Learn how to navigate directories, manage files, and track logs like a pro. |
|
Boost your productivity with these powerful Linux commands for developers!
[
](https://cycoderx.medium.com/?source=post_page---byline--825a4524218a--------------------------------)
[
Photo by Gabriel Heinzer on Unsplash
The command-line interface is the bread and butter for most tech professionals, especially developers and system administrators. Whether you are writing scripts, configuring servers, or automating tasks, knowing your way around Linux commands can save you hours of work.
For many, Linux may seem daunting at first glance, but once you unlock the potential of its command-line interface (CLI), you’ll realize it’s a programmer’s best friend. From directory navigation to file manipulation and even debugging, mastering these simple yet powerful Linux commands can drastically improve your workflow.
In this article, we’ll explore five command-line tricks that can make your life easier if you’re a developer. These aren’t just “nice-to-know” shortcuts — they’re game-changers that will save you time and make your interactions with the terminal far more efficient.
Let’s dive in!
I’d be grateful for your support with a clap! If you’re passionate about Python, SQL, Data Engineering and Data Science, consider following me for more content.😊
Python Chronicles CyCoderX
Creating Multiple Combinations of Folders with mkdir and Braces {}
Organizing files and directories is a frequent task for developers, especially when managing projects or setting up complex directory structures. Normally, creating multiple directories involves a lot of repetitive typing. However, there’s a powerful trick with the mkdir command that allows you to create multiple directories in one go using braces {}.
The Problem
Let’s say you want to create a folder structure for different environments (development, testing, and production) for several services in your project. Traditionally, you might do this manually, which could look like:
mkdir devmkdir testmkdir prod
This gets cumbersome when your structure becomes more complex. For example, if you needed folders for both backend and frontend services in all three environments, the process becomes tedious.
The Solution: mkdir with Braces {}
Instead of creating each directory one by one, you can combine all of this into a single command using mkdir and braces {}. This allows you to create combinations of directories efficiently.
For example:
mkdir -p {dev,test,prod}/{backend,frontend}
Here’s a breakdown of what’s happening:
- The
-pflag tellsmkdirto create parent directories as needed (i.e., it won’t throw errors if the parent directories don’t exist yet). - The curly braces
{}allow you to specify multiple options thatmkdirwill automatically combine. - The first set
{dev,test,prod}creates directories for different environments. - The second set
{backend,frontend}creates directories for different services.
What Happens Behind the Scenes
When this command runs, the following folder structure is created:
dev/ backend/ frontend/test/ backend/ frontend/prod/ backend/ frontend/
Why This Trick is a Game-Changer
This method saves significant time and reduces human error, especially when you’re setting up large projects with complex folder structures. Instead of typing out every combination manually, you can define them in one concise command.
Navigate Back with cd - Instead of Repeatedly Using cd ..
As a programmer, it’s common to find yourself working within deeply nested directories. For instance, you might be moving from your project’s root directory into subdirectories for code, configuration files, or logs. However, when it’s time to return to where you started, navigating back manually by typing cd .. multiple times can become frustrating and error-prone.
The Problem
Imagine you’re working in a nested directory like this:
/home/user/projects/webapp/src/frontend/components
To return to the project root, you might normally have to type cd .. several times, like this:
cd ..cd ..cd ..cd ..
That’s four separate commands just to get back to /home/user/projects/. Not only is this tedious, but it’s also easy to lose track of where you came from.
The Solution: cd -
Here’s a simpler, faster alternative: use the cd - command to instantly return to your previous directory, no matter how deep you’ve gone into a nested structure.
How It Works
- The command
cd -doesn’t navigate up the directory tree likecd ... Instead, it takes you back to the last directory you were in, almost like a "back" button in a web browser.
For example, let’s say you started in your project’s root directory /home/user/projects/webapp/, then moved into the nested directory /home/user/projects/webapp/src/frontend/components. Now, instead of typing cd .. several times to return, you can simply type:
cd -
And you’ll be instantly back in /home/user/projects/webapp/.
What’s Happening Behind the Scenes
The cd - command stores the previous directory in memory, so when you use it, it swaps your current directory with the last one. It’s a highly efficient way to navigate back and forth between two directories, especially when you’re jumping between a working directory and a deeply nested one.
Practical Use Case
This trick is especially useful when you’re debugging or working on multiple parts of a project. If you need to frequently switch between the project’s root and a deep subdirectory, cd - saves you from typing long paths or navigating up step by step.
Photo by Igor Starkov on Unsplash
Create Multiple Files Quickly with touch and a Range of Numbers
Creating multiple files is a common task for developers, whether you’re generating test files, placeholders, or logs. While you might normally create files one at a time, doing so manually for a large number of files becomes cumbersome. Fortunately, Linux offers a 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 for a series of test cases, like test1.txt, test2.txt, all the way up to test100.txt. Doing this manually with individual touch commands would look something like this:
touch test1.txttouch test2.txttouch test3.txt...touch test100.txt
As you can imagine, this is tedious and time-consuming. But there’s a better way!
The Solution: touch with a Number Range and Braces {}
You can use the touch command with a range of numbers inside braces {} to quickly create multiple files in one go. Here's how it works:
touch test{1..100}.txt
What’s Happening Here
- The
touchcommand is used to create new files. Normally, you’d use it like this:touch filename.txt. - By adding a number range inside braces
{1..100}, you tell the shell to create files numbered from 1 to 100. - The result is the creation of
test1.txt,test2.txt,test3.txt, all the way up totest100.txt—in a single command.
Example
Let’s break it down. Running:
touch test{1..10}.txt
Creates the following files in your directory:
test1.txttest2.txttest3.txt...test10.txt
Why This Trick is a Game-Changer
This method allows you to generate multiple files instantly, saving you from having to manually create each file one by one. It’s particularly useful in scenarios where you need placeholder files for testing or logging, or when automating processes that require multiple files as input.
Practical Use Case
For example, you might use this trick to quickly generate log files for 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.
Photo by BoliviaInteligente on Unsplash
Real-Time File Updates with tail -f
As a programmer, especially when working with backend systems, you’ll often need to monitor log files for errors, system behavior, or debugging purposes. In such cases, manually checking a log file over and over can be inefficient. Fortunately, the tail command has an option to automatically follow updates in real-time, allowing you to monitor changes as they happen.
The Problem
Let’s say you’re working on an application and you need to monitor the log file 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 gives you 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 lines. This is far from ideal, especially when troubleshooting live systems or monitoring logs for real-time events.
The Solution: tail -f for Real-Time Updates
Using the tail -f command, you can follow the changes in the log file as they happen. This allows you to see new lines appended to the file without needing to re-run the command.
tail -f error_file.log
What’s Happening Here
- The
tailcommand by itself shows the last 10 lines of a file. - The
-foption stands for “follow,” which keeps the terminal open and continuously displays new lines as they are added to the file.
This is especially useful when monitoring log files during the execution of your application, as you can see error messages or output in real-time.
Example
Let’s say you’re monitoring a web server’s log file to see incoming requests and errors:
tail -f /var/log/nginx/access.log
As new requests come in, you’ll see them 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 practical uses of tail -f is during debugging. Imagine you’re trying to find the cause of an intermittent issue in your application. By following the application’s error log in real-time, you can see new 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, you’ll see the error message pop up in real-time, allowing you to troubleshoot without delay.
Bonus Tip: Combine with grep
If you want to follow a log file in real-time but only care about specific entries, you can combine tail -f with grep. For example:
tail -f error_file.log | grep "ERROR"
This command will show only the lines that contain the word “ERROR,” helping you filter the log output for relevant information.
Review Recent Commands with history 5
As developers, we often find ourselves repeating certain commands during a work session — whether it’s compiling code, managing files, or restarting services. Remembering the exact syntax of a command you used earlier or retracing your steps can be tricky, especially if you’ve been working in the terminal for hours.
Thankfully, Linux keeps track of every command you run in a session. The history command allows you to see a list of your previously executed commands, and using it efficiently can save you a lot of time and effort.
The Problem
Let’s say 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 tedious.
The Solution: history 5 to Recall Recent Commands
The history command displays a list of commands you’ve previously run in your current shell session. By default, it shows your entire command history, but you can limit the number of commands it displays by specifying a number.
For example:
history 5
This will display the last five commands you executed, making it easy to quickly recall and rerun them.
What’s Happening Here
historyalone will print out the entire history of commands from the session.- By adding a number (e.g.,
history 5), you limit the output to the most recent five commands, helping you focus on just the last few 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, history saves you from having to figure it out all over again. It’s also great 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 several different commands to compile, check logs, and restart the service. Instead of remembering each command or scrolling back in the terminal, you can simply 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 the history by using the exclamation mark (!) followed by the command number. For instance, if you wanted to rerun the mkdir newdir command from above (command number 125), you would simply type:
!125
This saves you from having to retype the entire command and can be a real 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 have in your toolkit. From efficiently managing files and folders to navigating directories and recalling commands, these five tricks can significantly streamline your workflow and boost productivity:
**mkdir**with braces to create multiple folders in one go.**cd -**to quickly jump back to your previous directory.**touch**with a range to create multiple files at once.**tail -f**to follow log files in real-time.**history 5**to recall and re-execute recent commands.
These are just a few examples of the power that Linux commands can 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.
Always remember to conduct your own research and verify the information you encounter. Relying solely on others can result in outdated or incorrect practices. Take the initiative to implement and refine the code you find, ensuring it meets your specific needs and standards.
Photo by Rowen Smith on Unsplash
Final Words:
Thank you for taking the time to read my article.
This article was first published on Medium by CyCoderX.
Hey There! I’m CyCoderX, a data engineer who loves crafting end-to-end solutions. I write articles about Python, SQL, AI, Data Engineering, lifestyle and more!
If you want to explore similar articles and updates, feel free to explore my Medium profile:
Python Tips By CyCoderX
Join me as we explore the exciting world of tech, data and beyond!
What did you think about this article? Let me know in the comments below … or above, depending on your device! 🙃
Please consider supporting me by:
- Clapping 50 times for this story
- Leaving a comment telling me your thoughts
- Highlighting your favorite part of the story


