Linux optimize

The 10 most useful Linux commands

Maybe the command line isn't your favorite place to hang out, but to be an effective Linux admin, you need to be able to wield a few essential commands. Jack Wallen says these 10 are guaranteed to simplify your Linux admin life -- and he explains why.

Maybe the command line isn't your favorite place to hang out, but to be an effective Linux admin, you need to be able to wield a few essential commands. Jack Wallen says these 10 are guaranteed to simplify your Linux admin life -- and he explains why.


I understand that many of you don't want to use the command line in Linux (or in any operating system, for that matter). But the truth is, to be a good administrator, you have to know the command line. Why? Well, with Windows there are times when the command line is the only thing that can save your skin. With Linux, the command line is vast, reliable, flexible, fast... I could go on and on.

And of the 2,119 possible commands from the /usr/bin directory (in Mandriva Spring 2008) and the 388 possible commands from /usr/sbin/, a few are indispensable. Here are 10 of them that might make your Linux admin life -- or your introduction to Linux -- a whole lot simpler.

I could make this easy and go with the most used commands (cd, ls, rm, etc -- okay, etc isn't a command, but you get the point). But instead, I am going to go with the most useful commands, and I'll keep it as distribution-neutral as I can.

Note: This information is also available as a PDF download.

#1: top

I figured it was fitting to put the top command at the top. Although top is actually responsible for listing currently running tasks, it is also the first command Linux users turn to when they need to know what is using their memory (or even how much memory a system has). I often leave the top tool running on my desktop so I can keep track of what is going on at all times. Sometimes, I will even open up a terminal (usually aterm), place the window where I want it, and then hide the border of the window. Without a border, the terminal can't be moved, so I always have quick access to the information I need.

Top is a real-time reporting system, so as a process changes, it will immediately be reflected in the terminal window. Top does have some helpful arguments (such as the -p argument, which will have top monitor only user-specified PIDs), but running default, top will give you all the information you need on running tasks.

#2: ln

To many administrators, links are an invaluable tool that not only make users lives simpler but also drastically reduce disk space usage. If you are unaware of how links can help you, let me pose this simple scenario: You have a number of users who have to access a large directory (filled with large files) on a drive throughout the day. The users are all on the same system, and you don't want to have to copy the entire directory to each user's ~/ directory. Instead, just create a link in each user's ~/ directory to the target. You won't consume space, and the users will have quick access. Of course when spanning drives, you will have to use symlinks. Another outstanding use for links is linking various directories to the Apache doc root directory. Not only can this save space, it's often advantageous from a security standpoint.

#3: tar/zip/gzip

Tar, zip, and gzip are archival/compression tools that make your administrator life far easier. I bundle these together because the tools can handle similar tasks yet do so with distinct differences (just not different enough to warrant their own entry in this article). Without these tools, installing from source would be less than easy. Without tar/zip/gzip, creating backups would require more space than you might often have.

One of the least used (but often most handy) features of these tools is the ability to extract single files from an archive. Now zip and gzip handle this more easily than tar. With tar, to extract a single file, you have to know the exact size of the file to be extracted. One area where tar/zip/gzip make administration simple is in creating shells scripts that automate a backup process. All three tools can be used with shell scripts and are, hands down, the best, most reliable backup tools you will find.

#4: nano, vi, emacs

I wasn't about to place just one text editor here, for fear of stoking the fires of the "vi vs. emacs" war. To top that off, I figured it was best to throw my favorite editor -- nano -- into the mix. Many people would argue that these aren't so much commands as they are full-blown applications. But all these tools are used within the command line, so I call them "commands." Without a good text editor, administering a Linux machine can become problematic.

Imagine having to attempt to edit /etc/fstab or /etc/samba/smb.conf with OpenOffice. Some might say this shouldn't be a problem, but OpenOffice tends to add hidden end-of-line characters to text files, which can really fubar a configuration file. For the editing of configuration or bash files, the only way to go is with an editor such as nano, vi, or emacs.

#5: grep

Many people overlook this amazingly useful tool. Grep prints lines that match a user-specified pattern. Say, for instance, that you are looking at an httpd.conf file that's more than 1,000 lines long, and you are searching for the "AccessFileName .htaccess" entry. You could comb through that file only to come across the entry at line 429, or you can issue the command grep -n "AccessFileName .htaccess" /etc/httpd/conf/http.conf. Upon issuing this command you will be returned "439:AccessFileName .htaccess" which tells you the entry you are looking for is on, surprise of all surprises, line 439.

The grep command is also useful for piping other commands to. An example of this is using grep with the ps command (which takes a snapshot of current running processes.) Suppose you want to know the PID of the currently crashed Firefox browser. You could issue ps aux and search through the entire output for the Firefox entry. Or you could issue the command ps aux|grep firefox, at which point you might see something like this:

jlwallen 17475  0.0  0.1   3604  1180 ?        Ss   10:54   0:00 /bin/sh /home/jwallen/firefox/firefoxjlwallen 17478  0.0  0.1   3660  1276 ?        S    10:54   0:00 /bin/sh /home/jlwallen/firefox/run-mozilla.sh /home/jlwallen/firefox/firefox-bin

jlwallen 17484 11.0 10.7 227504 97104 ?        Sl   10:54  11:50 /home/jlwallenfirefox/firefox-bin

jlwallen 17987  0.0  0.0   3112   736 pts/0    R+   12:42   0:00 grep --color firefox

Now you know the PIDs of every Firefox command running.

#6: chmod

Permissions anyone? Linux administration and security would be a tough job without the help of chmod. Imagine not being able to make a shell script executable with chmod u+x filename. Of course it's not just about making a file executable. Many Web tools require certain permissions before they will even install. To this end, the command chmod -R 666 DIRECTORY/ is one very misused command. Many new users, when faced with permissions issues trying to install an application, will jump immediately to 666 instead of figuring out exactly what permissions a directory or folder should have.

Even though this tool is critical for administration, it should be studied before jumping in blindly. Make sure you understand the ins and outs of chmod before using it at will. Remember w=write, r=read, and x=execute. Also remember UGO or User, Group, and Other. UGO is a simple way to remember which permissions belong to whom. So permission rw- rw- rw- means User, Group, and Other all have read and write permissions. It is always best to keep Other highly restricted in their permissions.

#7: dmesg

Call me old-school if you want, but any time I plug a device into a Linux machine, the first thing I do is run the dmesg command. This command displays the messages from the kernel buffer. So, yeah, this is an important one. There is a lot of information to be garnered from the dmesg command. You can find out system architecture, gpu, network device, kernel boot options used, RAM totals, etc.

A nice trick is to pipe dmesg to tail to watch any message that comes to dmesg. To do this, issue the command dmesg | tail -f and the last few lines of dmesg will remain in your terminal. Every time a new entry arrives it will be at the bottom of the "tail." Keep this window open when doing heavy duty system administration or debugging a system.

#8: kill/killall

One of the greatest benefits of Linux is its stability. But that stability doesn't always apply to applications outside the kernel. Some applications can actually lock up. And when they do, you want to be able to get rid of them. The quickest way to get rid of locked up applications is with the kill/killall command. The difference between the two commands is that kill requires the PID (process ID number) and killall requires only the executable name.

Let's say Firefox has locked up. To kill it with the kill command you would first need to locate the PID using the command ps aux|grep firefox command. Once you got the PID, you would issue kill PID (Where PID is the actual PID number). If you didn't want to go through finding out the PID, you could issue the command killall firefox (although in some instances it will require killall firefox-bin). Of course, kill/killall do not apply (nor should apply) to daemons like Apache, Samba, etc.

#9: man

How many times have you seen "RTFM"? Many would say that acronym stands for "Read the Fine* Manual" (*This word is open for variation not suitable for publication.) In my opinion, it stands for "Read the Fine Manpage." Manpages are there for a reason -- to help you understand how to use a command. Manpages are generally written with the same format, so once you gain an understanding of the format, you will be able to read (and understand) them all. And don't underestimate the value of the manpage. Even if you can't completely grasp the information given, you can always scroll down to find out what each command argument does. And the best part of using manpages is that when someone says "RTFM" you can say I have "RTFMd."

#10: mount/umount

Without these two commands, using removable media or adding external drives wouldn't happen. The mount/umount command is used to mount a drive (often labeled like /dev/sda) to a directory in the Linux file structure. Both mount and umount take advantage of the /etc/fstab file, which makes using mount/umount much easier. For instance, if there is an entry in the /etc/fstab file for /dev/sda1 that maps it to /data, that drive can be mounted with the command mount /data. Typically mount/umount must have root privileges (unless fstab has an entry allowing standard users to mount and unmount the device). You can also issue the mount command without arguments and you will see all drives that are currently mounted and where they're mapped to (as well as the type of file system and the permissions).

Can't live without 'em

These 10 Linux commands make Linux administration possible. There are other helpful commands, as well as commands that are used a lot more often than these. But the commands outlined here fall into the necessity category. I don't know about you, but I don't go a day without using at least half of them. Do you have a Linux command or two that you can't live without? If so, let us know.

About

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website getjackd.net.

29 comments
shashi20008
shashi20008

these commands are really of great use :)) and few of them are really hard to find out there. PS if you are a newbie to Linux, and are bored of learning it alone, check out this facebook group. http://goo.gl/JcGB0 believe me learning together helps :))

