Master the Linux Command Line With These 6 Tricks

Master Linux Command Line TricksOne of the best ways that I know to truly embrace the command line is to have some fun with it. And what is more fun than learning about the available tricks and shortcuts that can be used?

Did you know that you don’t have to type in a complete file name, but that a single key-stroke could fill out the name for you?

Did you know that a single keyboard shortcut can clear your terminal screen of previous commands/results and other command-line clutter?

Did you know that every command that you type in is saved in command history, easily accessible with a few key-strokes if you need it again later?

These time-savers, and many others, are all ready and waiting for you. So let’s get started!

Tab Completion

Tab completion refers to the act of pressing the Tab key to auto-complete partially-typed commands, options, file names, directories, paths, etc.

The command will not be executed until you press Enter.

Tab completion will not work if there is more than one relevant completion option available. In this case, type another character (or two or three) and try again.

To view a list of relevant completion options, press the Tab key twice in quick succession.

Terminal History

Your terminal keeps a history of the commands that have been executed.

Use the Up arrow () to see the last command; it will show up in your current prompt, where you can either run it again (press Enter), or edit it and then run it again. Keep pressing the Up arrow to see the command history, one line at a time. (The Ctrl + P shortcut will do the same as the Up arrow.)

Use the Down arrow (as many times as you went up) to return to an empty command prompt. Alternatively, you can backspace, or use the Ctrl + C keyboard shortcut. (The Ctrl + N shortcut will do the same as the Down arrow.)

To view the history, type in history. The past commands will be listed, with a number next to each line. To execute a specific line from the history, type !, immediately followed by the line number to execute. Examples: !28 and !759

To display past uses of a command in your history, use history | grep command (where command is the command you’re looking for. Then pick the line number that you want, from among the results displayed, and prepend an exclamation mark to run it. (See examples above.)

To search for a specific command in your history, use the Ctrl + R shortcut and then start typing in part of the command and arguments. When the correct instance appears, press Enter.

To run the most recent command again, simply type !!.

To run the most recent command again, but this time with root privileges, simply type sudo !!.

To clear out your history and start fresh, type in history -c. (There’s no going back from this one!)

To keep your history from being logged (not recommended unless security calls for it, because history is cool!), type export HISTSIZE=0. This sets the number of commands to remember to 0.

Wildcards

The question mark ? is a wildcard, or placeholder, that represents a single character.

The asterisk * is a wildcard, or placeholder, that represents multiple characters.

Wildcards are used primarily in file names and extensions to indicate variations that allow for multiple files to be matched at one time.

For example, rm RandomFile.??? will delete all files with the specified name and a three letter file extension, while rm *.jpg will delete all .jpg image files regardless of their names.

As always, with great power comes great responsibility. Don’t get too curious and/or too lenient with wildcards! Used correctly, they’re invaluable, but you could modify or delete a lot of files (or file systems!) that you don’t mean to. Worst case scenario? Bye bye operating system! (And all the data in your file system.)

Redirection

The “greater than” operator > can redirect output to a file. For example: ll > directory-contents.txt

The “less than” operator < can redirect the contents of a file back into a program, or command. For example: wc -w < directory-contents.txt

Two “greater than” operators together >> can append output to a specified file. For example: ll >> directory-contents.txt

Piping

The vertical bar | is used as a pipe that feeds the output from one program directly into another program.

In other words, instead of executing a command and being presented with the output, as per usual, you can “pipe it” into another command to be handled by that other command.

The syntax is simple; anything on the left of the pipe will be piped into anything to the right of the pipe. Let’s look at some practical examples.

ls -al | tail -15 will display the last 15 results of the ls command.

ls | wc -w will display the word count of the results of the list command.

Clearing the Terminal

To remove all of the clutter that has accumulated on your terminal screen, type in clear, or use the Ctrl + L keyboard shortcut. Either option will clear off your screen by moving the session’s previous commands/results/etc. up out of sight; it is still accessible (during the session) if you scroll.

Leave a Reply

You have to agree to the comment policy.