Getting started with Linux commands

Share This article

Whether you’ve been inspired to try and add functionality to your home NAS, run commands on your rooted smartphone, or experiment with one of the powerful new desktop environments like Ubuntu, you’re likely to have come across the need to interact directly with Linux using its command line. If you’re new to Unix-like commands, they can be pretty inscrutable. Fortunately, by learning just a few of them you can find your way around.

The first thing to know about Unix, and now Linux, is that its authors didn’t like to type much. Commands tend to be short abbreviations for the longer versions you might find when using the command line on Windows, for example. cat is short for concatenate, ps for process, ls for list, and so on. Options are treated similarly, with “-a” being a common synonym for -all, etc. Fortunately, in many cases Linux has also added longer, human-readable, versions of command-line options as alternates for the shorter, harder to read, versions. So “–all” can be used in instead of “-a” for example.

You’ll also need to remember that Linux, unlike Windows and DOS, is case-sensitive when it comes to commands, so “ls” is not the same as “Ls” or “LS”. Likewise, “-d” as an option means something different than “-D”.

Navigating in Linux

When you first login to a Linux system, you’re in your home directory. You’ll be greeted with a (system dependent) prompt and white space. To see where you are and what’s there you can use:

pwd — Prints the Working Directory. You can tell how old some of these commands are, as the word print refers to the original paper teletype terminals used with Unix systems for most of the 1970s.

ls — List. Prints out a list of files in the current directory. If you want to list a different directory, use ls <pathname>. By default ls only provides the name of files in the current directory, as well as ignoring “hidden” files (those with a name beginning with a period [.]). You can change the default behavior by using ls -l to print information about each file or ls -a to include hidden files. Typical of Linux commands, you can simply add the two options together if needed, so ls -la will print out information about all the files, including hidden ones, in the current directory.

Almost all commands in Linux follow this same command line structure. The name of the command is followed by one or more options, also called flags, and then by the arguments to the command. Options are normally not required, and many commands have a default argument if none is supplied. ls, for example, has a default argument of the current directory — which can also be referred to with a single “.” So “ls” is the same as “ls .”

Another useful option for ls is “-t” to sort the output by time instead of by name. Conveniently for PC users, “dir” is typically an alias for ls.

There are many other options to ls, so now is probably a good time to mention the man command — short for manual. Typing man <commandname> will get you a page by page document describing the command you’re trying to learn. It will include sections on options, arguments, usage suggestions, and related commands. Once you are looking at the output of the man command, pressing space gets you the next page, and pressing “q” exits the display. You can also append “–help” to almost any command to learn more about it, for example by typing ls --help for information on the ls command.

cd — Change Directory. Simply changes your current directory to the location you specify. For example cd /usr/bin would change your working directory to one of the common command directories. Remember that in Linux that when you want to specify a directory path, the separator is “/” (forward slash), instead of the “\” (back slash) used in DOS & Windows.

Working with files

Linux command "cheat sheet" courtesy of FOSSwire. You can click on it to get a full-size PDF to print out.

Now that you can find your way around, you probably want to work with some of the files you’ve found.

cp — Copy. Copies one or more files or directories from one path to another. So cp original_file copied_file will copy the file named original_file in the current directory to one named copied_file. The cp command is quite powerful, with the “-r” option making it work recursively on an entire tree of directories and files. If the source is a file and the destination is an existing directory, the source file will be copied into that directory.

mv — Move. Just like cp, except it moves the file or directory, assuming you have permission to both create the new copy and delete the original. This command is also used in place of the DOS “rename” command.

Since Linux filenames can be pretty long, typing them can become painful. Fortunately, Linux allows you to use wildcards to pattern match filenames. (This is technically a property of the command line interpreter, called the shell, but it acts like it is part of Linux itself). The “*” character matches any sequence of characters, while “?” matches any single character. So “ls *” will list all the non-hidden files in the current directory. Likewise mv * output_directory will move all files in the current directory to a directory named output_directory if it exists.

There are many Linux editors. Depending on your distribution you may have one or more of pico, nano (similar to pico), vi or vim (an improved version of vi). Each one has its own command set.

Tagged In

Post a Comment

I just set up my first Ubuntu server so this showed up just in time for me. Thanks.

Christopher Williams

> while “.” matches any single character.

Actually, the “?” character matches any single character. It looks like you’re confusing regular expressions with filename globbing.

http://www.cardinalphoto.com David Cardinal

Christopher–Color me embarrassed. I’ve only been typing into the shell (various shells, really) since 1977, so I can only blame a synapse misfire for that one. I can possibly attribute it to the fact that I typically type “ls .??*” to find the . files in a directory. Especially sinful since several paragraphs before I state correctly that . is shorthand for the current directory!

http://www.mrseb.co.uk/ Sebastian Anthony

Thanks — have fixed!

http://profiles.google.com/baracuda68 Bob L

” its authors didn’t like to type much”I believe it was more of the fact that memory was SOOOO EXPENSIVE back in the ’70’s, that memory had to be saved down to the bit. Correct me if I’m wrong…

http://www.cardinalphoto.com David Cardinal

Bob–I’ve heard a lot of different stories as the reason for the short commands. By the time I started using Unix in 1977 they were certainly already enshrined. The most plausible (to me) is that the original telex terminals were really painful to type on, so even good typists wanted to minimize keystrokes.

I’ve also heard slow modem speeds being cited, lack of memory (although I’m not sure that command length is all that crucial, but on a 24K system, every byte would certainly help), rumor that Ritchie couldn’t type, Multics heritage (many of the Unix commands are the same as the Multics abbreviations for the same command), etc.

Use of this site is governed by our Terms of Use and Privacy Policy. Copyright 1996-2015 Ziff Davis, LLC.PCMag Digital Group All Rights Reserved. ExtremeTech is a registered trademark of Ziff Davis, LLC. Reproduction in whole or in part in any form or medium without express written permission of Ziff Davis, LLC. is prohibited.