A look at some useful Linux commands

command line 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

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 …

ps

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.

ps in action

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.

ps returning everything

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.)

cal

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.

cal in action

Run the command cal –3 to view the calendars for last month, this month, and next month.

cal 3 month view

To view the calendar for a specific year, type cal followed by the year – for example, cal 2013.

Looking ahead

cat

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

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.

Photo credit: By ZxxZxxZ (Own work) [GPL], via Wikimedia Commons

This work, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

flattr this!

  • Pingback: Links 7/8/2012: OpenGL 4.3, Nautilus Forked | Techrights

  • ViruSzZ

    Just a FYI, you have a useless use of `cat` there. Instead of doing `cat file | less` you can directly use `less /path/to/file`.

    take care bro,

    – d

  • Scott Nesbitt

    Just because you find it useless doesn’t mean others will. Having said that, there’s nothing wrong with using less. In fact, I have a whole post in this space covering that.

  • Ridgeland

    Thanks.
    I just created an icon on the panel that runs:
    gnome-terminal -e /Data/bin/Calendar2012.sh –geometry=70×40 –title “2012 Calendar”
    That *.sh file is just:
    #! /bin/bash
    # Calendar2012.sh
    cal 2012
    read -n 1 -p “Hit any key to exit…”
    exit

  • http://writtenandread.net M. Juhl-Johansen Zölde-Fejér

    Well, it is useful as an example of pulling text out and sending it to something else.

  • http://writtenandread.net M. Juhl-Johansen Zölde-Fejér

    Did you ever consider doing a piece on BASH history? That is a very useful thing to have, and a lot of people do not realise how much you can use again.

  • Scott Nesbitt

    Actually, I hadn’t considered that. But it’s on the list now. Thanks for the idea! And I’ll credit you with that idea when I write the post.

  • Scott Nesbitt

    Nice!

  • http://writtenandread.net M. Juhl-Johansen Zölde-Fejér

    To be honest, I have been thinking of writing up something for the beginners, but I never got around to it. I use command history numbering a lot.

  • Scott Nesbitt

    Well, if you do write it up let me know. I’d like to read the post!