9 time-saving tricks for your command line

Navigate your terminal like a productivity wizard

1. Create aliases

Every shell comes with a file called~/.bashrcor~/.bash_profile. Which one you’ll use depends on your system. This post explains it in detail.

Now think about commands that you use a lot. Typing them each time is arduous, and you can mess things up if you’re prone to making typos. That is what aliases are there for: They replace your original command by a shortcut.

For example, I have a Dropbox folder that I need to access quite often. As I don’t like typing too many characters, I have this alias in my~/.bash_profile:

alias topbox=“cd ~/Dropbox/top-eft/”

Some other useful aliases are:

alias mv=“mv -i"alias cp=“cp -i”

The option-iindicates that you’ll be prompted before you overwrite a file. If you’ve ever accidentally overwritten something important, you know what I’m talking about.

If you’re using a lot of aliases, it might make sense to create a separate file. Just pack all your aliases into a new file called~/.bash_aliases. Now, you need to tell your original configuration file where the aliases live. For that, add the following lines to the top of your~/.bash_profileor~/.bashrc:

if [ -f ~/.bash_aliases ]; then. ~/.bash_aliasesfi

And you’re done!

Whenever you’ve edited your bash file, make sure to run the relevant of the following two commands:source ~/.bash_profilesource ~/.bashrc

This way, you’re telling your system to adhere to your changes from now on. If you open a new window, it will source the file automatically.

2. Pimp your prompt

Your command line prompt is the first thing you’ll see when you start working, and probably the last thing you look at before you leave. So it makes sense to tailor it to your preferences.

I’m a minimalist, so I only include the current directory in my prompt. Therefore, in my~/.bash_profileI’ve specified:PS1='[\W]$ ‘Here, the\Wrefers to the current directory, and PS1 is the variable that refers to the prompt.

Other popular options are:

You can also print your prompt in colors. This could help when you’re generating lots of output (or error messages…) and you want to see where the program started.

3. Make the most of your history

Of course you don’t want to type the same commands over and over again. Of course there’s the tab completion — start typing a command, then hit tab to auto-complete it. But what if want to access your past commands? There are a few options:

4. Use your environment efficiently

You’ve already encountered a few environment variables —PS1,HISTSIZEandHISTFILESIZE. In general, these are variables written in CAPITAL LETTERS that define important properties of the system.

You can access the complete list of them with the set command. Another example (of many) isSHELLOPTS. It lists all the programs that are set to ‘on’ upon startup of your terminal session. A full documentation of the preset variables can be found in the GNU documentation.

  1. Profit from shell optionsYou can customize your shell in a number of ways with shell options. To display all options, run the following commands in your terminal:bash -Obash -o

The option-Orefers to options that are specific to the bash shell, while-orefers to all other options.

In the displayed list, you’ll also see whether an option is toggled on or off. You can change the default setting by adding a line in your configuration file. Some handy examples are:# Correct spellingshopt -q -s cdspell# Cd into directory with only its nameshopt -s autocd# Get immediate notification of background job terminationset -o notify

The first option listed here makes the shell more robust to typing mistakes. The second saves you from typingcdevery time you want to change your directory. And if you have a lot of background jobs running, you might want to get notified by adding the third option to your environment file.

6. Find files by name

Say you’re searching for the file foo.txt, but you have no idea where you’ve put it. Then from your home directory, type:find . -name foo.txt

Here, the . stands for the current working directory, and you specify the file name with the option -name. You can also use wildcards. For example, this command will return all files in txt format:find . -name *.txt

7. Search for files by content

Say you want to search for all occurrences ofbarin foo.txt. Then grep is your friend:grep bar foo.txt

If you want search multiple files, you can add them like this:grep bar foo.txt foo2.txt foo3.txt

And if you want to search for bar in all files of the directory dirfoo, you can use the recursive mode:grep -r bar dirfoo

For more options, you can check out themanualpage. Just hitman grepin your terminal.

8. Deal with lots of output

Sometimes you’ll want to grep for something, but the output is too lengthy to be printed on the screen. That’s where piping with the symbol|comes in handy.

Say for example that there are a thousand files in the directory dirfoo that contain bar. You might want to sift through the output and cherry-pick the files that interest you. Then you can type:grep -r bar dirfoo | less

This will show the output in a more digestible way. You can then take note of the files you want, and close the display by typingq. This will leave your command line interface uncluttered.

You can also usegrepin the inverse way. Say you want to run a programfooprogwhich produces a lot of output. But you’re only interested in the part that containsbar.

To make this output more understandable, you might want to add the three lines preceding each occurrence of bar, and the five lines following it. You can then type:./fooprog | grep -B3 -A5 -i bar

Here,-B3refers to the three lines before bar, and-A5to the five after it. This way, you can ensure that you only get meaningful information printed in your terminal.

9. Move around in text

You thought your terminal was all about keystrokes? Well, here’s one: you can alt-click in the middle of a line in your terminal. It’s a bit clunky, but it’ll be enough to impress your friends.What will save you tons of time, though, are keyboard shortcuts. To get started, I suggest you start adopting the following:

Story byAri Joury

Get the TNW newsletter

Get the most important tech news in your inbox each week.

Also tagged with

More TNW

About TNW

These are 3 of the hardest and 3 of the easiest programming languages to learn

Battle of the programming languages: Kotlin vs Java in the wake of AI

Discover TNW All Access

Tech bosses think nuclear fusion is the solution to AI’s energy demands – here’s what they’re missing

Could new programming language Mojo spark your career in AI and ML?