SEARCH

How-To Geek

Become a Linux Terminal Power User With These 8 Tricks

bash tricks header

There’s more to using the Linux terminal than just typing commands into it. Learn these basic tricks and you’ll be well on your way to mastering the Bash shell, used by default on most Linux distributions.

This one’s for the less experienced users – I’m sure that many of you advanced users out there already know all these tricks. Still, take a look – maybe there’s something you missed along the way.

Tab Completion

Tab completion is an essential trick. It’s a great time saver and it’s also useful if you’re not sure of a file or command’s exact name.

For example, let’s say you have a file named “really long file name” in the current directory and you want to delete it. You could type the entire file name, but you’d have to escape the space characters properly (in other words, add the \ character before each space) and might make a mistake. If you type rm r and press Tab, Bash will automatically fill the file’s name in for you.

Of course, if you have multiple files in the current directory that begin with the letter r, Bash won’t know which one you want. Let’s say you have another file named “really very long file name” in the current directory. When you hit Tab, Bash will fill in the “really\ “ part, since the files both begin with that. After it does, press Tab again and you’ll see a list of matching file names.

tab completion

Continue typing your desired file name and press Tab. In this case, we can type an “l” and press Tab again and Bash will fill in our desired file name.

This also works with commands. Not sure what command you want, but know it begins with “gnome”? Type “gnome” and press Tab to see a list.

Pipes

Pipes allow you to send the output of a command to another command. In the UNIX philosophy, each program is a small utility that do one thing well. For example, the ls command lists the files in the current directory and the grep command searches its input for a specified term.

Combine these with pipes (the | character) and you can search for a file in the current directory. The following command searches for the word “word”:

ls | grep word

piping

Wild Cards

The * character – that is, the asterisk – is a wild card that can match anything. For example, if we wanted to delete both “really long file name” and “really very long file name” from the current directory, we could run the following command:

rm really*name

This command deletes all files with file names beginning with “really” and ending with “name.” If you ran rm * instead, you’d delete every file in the current directory, so be careful.

wild card

Output Redirection

The > character redirects a command’s output to a file instead of another command. For example, the following line runs the ls command to list the files in the current directory and, instead of printing that list to the terminal, it prints the list to a file named “file1” in the current directory:

ls > file1

bash tricks header

Command History

Bash remembers a history of the commands you type into it. You can use the up and down arrow keys to scroll through commands you’ve recently used. The history command prints a list of these commands, so you can pipe it to grep to search for commands you’ve used recently. There are many other tricks you can use with Bash history, too.

history

~, . & ..

The ~ character – also known as the tilde – represents the current user’s home directory. So, instead of typing cd /home/name to go to your home directory, you can type cd ~ instead. This also works with relative paths – cd ~/Desktop would switch to the current user’s desktop.

Similarly, the . represents the current directory and the .. represents the directory above the current directory. So, cd .. goes up a directory. These also work with relative paths – if you’re in your Desktop folder and want to go to the Documents folder, which is in the same directory as the Desktop folder, you can use the cd ../Documents command.

characters

Run a Command in the Background

By default, Bash executes every command you run in the current terminal. That’s normally fine, but what if you want to launch an application and continue using the terminal? If you type firefox to launch Firefox, Firefox will take over your terminal and display error messages and other output until you close it. Add the & operator to the end of the command to have Bash execute the program in the background:

firefox &

background process

Conditional Execution

You can also have Bash run two commands, one after another. The second command will only execute if the first command completed successfully. To do this, put both commands on the same line, separated by a &&, or double ampersand.

For example, the sleep command takes a value in seconds, counts down, and completes successfully. It’s useless alone, but you can use it to run another command after a delay. The following command will wait five seconds, then launch the gnome-screenshot tool:

sleep 5 && gnome-screenshot


Do you have any more tricks to share? Leave a comment and help your fellow readers!

Chris Hoffman is a technology writer and all-around computer geek. He's as at home using the Linux terminal as he is digging into the Windows registry. Connect with him on Google+.

  • Published 04/3/12

Comments (16)

  1. Jason

    Very useful. Some I already knew. Some I didn’t such as && and &. Thanks.

  2. MJ

    “…the Documents folder, which is in the same directory as the Documents folder…” Second folder should be “Desktop”. Also, typing cd alone will change to home, no need for the ~ (but cd ~ works great as an example).

    Great article, I will share this with some mates that will need to use the Terminal in order to use OpenFOAM. Keep all this Linux stuff up!

  3. Cam2644

    More useful info here. Thanks

  4. KrazyKyngeKorny

    I want to make a sh file that will run several files simultaneously. I use several graphics programs togetyher. I want a command that will start them all at once.

  5. sikedestroya

    Grep is wonder – maker command… Very, very useful… Thanks for sharing those tips!

  6. Daniel

    For the record, you can just do “cd” with nothing else to go to your home dir. No need to use a ~ or anything.

  7. bob

    @KrazyKyngeKorny

    #!/bin/sh

    /path/to/prog1 &
    /path/to/prog2 &
    /path/to/prog3 &

  8. andriajohn

    Very useful.Thanks for sharing the information.

  9. sverre

    cd – will take you back to your previous directory (one step back)

  10. Qrazydutch

    Like this…

  11. Justin

    Ctrl-r will allow you to search your recently executed commands.

  12. Chris Hoffman

    @MJ

    Oops, thanks for catching that. It’s fixed!

  13. jamesjoseph

    Pipes allow you to send the output of a command to another command

  14. sudobash

    Instead of && you can use ; This makes much more sense to any c programmers out there and it seems to be used more in (ba)sh scripting.

  15. Caleb

    @sudobash:
    Using ; is not the same as &&.
    “cd ../ && ls” – executes ls only if cd ../ is successful.
    “cd ../; ls” – executes ls after cd ../ is done, whether the cd ws successful or not.

  16. Dabheid

    Cheers Chris, as a noob Linux (dual boot) user, I’m always appreciative of all and any Linux related info like this. :)

Enter Your Email Here to Get Access for Free:

Go check your email!