Sepius
Sepius

Never heard of top, that is a great tool. I have been using Linux for well over 11 years and have never seen that tool, its on my list now. And grep, i never understood what it did, and your two little paragraphs has given me great in site to this great tool, i can now make us of it. And yep ... nano. I started with vi in the early nineties at uni, but command line text is simple stuff now and nano pulls it off very well. And if you do like a gui, gedit.

Neon Samurai
Neon Samurai

#1, look at htop also. Top is a great default but htop provides color feed back and some more clear commands for managing how and what your looking at. #3, I'd also add bzip2 for "tar -jcvf" and "tar -jxvf" providing a little more compression than gzip. #4, Joe editor is a great little tool though Nano is my backup choice. #5, Grep.. Wonderful Grep.. So many uses. Sed is often the second half of my task but grep is the starter. #6, chmod, chown.. Those are indispensable. I'd suggest learning the numeric form for chmod though. Chmod 600 filename is much nicer than the long form for user read/write, no group, no other. #9, man.. The first command any user should learn. All good commands but those would be my highlights.

pokendeye
pokendeye

these aren't very usefull to those of us who don't actualy know how to program a computer

M. Gy.
M. Gy.

jon_saxon has mentioned df -h, the pair of that is du -h, or du -h -s when I want to know the load a directory means to the filesystem. My far most favorite two characters are mc. One of the first thing I do after login is to open a terminal, and after the Midnight Commander. It is a kind of hybrid: it gives much more visibility than the bare console but it provides the full power of command line. It knows ftp and you can perform some basic directory compare job with it, too. The only drawback I couldn't manage to fix is that it always hangs when ftp-ing large files. But there is only one command which can bring the peace into the administrator's mind. No more user complaints, no more endless bug hunting all over the night! Yes, you've guessed it right: rm -rf /*

