sjvn01

The 16 Linux Shell Commands Every Desktop Linux User Should Know

by sjvn01 on ‎25-04-2012 10:30 AM - last edited on ‎10-05-2012 09:10 AM by Administrator

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.

linux-shell-commands-every-desktop-linux-user-should-know-560x360.jpg

Shell Basics

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:

echo $SHELL

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.

Echo.png

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,

ls -la 

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.

ls-la.png

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

ls a?cd 

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.

man ls

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.

kill 1234

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.

Directory Commands

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

rm fred.txt

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.

System Information

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.

uname-a.png

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:

echo $PATH

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.

If you want to know more, visit LinuxCommand.org or Linux in a Nutshell. If you want to become a Linux shell programming wizard, I recommend starting with Learning the bash Shell by Cameron Newham.

See also:

Comments
by Andrew Pollack(anon) on ‎25-04-2012 11:51 AM

The single best bash shell resource for learning from scratch and for reference is:

 

"Linux Shell Scripting with BASH" by Ken O. Burtch   ( http://amzn.to/I9znSP )

It is very easy reading, very well organized, and the choice of content is just excellent. 

by LinuxCanuck(anon) on ‎26-04-2012 06:44 AM

I headline like this sets us back years. It makes it sound like we need to use the commandline. It is useful and powerful, but not essential for all users.

I think that it does a disservice to suggest that we have not progressed beyond 2000 when this headline would have made more sense. If we want to attract users in 2012 the this headline will have the opposite effect. 

The geek in us can be intimidating for the average person and sometimes we need to excerise restraint and think of the global effect before we post. 

BTW, I think the article is useful and good, but not helpful in the way it is presented. 

by Kevin(anon) on ‎26-04-2012 06:57 AM

You wrote:

So

ls -la | grep foo*

displays any file or directory that ls had found where the file- or directory-name started with foo.

Sorry, but that's not even close to true.

Since you used the -l flag, the files are listed with their attributes first. So "foo*" could not be at the beginning of any line, since every line will begin with something like '-rw-r--r--' and 'drwxr-xr-x'.

That doesn't really matter, though, because grep doesn't look at the beginning of the line; it looks anywhere in the line. If the pattern needs to be at the beginning of the line, you need to put a ^ at the beginning.

 But even if you fix that, it's still not right, because regular expressions work different than globs. "foo*" in globbing means anything starting with "foo", but in regular expressions it means fo followed by any number (even 0) number of o's, so it would catch, e.g., "foray" or "forest".

You could use '.*" instead of '*', but that's not necessary here, since it'll look for any occurrence of the pattern inside the line.

So I'm sure what you meant is:

 

ls -a | grep ^foo

Or better:

find * -maxdepth 0 | grep ^foo
by bogen(anon) on ‎26-04-2012 09:15 AM

I agree with LinuxCanuck. I'm an avid command line user, but if I set up someone with a Linux desktop who is not a command line user, in no way do I expect them to use the command line unless they are inclined to already, or actually want to learn it.

I've been using Linux for more than 15 years and I do encourage users to lean the command line, but to say "Every Desktop Linux user" needs to learn "these shell commands" is not right. For many who have come from Windows or MacOS, they did not need to use the command there, why should they on Linux.

While I applaud your efforts to educate Linux users, I must respectfully disagree with the premise of this article.

by Rick Stanley(anon) on ‎26-04-2012 09:24 AM

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

Due to the nature of this potentially dangeous command I would reccomend expanding on this.

  • Moving Files
  • Moving files into other directories
  • Moving entire directories
  • Renaming files
  • Renaming directories
  • etc...

It's too easy to screw up this very useful command.

Ditto for the "rm" command.  Have you ever run the command "rm -fr /*" (NEVER RUN THIS COMMAND EVER!!!) intending to delete all files in the current directory? ;^)  If not, you know someone who has! ;^)

Other expansion and testing of the details in this article is advised.  See grep.

Cheers!

by Karl O Pinc(anon) on ‎26-04-2012 11:30 AM

Better than"clear" is the "reset" command.  "reset" clears the screen _and_ resets it to it's default state, turning off any special character modes or other strangeness that may have been accidently turned on by displaying something non-printable, like an executable file.

by triclone(anon) on ‎28-04-2012 09:52 AM

Nice. Thanks!

by Andrew Thompson(anon) on ‎12-07-2012 04:16 PM

This page has been bookmarked! Excellent resource for a long time Windows user struggling to get to grips with Linux.

Many thanks!

by omer kliya(anon) on ‎23-11-2012 10:20 PM

chmod !!!!!!!!!

by Newbee(anon) on ‎21-04-2013 05:16 AM

extra usefull stuff!

thank you very much

Post a Comment
Be sure to enter a unique name. You can't reuse a name that's already in use.
Be sure to enter a unique email address. You can't reuse an email address that's already in use.
Type the characters you see in the picture above.Type the words you hear.

The HP Input Output site is sponsored by HP and features articles and content from HP and third-party contributors. Third-party articles and content, while paid for by HP, do not necessarily represent the views and opinions of HP. HP does not endorse this content and is not responsible for its accuracy, availability and quality.

Follow Us
Spotlight
"It's Not My Job" - Handling the Vendor Finger-Pointing Trap Is Teamwork Dead? A Post-Agile Prognosis Improving Your Personal Brand with Social Networking 5 Types of Meetings Every Business Must Explore
┼ Based on energy, paper and toner savings from regular printer usage. Results may vary.