As useful as I find the Linux command line, there are more than a couple of commands that I use rarely. But when I do use them they definitely come in handy.
Some (maybe more than some) power users will balk at my choices – I know several who use these commands regularly. But this post isn’t for them. It’s for the rest of us.
Let’s take a look at these five commands, what they can do, and how to use them.
Note: This post isn’t an exhaustive look at all the options available for each command. I’m just going over the basic usage, which should be enough for most people.
pwd is short for print working directory. Its purpose is simple: to let you know where you are in the file system. That might seem odd. Why? When you’re at the command line, the prompt usually include the path of the directory you’re in – for example, /home/scott. But I’ve worked on systems where this wasn’t the case. And sometimes, when I went off for an hour or two, and then came back I couldn’t remember where I was. Sort of like losing your place in a book.
pwd helps get around that. To use it, just type pwd. It will return the full path of the directory that you’re in, for example /usr/share/texmf/tex/latex/ltxmisc. You’re lost no more …
Ever wonder what processes and programs are running on your system? Or do you need to stop an application that’s hung using the command line, and don’t know which process it is? Well, that’s where ps comes in.
You can run ps on its own, but it will only show you two processes: your shell (in my case, bash) and ps.
If you want more detail, you can run ps -ef. This returns a list of all the running processes. That’s probably a bit too much for you, though.
To get a listing of only the processes you’re using, type ps -u your_name – for example, ps -u scott.
Notice the information that the command returns (from left to right): the numeric ID of the process, the name of the terminal that you’re logged into, the amount of time that the process has been running, and the name of the command associated with the process.
(As a quick aside, if you want to kill a process, just type kill followed by the numeric ID of the process.)
Every so often, I need to check a date either in the past or in the future. That could be to determine the day of week on which the date lands or just to see how long it is before I go on a trip or have a day off. At the command line, you can do that with cal.
If you type cal, a calendar for the current month (with today highlighted) appears.
Run the command cal –3 to view the calendars for last month, this month, and next month.
To view the calendar for a specific year, type cal followed by the year – for example, cal 2013.
cat is one of those nifty commands that does several useful things. I only use it to do two useful things: viewing and combining text files.
Let’s say you have a text file called class_notes.txt. To view it at the command line, just type cat class_notes.txt. That’s all well and good, but the problem is that cat will display the whole file and stop at the end. No one can read that quickly. To get around that, you can combine cat with the less command:
cat class_notes.txt | less
All you need to do is press the space bar on your keyboard to move through the file one page at at time.
What about combining files? Let’s say you have a set of class notes with file names like class_notes–02092012.txt, class_notes–02162012.txt, and class_notes–02232012.txt that you want to combine into a single file named class_notes-Feb2012.txt. Just type this command:
cat class_notes–02092012.txt class_notes–02162012.txt class_notes–02232012.txt > class_notes-Feb2012.txt
You wind up with a single text file.
mkdir creates directories. Sure, you can do that in a file manager, but if you’re at the command line why waste time jumping to your file manager? mkdir is faster, by far.
To create a directory, type mkdir directory_name – for example, mkdir Backups. You can, of course, create directories with longer names and which contain dashes and underscores.
What happens if you want to create a directory whose name consists of multiple words and has spaces between those words? Just surround the name with quotation marks – for example, mkdir “Word Processing Templates”.
Like all Linux commands, mkdir is case sensitive. Typing mkdir Backups and mkdir backups won’t create the same directory; you’ll wind up with one directory named Backups and another named backups.
Are there any Linux commands that you find useful, but which you infrequently use? Feel free to share your picks by leaving a comment.
This work is licensed under a Creative Commons Attribution 4.0 International License.