Files
second-brain/Clippings/5 Linux Command Tricks That Will Change Your Life as a Programmer.md
T

18 KiB
Raw Blame History

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
CyCoderX
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.
clippings

Boost your productivity with these powerful Linux commands for developers!

[

CyCoderX

](https://cycoderx.medium.com/?source=post_page---byline--825a4524218a--------------------------------)

[

Level Up Coding

](https://levelup.gitconnected.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), youll realize its a programmers 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, well explore five command-line tricks that can make your life easier if youre a developer. These arent just “nice-to-know” shortcuts — theyre game-changers that will save you time and make your interactions with the terminal far more efficient.

Lets dive in!

Id be grateful for your support with a clap! If youre passionate about Python, SQL, Data Engineering and Data Science, consider following me for more content.😊

CyCoderX

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, theres a powerful trick with the mkdir command that allows you to create multiple directories in one go using braces {}.

The Problem

Lets 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}

Heres a breakdown of whats happening:

  • The -p flag tells mkdir to create parent directories as needed (i.e., it wont throw errors if the parent directories dont exist yet).
  • The curly braces {} allow you to specify multiple options that mkdir will 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 youre 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, its common to find yourself working within deeply nested directories. For instance, you might be moving from your projects root directory into subdirectories for code, configuration files, or logs. However, when its time to return to where you started, navigating back manually by typing cd .. multiple times can become frustrating and error-prone.

The Problem

Imagine youre 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 ..

Thats four separate commands just to get back to /home/user/projects/. Not only is this tedious, but its also easy to lose track of where you came from.

The Solution: cd -

Heres a simpler, faster alternative: use the cd - command to instantly return to your previous directory, no matter how deep youve gone into a nested structure.

How It Works

  • The command cd - doesnt navigate up the directory tree like cd ... Instead, it takes you back to the last directory you were in, almost like a "back" button in a web browser.

For example, lets say you started in your projects 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 youll be instantly back in /home/user/projects/webapp/.

Whats 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. Its a highly efficient way to navigate back and forth between two directories, especially when youre jumping between a working directory and a deeply nested one.

Practical Use Case

This trick is especially useful when youre debugging or working on multiple parts of a project. If you need to frequently switch between the projects 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 youre 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 theres 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

Whats Happening Here

  • The touch command is used to create new files. Normally, youd 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 to test100.txt—in a single command.

Example

Lets 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. Its 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, youll 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

Lets say youre 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 doesnt update in real-time. If the file changes, youll 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

Whats Happening Here

  • The tail command by itself shows the last 10 lines of a file.
  • The -f option 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

Lets say youre monitoring a web servers log file to see incoming requests and errors:

tail -f /var/log/nginx/access.log

As new requests come in, youll see them displayed live in your terminal. Theres 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 youre trying to find the cause of an intermittent issue in your application. By following the applications 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, youll 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 its 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 youve 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

Lets say youve been working for a while and you ran a complex grep or find command that you now need to reuse. You cant 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 youve 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.

Whats Happening Here

  • history alone 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 youve 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 youve forgotten how you executed a command, history saves you from having to figure it out all over again. Its also great for recalling complex commands, such as those with long flags or options that you dont want to retype from memory.

Practical Use Case

Lets say youre 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

Youll 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 weve 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! Im 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:

CyCoderX

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:

  1. Clapping 50 times for this story
  2. Leaving a comment telling me your thoughts
  3. Highlighting your favorite part of the story