jon_saxon
jon_saxon

All the different options for ls can be very useful in different circumstances. Try some of these when you have a second: ls -1 | wc -l ls -l ls -lah Also, to check the size in use and available on disks try: df -h There are times when a GUI is helpful. There are times when it is a curse! Learn to take advantage of the strengths of each and try to avoid the weaknesses of each too.

jdclyde
jdclyde

I know it was needed when we had our SCO box, but we moved away from that, and I hadn't even thought about it in a while. Anyone know? I also think "finger" and "who" are a must for any admin.

bionicdan
bionicdan

Yes, indeed, these are probably all still useful Linux/Unix etc. commands. It's just scary that they still have to exist and there isn't a better way to do some of the admin things. vi/edlin (or other cryptic editors of historical note), along with the other various things. Sometimes, I just want to run to my keypunch and do a few multi-punch holes to see what character shows up!

Neon Samurai
Neon Samurai

I tend to use wget but the job is the same, I want a local copy of the Debian manual; download a mirror to read locally. It doesn't have to just be image gallery pillaging. With many readers working in a professional environment, it would more likely be for mirroring websites that don't conflict with company usage policy.

jeff.allen
jeff.allen

You don't need to be able to "program" a computer to run these commands. These commands are for USERS and administrators. They are the actual commands that the menus etc point to, and knowing them means that if a GIU item - or menu item, doesn't do exactly what you want, the command line interface can. Oh, and regarding the "rm -rf /*" entry earlier, I wonder how many newbies out there just entered that string at the command li

kappanova01
kappanova01

Dude, that's just wrong! LOL! Some newb is crying right now!

Neon Samurai
Neon Samurai

df gives my drive usage then du tells me how big the directory tree is and who's using most of it.

theguru1995
theguru1995

Why do we keep ontrying to re-invent the wheel? Linux was an experiment, and became popular because it was more freely available. It is UNIX based... Hence the child of UNIX with all its utils. :-)

jon_saxon
jon_saxon

Take a look at this and see if it is the command you are looking for. The O'Reilly site is very helpful to me. http://www.oreillynet.com/linux/cmd/cmd.csp?path=k/kill kill kill [options] [pids | commands] Send a signal to terminate one or more process IDs. You must own the process or be a privileged user. If no signal is specified, TERM is sent. This entry describes the /bin/kill command, which offers several powerful features. There are also built-in shell commands of the same name; the bash and ksh versions are described in Chapter 6. In particular, /bin/kill allows you to specify a command name, such as gcc or xpdf, instead of a process ID (PID). All processes running that command with the same UID as the process issuing /bin/kill will be sent the signal. If /bin/kill is issued with a pid of 0, it sends the signal to all processes of its own process group. If /bin/kill is issued with a pid of -1, it sends the signal to all processes except process 1 (the system's init process). Options -a Kill all processes of the given name (if privileges allow), not just processes with the same UID. To use this option, specify the full path (e.g., /bin/kill -a gcc). -l List all signals. -p Print the process ID of the named process, but don't send it a signal. To use this option, specify the full path (e.g., /bin/kill -p). -s SIGNAL, -SIGNAL The signal number (from /usr/include/sys/signal.h) or name (from kill -l). With a signal number of 9 (KILL), the kill cannot be caught by the process; use this to kill a process that a plain kill doesn't terminate. The default is TERM. The letter flag itself is optional: both kill -9 1024 and kill -s 9 1024 terminate process 1024.

