Some Useful Linux Terminal Commands

I am not one of those die-hard Linux users who goes out of his way to do everything from the command-line. Nonetheless, I use the terminal on a daily basis. Throughout the years I have learned some useful tricks, and today I want to share some with you.

Note: I use Fish as my shell, which is uncommon. Most Linux systems use the Bourne Shell. Fish does not use the same syntax. Therefore I try to present these tips and tricks in ways that will work under the most common shells.

Making Directories

Linux users quickly learn the mkdir command. But if you want to make a directory with one or more sub-directories you will run into a problem. This command will fail: mkdir foo/bar/baz. The solution? Simply add the -p flag. So you can use mkdir -p foo/bar/baz to create that directory structure all in one swoop.

Changing File Extensions

The most common way to rename a file in Linux is to use mv, the ‘move’ program. The syntax is simple: mv filename.old But it is tedious if the filename is long. Instead of spending the time typing the name twice—which can also be the source of mistakes via typos—you can use brace expansion to avoid the duplication, a feature that many shells support. Here is an example:

$ mv SomeLongFilename.{txt,rst}

Shells that support brace expansion will turn the above command into this:

$ mv SomeLongFilename.txt SomeLongFilename.rst

You can use brace expansion anywhere, and with more than two options, e.g.

$ cat {foo,bar,baz}.txt > collection.txt

Keep your eyes open and you will find a lot of convenient uses of this feature.

RAR Archives as Images

Speaking of the cat command, you can use it to pass around archives in the RAR format as if they were images. You just need an image like a PNG. Then you can do this:

$ cat image.png file_archive.rar

Save the output as an image and send it along. To un-archive the files you can simply open the archive with a RAR program. I admit this has limited use. The only time I’ve used this trick is so that I can upload archives onto sites that accept image uploads but not RAR files.

Delete Empty Directories

Want to delete every empty sub-directory from the current directory you’re in?

$ find . -type d -empty -print0 | xargs -0 rmdir

Few Linux commands have the flexibility of find. It creates a list of files based on all manners of criteria and then lets you filter that list into other commands. The command above fits into this typical pattern:

$ find . [criteria] -print0 | xargs -0 [command]

In this example the criteria are:

-type d
Show only files of type d, i.e. directories.
Show only directories which are empty.

The flag -print0 and the corresponding -0 flag for [xargs][xargs] ensures that this command will work with directories that have whitespace in their names. The net result is that the names of all empty directories are given to rmdir. This is an example of the common idiom of piping output from find into a combination of xargs and another command in order to execute that command once per each file that meets the criteria of find.

Delete Difficult-to-Type Files

Another use for find is to delete files that have names which are difficult to type, perhaps because they have odd control characters or use a foreign language which you cannot type due to input settings on your computer. Fortunately files have other identifiers than their names. Every file on (most) Unix-based systems have an index node, more often called an inode. Every file has a unique number which you can use in place of its name.

$ ls -il
$ find . -inum [NNNNN] -delete

The first command above shows the inode numbers for every file. Take the number for the file you want to delete—often a large number in the first column of output—and use it in conjunction with -inum in order to delete that file.

Selecting a Column of Output

If you want to rip out a column of information from the output of a command then you could use cut. It is the standard tool for this purpose. However, if you are comfortable with regular expressions then you can use the grep in conjunction with its -o flag to select the column you want. For example, if you to show only the inode numbers for files you can select that column like so:

$ ls -il | grep -Po '^\d+\b'

Note: This example uses the -P flag to enable the use of Perl compatible regular expressions.

Process File Descriptors

Unix-based systems also use file descriptors to name files, or more accurately things which name a source or destination of data, e.g. the terminal. Every running program has access to three file descriptors with these numbers:

Standard Input
Standard Output
Standard Error

The /proc file-system gives you access to all file descriptions for any process. For example, tail -f /proc/9274/fd/2 would show you anything written to Standard Error by the program with the process identifier (PID) 9274. But you probably do not know the PID of the program you want to monitor. The ps command will show you the PID for every process, which you can use as part of the /proc/… path.


Consider this article the first part of a series. There are a lot of other useful things you can do from the command-line. These are just some tricks that I find useful, but the list is nowhere close to exhaustive. In fact, I could (and eventually will) devote an entire article to /proc and its functionality.

Until then I hope you get some use out of these tips.


Add Your Thoughts

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s