The 15 Most Useful Linux commands
By Pete Freitag
Learning Linux or UNIX is a seemingly daunting task, there are thousands of commands out there, each with hundreds of options. But in reality you only need to know a few of them.
I use unix quite a bit, usually either on one of our Linux servers, or on my Mac with OS X (a unix based operating system). And these are the 15 commands that I use most. If you can memorize these 15 commands, you can do quite a bit on a unix operating system, and add unix as a skill on your resume.
The 15 Most Important UNIX commands
- man - show manual for a command, example:
man ls
hitq
to exit the man page. - cd - change directory, example:
cd /etc/
- ls - list directory, similar to
dir
on windows. example:ls /etc
, usels -l /etc
to see more detail - cp - copy a file or directory, example:
cp source dest
if you want to copy a directory use the-R
option for recursive:cp -R /source /dest
- mv - move a file, example:
mv source dest
- rm - remove a file, example:
rm somefile
to remove a directory you may need the -R option, you can also use the -f option which tells it not to confirm each file:rm -Rf /dir
- cat - concatenate, or output a file
cat /var/log/messages
- more - outputs one page of a file and pauses. example:
more /var/log/messages
pressq
to exit before getting to the bottom. You can also pipe to more| more
from other commands, for examplels -l /etc | more
- scp - secure copy, copies a file over SSH to another server. example:
scp /local/file user@host.com:/path/to/save/file
- tar - tape archiver, tar takes a bunch of files, and munges them into one
.tar
file, the files are often compressed with the gzip algorithm, and use the.tar.gz
extension. to create a tartar -cf archive.tar /directory
, then to extract the archive to the current directory runtar -xf archive.tar
to use gzip, just add az
to the options, to create a tar.gz:tar -czf archive.tar.gz /dir
to extract ittar -xzf archive.tar.gz
- grep - pattern matcher, grep takes a regular expression, or to match a simple string you can use fast grep,
fgrep failure /var/log/messages
, I'm usually just looking for a simple pattern so I tend to use fgrep more than regular grep. Grep is case sensitive by default, but here's how you can do case insensitive grep. - find - lists files and directories recursively on a single line, I usually pipe grep into the mix when I use find, eg:
find / | fgrep log
- tail - prints the last few lines of a file, this is handy for checking log files
tail /var/log/messages
if you need see more lines, use the-n
option,tail -n 50 /var/log/messages
you can also use the-f
option, which will continuously show you the end of the file as things are added to it (very handy for watching logs)tail -f /var/log/messages
- head - same as tail, but shows the first few lines the file
- vi - text editor, there are several text editors such as emacs, and nano, but vi is usually installed on any server so its a good one to learn. To edit a file type
vi file_name.txt
to edit a line pressEsc i
then to save changes and exit useEsc wq
, or to quit without saving useEsc q!
. There are a million other commands, but that will enable you to edit files at a basic level.
Once you learn these commands, and are comfortable with them, you shouldn't stop there, there are lots of other commands that can make your life easier.
Runner Up Essential Commands
When I first posted this in 2005, I had quite a few comments suggesting some commands I was missing, here's a list for you:
- chmod - change file permissions
- chown - change file owner user and file group
- ln - create a symbolic link - kind of like a virtual file or folder.
- less - some are saying that less is more, less is the opposite of the more command
- ps - show running processes, along with
top
it these commands are the task manager for linux or unix based systems. - wc - word count, this can be useful for counting things in the output, for example to count the number of files in a directory on linux.
Did I miss any commands that you think are essential to using a UNIX or Linux based operating system?
The 15 Most Useful Linux commands was first published on July 29, 2005.
If you like reading about unix, linux, mac, osx, commands, shell, bash, vi, grep, or scp then you might also like:
- Recursively Counting files by Extension on Mac or Linux
- Howto Backup your Mac incrementally over SSH
- Shell Script for backpack todo lists
- Creating a Symbolic Link with ln -s What Comes First?
Weekly Security Advisories Email
Advisory Week is a new weekly email containing security advisories published by major software vendors (Adobe, Apple, Microsoft, etc).
Comments
symbolic links are a must when creating virtual hosts on Linux, b/c the Flash forms will not work without them.
Explanation
If youre running a web server, you will probably have it configured to run virtual hosts. This may create an issue, if youve decided to use CFMX 7s Flash forms. Flash forms reference files in the {virtual host webroot}/CFIDE/scripts directory. The CFIDE directory is located in the directory you specified upon installing ColdFusion MX 7. When you create a virtual host, the CFIDE directory doesnt exist for the virtual host. Here, there are two things you DO NOT want to do.
1. DO NOT copy the CFIDE directory to your virtual host directory. When coldfusion is upgraded, changes that may occur will not be implemented in files that your virtual host may reference. Copying CFIDE will also allow access to the CF administrator from your virtual host
2. DO NOT explicitly create a symbolic link to the CFIDE directory. This will allow access to the CF Administrator from a virtual host, which should be avoided at all costs.
The best solution is to create a CFIDE directory in your virtual hosts, and then create a symbolic link to the CFIDE/scripts directory. Follow the steps below:
1. go to the directory of your virtual host
2. $mkdir ./CFIDE
3. $ln s ./CFIDE ./cfide
4. $cd ./CFIDE
5. $ln s {path to default host}/CFIDE/scripts ./scripts
Note: By default, Apache may not resolve symbolic links. If your apache installation doesnt resolve symbolic links, youll need to make the change in the apache configuration file.
For example:
tail -f foo.log | grep "[ERROR]" will stream a log file, showing only lines that have "[ERROR]" in them.
grep also has the -v option, that inverts the match. So:
grep -v "[ERROR]" will show you all lines that don't have "[ERROR]" in them.
Oh, and don't forget that on most linuxes, more is actually less, and vi is actually vim :D
By the way, how you liking the new server?
Maybe my memory just isn't what it used to be, but having a quick way to find out where a file is often proves invaluable.
Following on Brad's comment (from 2 months ago), I update the locate database via cron job every morning @ 3am. That keeps the database current enough for my needs.
thanks and regards
raja.p
i want to run two commands in a single line. is there any command to run two commands in a single line. i don't want to merge two commands using | pipe. but i want to run both seperately.
thank you!
i.e. $cat filename | less
OR you can do just $less filename
less allows you to scroll up and down, more is only one way scrolling.
another good one to keep in mind is ps
i.e. $ps -A (this shows all running processes)
chown and chmod are good to know too