Some people still have a silly notion that to use Linux you must know Linux shell commands and syntax like the back of your hand. What nonsense! With any modern Linux desktop distribution, you no more need to use Linux commands like ps, grep, or ls than you need to use DOS commands in Windows today.
Except, of course, just as in Windows, every now and then it is helpful to use the good, old ASCII command-line instructions.
Mind you, 99% of the time desktop Linux users are fine with Linux desktops such as Cinnamon, GNOME, Unity, or KDE. For system administrators, it's a bit different. Linux sysadmins use shell commands all the time.
But, once every blue moon, its handy to know some Linux command basics, so here's what you need to know.
To get a Linux shell, you need to start a terminal. That terminal, in turn, runs a Linux command shell. There are many such shells, include csh, Bourne shell, and the Korn Shell. On most Linux distributions today, the default shell is the Bourne again shell (bash). (Yes, Linux users have long loved puns.)
To see what shell you have, run the following command:
That $ sign? In Linux, the dollar sign stands for a shell variable. These are variables that the shell, and any shell program, can use. Some are set as default when you start your Linux PC. For example, $SHELL; $LOGNAME is your login name; and $PATH identifies the directories in which your shell searches for commands.
The echo command just returns whatever you type in. If what you type in has a particular meaning to the shell, such as the shell variables, it returns the variable's value.
To expert users and anyone writing shell scripts, the shell you use is important. For a Linux desktop user who just needs to go to the shell every now and again, it doesn't really matter.
What does matter is that you use the correct text case. For example, ls, Linux's version of MS-DOS' dir command, gives you a listing of the files in your current working directory. LS just gives you a “command not found” error message.
Another important thing to know about the Linux shell is that you can string together commands. Indeed, that's been one of Unix/Linux's neatest features since its first days. The simplest way to chain commands is with the pipe, often found above the backslash on your keyboard |. The pipe takes the output of your first command and uses it for the input for your next command.
Linux commands have their own syntax, just like the English grammar you tried not to learn in 9th grade. Unlike your freshman English teacher, Linux has no forgiveness whatsoever for mistakes. If you get a command wrong, you won't flunk or damage anything, but it won't work.
The basic syntax is:
command -option file
So, for example,
shows the “long” version of “all” file names in the present working directories.
That could give you a lot of files spilling down the screen. So, it's time to put pipe to work:
ls -la | more
This takes that stream of files and directories and lets the more command display it one screen at a time.
You can also use most Linux commands with wildcards. Wildcards are characters that can stand-in for unknown characters in file names. For example, you can use the * wildcard to represent any string of characters. The asterisk (*) matches any number of characters; the question-mark (?) matches a single character. So, for instance:
ls -l a*
returns all the long version of the files and directories in your present working directory that have names which start with a lower-case a: abc.txt, alphabetsoup.jpg, albatross.zip. The command
returns all file and directories names that started with a, then any other character, and had cd for its last two characters: abcd, axcd, but not albatrosscd.
With me so far? OK, on to the commands!
The Cream of the Commands
man: If you really want to know what each command does, use the man (manual) command.
for example, shows the documentation about the list directory command.
Man pages were written for technicians by system administrators and developers. They can be a little dense. So, if you find yourself foundering with man's documentation, check out 30 Best Sources For Linux / *BSD / Unix Documentation On the Web, which links to the Web documentation for most of the major Linux distributions, often more user-friendly than man pages. Another handy resource is Daniel Barrett's Linux Pocket Guide.
The old wisdom was that if you really want to learn Linux and Unix, you needed to read the man pages. In 2012, with all those useful GUIs, that's not as true as it once was. That said, if you really want to understand Linux’s nuts and bolts, the man pages are still a great place to start.
su and sudo: Switch user (su) lets you login as a different user. This is often also called super-user because on some systems su lets you login as your system's all-powerful root user. Because the possibilities for trouble is so great, I recommend you never su to root unless you're the system administrator.
The far safer thing to do is to use sudo instead. This command lets you run one other command as if you were the root user.
In both cases, you need the system password. On most desktop Linux distributions that is the same password as you assigned your first user. You did assign a password when you first set up your system right? Right!?
grep: Grep is a shell utility for searching plain-text data sets for lines matching a regular expression. As such, it's an essential part of any serious Linux shell or script user’s tool collection. For example:
grep foo /etc/passwd
returns any line from the password file with the string “foo” in it.
grep -i "foo" /etc/passwd
finds any line from the password file with any variation of “foo” such as FOO or fOo.
You can also use grep to search recursively i.e. read all files under each directory for the string “foo”:
grep -r "foo" /home/sjvn
Grep is also often used with the pipe command to find specific strings from the output of another command. So
ls -la | grep foo*
displays any file or directory that ls had found where the file- or directory-name started with foo. If files with the names foo, foobar, and foolish were in the current present working directory, this set of paired commands would display them.
Regular expressions are a subject unto themselves; while you can do a lot with the basics, there are vast depths to be plumbed and great power to be found if you give these dedicated attention. To learn about them, and how to use them, visit the Regular-Expression.info site.
ps: report process status. This command shows what programs are currently running. I probably use ps, along with grep, more often than any other command.
Say you have a program that's misbehaving and you can't turn it off from the desktop. (Yes, I'm looking at you, Firefox!) I just run:
ps -ef | grep firefox
This command does several things in 17 characters:
- First, it finds all programs currently running on my PC.
- Then grep picks out the files that have “firefox” in them and displays them on my monitor.
- Each line also contains its process ID number. Armed with that I can now use...
kill: The command where the name says it all: Kill off the misbehaving Firefox process—-or any other process I want to zap.
would blow away any program with the process ID 1234. To make darn sure that the program was dead, dead, dead, I'd use the command:
kill -9 1234
The -9& flag is the equivalent of terminate with extreme prejudice in Linux command land.
clear: Too much stuff on your terminal screen? Just run clear and it all disappears. If you then find yourself looking for information you just erased from the screen, use the up-arrow and down-arrow keys to bring your recently issued commands back from history and re-run them as needed.
cp: cp stands for copy and it does exactly what you think it does: It copies one or more files to a different name and/or directory. Some common uses include:
cp fred.txt ethel.txt
Copies the file fred.txt in the current directory to the new file ethel.txt in the same directory.
cp fred.txt /home/sjvn/docs/fred.txt
Copies fred.txt to the directory /home/sjvn/docs.
cp *.txt /home/sjvn/docs/
Copy all files ending in .txt into the /home/sjvn/docs/ directory.
cp -r /home/sjvn/docs/* /home/sjvn/backup
Copies all the files, directories, and subdirectories in the home/sjvn/docs directory into the /home/sjvn/backup directory.
hostname: The name of the computer into which you're currently logged in. With a desktop system, 99 times out of 100, that is your PC.
mv: Move. mv works pretty much the same way that cp does except you're moving files from one location to another or you're changing its name. So if I typed
mv fred.txt ethel.txt
instead of having two files, fred.txt and ethel.txt, as I did with the example of the cp command, I'd only have the ethel.txt file after “mv”ing it.
pwd: Print working directory. pwd displays the name of your current directory. Knowing where you are is always important in Linux land!
rm: rm stands for remove, Linux's version of delete. So if I ran
I'd be deleting fred.txt once and for all.
I highly recommend that you run the rm command with the -i interactive flag. This makes the program ask you if you really want to delete a file before it's removed. So, in real life I'd type:
rm -i fred.txt
just in case I'd mistyped the file name, or worse still used a wildcard incorrectly. Bringing deleted files back in Linux tends to be a pain.
uname: uname -a gives you a one-line summary of the PC's basic information. This typically includes your computer's name, which Linux kernel you're running, your system architecture, and your distribution name.
For more detailed information, you use the cat command, which displays text information with your Linux PC's system resources. The most useful of these are:
cat /proc/cpuinfo: Displays your CPU's vital statistics.
cat /proc/version: Shows you more details about your currently-running Linux distribution.
cat /etc/printcap: Shows you all your currently set-up printers.
set | more: The pairing of commands set | more gives you more, probably much more, than you ever wanted to know about your current desktop environment. Rather than getting the full nine-yards of what's been set up in your environment, if you want to know about just one or two elements in your system environment, you're better off running a command like:
which would display the directories your system checks for executable files.
The end, or rather the beginning
What I've given you where is just the surface of Linux commands. Hundreds of books and websites give far more comprehensive overviews and go into greater depth on how to use the commands by themselves or in shell programs. This article is just meant to give you enough to get by when you need to take a deeper look into your system than your GUI is giving you.