Improve your terminal experience and save more time by reducing the length of your Linux commands with these methods.
Linux commands are often known for being too long, and with excessive length comes increased complexity and comprehension issues. But there's always a way to shorten your Linux commands and turn them into the neat, concise strings that everyone loves.
Here are four ways to shorten the length of your commands so you can type less and do more within the Linux terminal.
Anyone familiar with the fundamentals of passing arguments to Linux programs would know that there are two different path expressions in Linux: relative and absolute paths.
Apart from being pleasing to look at, relative path expressions carry another benefit, and that is, they can do more with fewer characters. You can substitute absolute paths in your commands with relative path expressions and that alone would save you the trouble of typing unnecessary characters when specifying a file or directory path.
Also, absolute path expressions have an inherent requirement associated with them. You need to know about the entire Linux directory structure in detail to properly use absolute path names.
Consider you're currently inside folder2 in the following directory structure:
Now, if you want to navigate to the folder3 directory at the end of the hierarchy using absolute path, you'd type:
On the other hand, to point to folder3 while you're inside folder2 using relative paths, you'd type:
Using relative path expressions alone saved you the effort and time that you'd otherwise waste typing 19 characters. Although the savings might not seem significant at first, it'd be helpful in the long run.
Another great benefit of using the command-line interface rather than the GUI is that most Linux shells allow you to set command-line aliases, which are variables that act as a reference to another command whenever they're called in a shell.
Aliases are like string variables in programming languages. The variable name is usually short, but when you print its value, it can print out a hundred, or even thousands of words at once.
Therefore, if there's a lengthy command that you run quite often, you can set a much shorter alias for it to save time.
Consider you're working on a web-scraping project and need to frequently navigate to your project directory using the following cd command:
Typing it once is a cinch for sure. But what if you need to retype the same command a dozen times? Maybe 20, 30, or 50 times? The smarter choice would be setting an alias that acts as a replacement for the aforementioned command. You can do so using the alias command as follows:
Now when you type cdproj in the terminal, the shell would change the present working directory to the project folder.
To save the alias permanently, add the above-stated command to your shell configuration file, i.e. .bashrc, .zshrc, etc.
Most Linux shells make it easier for you to type out commands by offering you an auto-complete feature, similar to what you have on your smartphones.
When you're specifying the path to a file or directory, most of the time, you can hit Tab to auto-complete the path expression. Depending on the shell you're using, you can even auto-complete other Linux commands by pressing Tab.
Consider the /etc/folder1/folder2/folder3 directory structure as an example. If you're inside folder1 and want to change the directory to folder2, you can type "cd fold" and then immediately press Tab to let the shell complete the command for you.
Writing your own shell scripts for repetitive tasks can be helpful if you've got a series of commands that you run frequently. It's a lifesaver for programmers especially, who often need to compile and execute a program using a series of commands that operate on the source file.
Let's assume you've to run a group of commands multiple times during the day. This might include commands to commit changes to the project's central Git repository, move some files from one location to another, or simply change the name of the last modified file in a specific folder.
Instead of manually typing the commands in the terminal, you can write a shell script that automates this process for you. Consider you need to add and commit changes made to a project's source code using Git. You can use the following shell script to automate this:
Save the file as "changes.sh" and then run it using the following command, whenever you want to commit changes to the repository:
Before running the script, make sure to grant execute permissions to the file using chmod:
Linux tries its best to minimize the time and effort you spend working with the command line. Shell aliases, scripts, and the auto-complete feature make typing commands convenient for terminal dwellers so they can enjoy working in the shell.
Another such feature is the ability to access previously entered commands using command history. When inside a terminal, you can use the Up key to navigate through the command history and hit Enter to issue the command.
Let's assume you changed your present working directory to the project folder two hours earlier. You can keep pressing Up until you find the command you need. Then, simply press Enter to issue the command again.
You can only view and re-execute the commands that you ran in the current terminal session.
While the command line can be intimidating to newcomers at first, they quickly realize it's advantageous to use the terminal for performing both simple and complex operations, as it gives them more control over their system.
You can either choose to use your Linux desktop entirely using the graphical interface or learn more about the operating system and computers by familiarising yourself with the command line and its applications. The choice is yours!
Deepesh has a degree in Computer Applications and has been writing about technology for over five years. When not penning down informational guides on Linux, Windows, or Gaming, you can find him secluded in a corner reading books, playing FPS games, or searching for new hobbies to take up, only to quit and find a new one again.