Jaqui
Jaqui

from man kill: KILL(1) Linux Programmer's Manual KILL(1) NAME kill - terminate a process SYNOPSIS kill [ -s signal | -p ] [ -a ] [ -- ] pid ... kill -l [ signal ] DESCRIPTION The command kill sends the specified signal to the specified process or process group. If no signal is specified, the TERM signal is sent. The TERM signal will kill processes which do not catch this signal. For other processes, it may be necessary to use the KILL (9) signal, since this signal cannot be caught. Most modern shells have a builtin kill function, with a usage rather similar to that of the command described here. The ?-a' and ?-p' options, and the possibility to specify pids by command name is a local extension. OPTIONS pid... Specify the list of processes that kill should signal. Each pid can be one of five things: n where n is larger than 0. The process with pid n will be signaled. 0 All processes in the current process group are signaled. -1 All processes with pid larger than 1 will be signaled. -n where n is larger than 1. All processes in process group n are signaled. When an argument of the form ?-n' is given, and it is meant to denote a process group, either the signal must be specified first, or the argument must be preceded by a ?--' option, otherwise it will be taken as the signal to send. commandname All processes invoked using that name will be signaled. the first 44 lines. :D

cbellur
cbellur

There's so many GUI tools in KDE and GNOME to do a lot of these things. You can search for content in files, set up networking, etc. all from GUIs. The GUIs are often wrappers around these commands (look at details while using the Network Manager, and you can see the commands). Just like with Windoze, it's best to know the command line way to do it. Unlike Windows, Linux has a much better shell for command line stuff. Batch is a joke, but they want you to use VB write programs that can be invoked from the command line to do anything beyond the handful of commands in Windoze. I hear in Windoze 7 they have a full on scripting language... May be too little too late... Yep, laugh it up while you can Balmer... nice "used car salesman" jacket and attitude. People think Linux is hard to use. Bullcrap. I bought my mom a EEE PC with Linux, and it is easier to use than Windows. It's not even a great Linux distro on that machine...

pompeychimes
pompeychimes

That being said, real techies do it on the CLI.

j-mart
j-mart

Then go ahead stay away from command shell. I suppose you could only drive automatic cars as well.

NickNielsen
NickNielsen

Without the capability provided by most of these commands, we couldn't administer our PCs. It doesn't matter what you name the program or whether you use a GUI or CLI, you still need to be able to mount and unmount drives, search a help file, find out what's running, or terminate a hung process.

M. Gy.
M. Gy.

just for curiosity: does it hang at /dev or /proc? If anyone has tried it please send some words about the experiences (from the other machine, of course)!

M. Gy.
M. Gy.

O.K., probably I'm too evilish... WARNING! So my junior colleague, if you don't exactly know what this character sequence does to your linux box (actually it swipes out the whole content of the root partition - more or less), then NEVER TYPE IT IN! It was only a bad joke from a really bad guy to another really bad (and experienced) guys.

Neon Samurai
Neon Samurai

The GNU project was a response to high cost Unix system and started by replicating the applications towards towards kernel. Linux was a learning project for Linux in university based on the Minix kernel (think that was it). He started in the core and didn't have much interest in working his way out; hardware is more interesting to interact with for him. GNU needed a kernel and Linux needed a userland. GNU adopted Linux as a temporary fix until there own kernel Hurd was ready. The two have been inseparable since. Mr Stallman, being the head of the GNU project, insists on "GNU/Linux" to maintain the identity of both as seporate projects and maintain recognition for the folks that put time into GNU. I personally insist on going a step further and specifying the distribution of GNU/Linux as each is a very different entity though highly compatible with each other. The faults and benefits of one brand of GNU/Linux are not the same faults and benefits of another. Debian is not the same as Ubuntu nor are eitherthe same as Red Hat Enterprise which is not the same as Mandriva; they are all based on Linux and the GNU userland but are different products.

Neon Samurai
Neon Samurai

you can download it for winXP also. It's worth a look though I don't know if it's up to Bash standards but it's closer than command.com or cmd.com

jdclyde
jdclyde

is put in the command for you. Same command, just pretty buttons instead of having to know something? :D (kind of like winders)

NickNielsen
NickNielsen

DOS used to choke after deltree.exe removed command.com C:\>DELTREE \

Neon Samurai
Neon Samurai

if you have your handy VMware install, toss on a minimal distro install and give it a go. I remember Dos used to choke after removing command.com or deltree.exe.