Tricks for Programmers: Five Linux Commands After Which Your Life Won't Be the Same
The command-line interface is the alpha and omega for most techies, especially for developers and system administrators. Whatever you do—scripting, server configuration, or task automation—Linux commands will save you hours of work time. But only if you know how to use them correctly.
At first, Linux can be daunting for many, but once you get the hang of the command-line interface (CLI), you'll realize it's a programmer's best friend. Simple yet effective Linux commands can significantly improve any workflow, from navigating directories to manipulating files and even fixing bugs.
In this article, I'll tell you about five commands that make a developer's life easier. These aren't just handy tricks that are good to know. These commands will save you time and significantly increase your efficiency when interacting with the terminal.
Creating multiple folders using mkdir and curly braces {}
Developers often need to organize files and directories, especially when managing a project or setting up a complex directory structure. Usually, creating multiple directories requires typing many similar names. But there's a powerful trick: using the mkdir command with curly braces {} allows you to create multiple directories in one go.
What's the problem
Let's say for a project you need to create a folder structure for different environments (development, testing, and production) for several services. Here's what it looks like when you do it manually out of habit:
mkdir dev
mkdir test
mkdir prodThe more complex the structure becomes, the more tedious this task gets. For example, if you need folders for backend and frontend services in all three environments, the task becomes even more complicated.
Solution: mkdir with curly braces {}
Instead of creating directories one by one, you can combine everything into a single command using mkdir and curly braces {}. This is much more efficient because it creates a whole directory tree at once.
For example:
mkdir -p {dev,test,prod}/{backend,frontend}Let's break down this code:
- The
-pflag tells themkdircommand to create necessary parent directories (i.e., it protects you from an error in case the parent directories don't exist yet). - The curly braces
{}allow you to specify multiple options that are automatically combined in themkdircommand. - The first set
{dev,test,prod}creates directories for different environments. - The second set
{backend,frontend}creates directories for different services.
What's under the hood
When this command is executed, the following folder structure is created:
dev/
backend/
frontend/
test/
backend/
frontend/
prod/
backend/
frontend/What's the trick
This method significantly saves time and reduces the chance of human error, especially if you need to set up a large project with complex folder structures. All combinations can be specified in a single, concise command, rather than typing each one manually.
Go back using cd -, instead of repeating the cd .. command
Programmers often have to work with deeply nested directories. For example, you might be moving code, configuration files, or logs from the project root to subdirectories. And when you need to return to where you started, you have to manually type cd .. over and over. This process is annoying and can lead to errors.
What's the problem
Imagine you are working in a nested directory:
/home/user/projects/webapp/src/frontend/componentsTo return to the project's root directory, you usually have to enter cd .. several times:
cd ..
cd ..
cd ..
cd ..That's four commands just to get back to /home/user/projects/. You'll agree, it's tedious, and it's easy to lose count.
Solution: cd -
There is a simple and fast alternative: the cd - command. It instantly returns you to the previous directory, no matter how deep you've gone into the nested structure.
How it works
The cd - command doesn't move up the directory tree like cd .. does. It takes you back to the last directory you were in. It's almost like the 'Back' button in a browser.
Suppose you started in the project's root directory /home/user/projects/webapp/ and navigated to the nested directory /home/user/projects/webapp/src/frontend/components. Then you typed cd .. several times. But you can go back by simply typing:
cd -And you'll instantly land in /home/user/projects/webapp/.
What's under the hood
The cd - command remembers the previous directory, so it quickly switches you from the current directory to the previous one. It's very convenient for moving between two directories, especially when jumping between a working directory and a deeply nested one.
How to apply this in practice
This trick is especially useful when you're debugging or moving from one part of a project to another and back. If you need to switch frequently between the root directory and a deeply nested subdirectory, cd - saves you from typing the long file path or navigating to the desired directory step by step.
Quickly creating multiple files with touch and a number range
Developers often need to create multiple files, for example, when generating test files, placeholders, or logs. Creating one file is quick, but creating them one by one is tedious. Fortunately, Linux allows you to create multiple files with a single touch command and a number range in curly braces {}.
What's the problem
Suppose you need to create 100 numbered files for a series of test cases: test1.txt, test2.txt, and so on up to test100.txt. If you do this manually with separate touch commands, the whole process would look something like this:
touch test1.txt
touch test2.txt
touch test3.txt
...
touch test100.txtOf course, this is long and tedious. But there is a faster way.
Solution: touch with a number range and curly braces {}
To create many files in one go, use the touch command with a number range in curly braces {}. Here's how it works:
touch test{1..100}.txtWhat's under the hood
- The
touchcommand is used to create new files. We usually use it like this:touch filename.txt. - By adding a numeric range
{1..100}in braces, you tell the shell to create files numbered from 1 to 100. - As a result, a single command creates the files
test1.txt,test2.txt,test3.txt, all the way up totest100.txt.
Example
Let's take a look. We execute the command:
touch test{1..10}.txtThe following files are created in the directory:
test1.txt
test2.txt
test3.txt
...
test10.txtWhat's the trick
This method allows you to instantly generate multiple files instead of creating them manually one by one. It is most suitable for scenarios where placeholder files are needed for testing or logging, or for automating processes that require many files as input.
How to apply this in practice
With this trick, you can quickly generate log files for testing an application that processes many files. Create them with a single command instead of manually file by file. This frees up your time for writing and testing code.
Updating files in real-time with the tail -f command
Programmers often deal with logs, especially if they specialize in backend systems. Based on log data, developers look for errors, analyze system behavior, or debug. In this case, it's inefficient to manually go through logs over and over. Fortunately, the tail command automatically monitors updates in real time, showing only the most recent changes.
What's the problem
Suppose you are working on an application and you need to track new errors being logged to error_file.log. Usually, the tail command is used to read the last few lines of a file:
tail error_file.logWhile this gives you an idea of the current state of the file, this command doesn't update in real time. To see new lines with changes, you have to repeat the command. This is inconvenient, especially if you are troubleshooting a live system or monitoring event logs in real time.
Solution: tail -f tracks updates in real time
With the tail -f command, you will immediately know about changes in the log. You won't have to repeat the command to see if new lines have appeared in the file.
tail -f error_file.logHow it works
- The
tailcommand shows the last 10 lines of a file. - The
-fparameter stands for 'follow'—in this case, the terminal stays open and continuously displays new lines of the file as they appear.
This is especially useful when monitoring a log while an application is running, as error messages or results can be seen in real time.
Example
Let's say you're monitoring incoming requests and errors in a web server's log:
tail -f /var/log/nginx/access.logAs new requests come in, you will see them immediately in the terminal. No need to refresh or repeat the command—it continuously tracks updates in the file.
How to apply this in practice
The tail -f command is very convenient to use during debugging. Imagine you are trying to find the cause of a recurring problem in an application. By monitoring the error log in real time, you can immediately notice error messages and take prompt action.
For example, you can run the command:
tail -f /var/log/app/error.logIf the application crashes, you will immediately see an error message pop up and can quickly fix everything.
Life Hack: Combine tail -f with grep
If you want to monitor a log in real time, but are only interested in specific entries, you can combine tail -f with grep. For example:
tail -f error_file.log | grep ERRORThis command only shows lines containing the word ERROR, allowing you to filter the necessary information from the log results.
Viewing recent commands with history 5
Developers often have to repeat certain commands, for example, when compiling code, working with files, or restarting services. It's not easy to remember the syntax of a command you've already executed or the actions you've taken, especially if you've been working in the terminal for several hours.
Fortunately for us, Linux tracks every command during a session. The history command shows a list of executed commands, and using it can save you time and effort.
What's the problem
Suppose you've been working for hours and have executed a complex grep or find command. And you know you'll need to use it several times. It's hard to remember the exact syntax, so you have to spend time manually searching for it in the terminal.
Solution: history 5, which remembers the last commands
The history command shows a list of commands previously executed in the current shell session. By default, it shows the entire command history, but you can limit the number of commands displayed.
For example:
history 5This command will show the last five commands you executed, in case you need to quickly find and repeat them.
How it works
historyby itself outputs the entire command history for the session.- If you add a number (e.g.,
history 5), you limit the list to five commands, allowing you to focus on the most recent tasks you've been working on.
For example, if the last five commands are:
123 ls
124 cd src/
125 mkdir newdir
126 touch newfile.txt
127 history 5After executing the history 5 command, you will see:
123 ls
124 cd src/
125 mkdir newdir
126 touch newfile.txt
127 history 5Why this trick is useful
history saves you from having to start your search from scratch if you've forgotten which command you executed. It's also handy for remembering complex commands with long flags or parameters that you don't want to type from memory.
How to apply this in practice
Let's say you're debugging a program and running several different commands to compile, check logs, and restart a service. Now you don't need to memorize each command or scroll back through the terminal to find the right one. You can simply run the command:
history 5You will see a list of the last five commands and can repeat any of them with minimal effort.
Life Hack: Re-executing commands
You can quickly repeat any command from your history using an exclamation mark (!) followed by the command number. For example, to execute the mkdir newdir command above (number 125), you just need to type:
!125You don't need to type the entire command. This trick really saves time, especially if you have to work with long commands.
Summary: Five Linux commands to optimize your workflow
mkdirwith braces creates multiple folders in one go.cd -quickly returns to the previous directory.touchwith a number range creates multiple files at once.tail -fallows you to monitor changes in logs in real time.history 5recalls the last commands for re-execution.