Americas

  • United States
sandra_henrystocker
Unix Dweeb

Easy command line Linux tricks

How-To
Jan 30, 20245 mins
Linux

Linux tricks can help you get work done faster, especially when they're easy. Here are some ways to find files, reuse prior commands, stop processes and more.

Typing at a computer
Credit: Shutterstock

In this post, we’ll take a look at a series of basic Linux command tricks. I’ve included short and easy descriptions and some example commands to try. All of these are very useful and can be easily put into use, whether you need them frequently or only from time to time.

Moving around the file system

The cd command (one of those most basic Linux commands) followed by the name of any directory takes you there as long as your access permissions allow. To move back into your home directory from anywhere in the file system, simply type cd without any arguments (cd). To move up one directory, use the command followed by two dots (cd ..). To return to whatever directory you were sitting in prior to your moving to your current location, use the cd command with a dash (cd -). Note that if you use this trick a number of times in a row, you will move back and forth between the two locations rather than backing up further and further.

Working with files

You can look at text file content using the more command (more myfile), examine the ends of those files with commands that pipe output to the tail command (tail -f filename), and examine them a screenful at a time with commands like less (less logfile).

You can empty a text file simply using the > symbol (> oldfile) or update its most recent update date/time using just a touch command (touch myfile).

Reusing prior commands

You can list the commands you have run previously with the history command, or show a screenful at a time by piping the output to more (history |more). If your history buffer saves 1,000 or 5,000 commands, as most do these days, that’s a bit too much to browse through. Instead, you can use the up arrow to move back through prior commands (starting with the most recent) one at a time. Press return when you reach the one you want to rerun.

You can also pipe the output of the history command to the tail command to see only a small range of the most recently entered commands (history | tail -20).

You can always repeat whatever command you most recently entered simply by using two exclamation points (!!). To display or reuse your previous command’s final argument, refer to it as !$ (echo !$).

How to source files on the Linux command line

If you want to avoid adding every command you enter to your history buffer, that’s easy. Just start commands you don’t want remembered by first pressing the space bar on your keyboard and then typing the command. For example, enter an echo command preceded by a blank character (“ echo hello”). Then look at the last lines saved in your history buffer and you’ll notice it isn’t included. This will save you time when you need to review or reuse prior commands since you won’t have to look through so many commands to find those you want to rerun.

You can also rerun a particular command by typing an exclamation point followed by enough characters to identify the command that you want to rerun (!tail).

You can also pipe the history command’s output to grep to list only those commands in your history buffer that contain the particular command (history | grep reset).

Finding files by various criteria

The find command can help you find files by name, content or other criteria. A command like “find . -mtime -10” will find files starting in the current location that are less than ten days old. A command like “find . -name fort*” will find any files that start with “fort”.

Controlling your terminal window

You can freeze and then unfreeze your terminal window using ^s (freeze) and ^q (unfreeze). Note that the ^s string means “hold the Ctrl key and press an s” while ^q means “hold the Ctrl key and press a q”. In a similar manner, you can move to the beginnings and to the ends of lines using ^a and ^e or clear the current line with ^u.

You can clear your screen any time you want – helpful when you’re ready to refocus on a new issue – simply by typing clear.

Stopping processes

You can stop a command that you just started by using the ^c command (hold Ctrl key and press c), kill a command that’s running independently of your terminal (in the background) by using the kill command followed by the process ID (kill 2604), and kill a running process by name using pkill (pkill myloop).

Getting help

Don’t forget to check out command man pages from time to time to see more of the available options or to try the –help option (pkill –help) to get a fairly quick explanation of the options.

Making life on the command line easier

In addition, try turning some of the more complex commands you need to use into aliases to make then easier and avoid typos by adding them to your .bashrc file (alias recent=”history | tail -10″).

Wrap-up

This post provides a series of how-to tips for commands that you’re likely to find very helpful and easy to use.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author