Select Page

Basic Unix Shell Commands

How many of us haven’t been intimidated by the weird black Unix shell, at least once. A blank empty prompt that dares us to type commands.

Well, the Unix Shell is the most versatile and powerful tool on the Unix platform. It interprets the commands typed in by the user, executes them and outputs results to be viewed by the user. The Unix shell comes in many flavours. Common favorites are bash(GNU Bourne Again-SHell) and csh(C-SHell). This article will explain many commonly used commands.

 

Hire Bobcares Linux Server Administrators
Get super reliable servers and delighted customers

See how we do it!

 

 

Commonly used shell commands.

Listing a directory (ls)

The ls commands lists all the non-hidden files in the current directory.

[root@localhost www]# ls
cgi-bin error html icons manual mrtg nut-cgi-bin usage wordtrans

To list hidden files as well, use the -a option

[root@localhost www]# ls -a
. .. cgi-bin error .htaccess html icons manual mrtg nut-cgi-bin usage
wordtrans

To list more details on each file in the directory, use the -l option. This option lists the permissions, size and the last modified dates of the files .

[root@localhost www]# ls -la
total 88
drwxr-xr-x 11 root root 4096 Jun 17 2005 .
drwxr-xr-x 25 root root 4096 Jun 17 2005 ..
drwxr-xr-x 2 root root 4096 Jun 17 2005 cgi-bin
drwxr-xr-x 3 root root 4096 Jun 17 2005 error
drwxr-xr-x 13 root root 4096 Feb 5 13:28 html
drwxr-xr-x 3 root root 4096 Jun 17 2005 icons
drwxr-xr-x 13 root root 4096 Jun 17 2005 manual
drwxr-xr-x 2 root root 4096 Jun 17 2005 mrtg
drwxr-xr-x 2 root root 4096 Jun 17 2005 nut-cgi-bin
drwxr-xr-x 2 webalizer root 4096 Mar 14 04:02 usage
drwxr-xr-x 6 root root 4096 Jun 17 2005 wordtrans

Changing Directory (cd)

cd is used to change from one directory to another

[root@localhost www]# ls
cgi-bin error html icons manual mrtg nut-cgi-bin usage wordtrans
[root@localhost www]# cd html
[root@localhost html]#

In order to change to another absolute path, type cd, followed by the entire path.

[root@localhost html]# cd /usr/bin
[root@localhost bin]#

In order to change to the home directory, you can use this simple shortcut (cd ~)

[root@localhost html]# cd ~
[root@localhost ~]#

Now, suppose you want to go back to the previous directory, without having to type out the directory again, use this simple shortcut (cd – )

[root@localhost ~]# cd -
/var/www/html
[root@localhost html]#

Copying files (cp)

Use the ‘cp’ command to copy single or multiple files, and directories as well.

To copy a single file ‘abc’ to ‘def’

[root@localhost tmp]# ls abc
abc
[root@localhost tmp]# cp abc def
[root@localhost tmp]# ls def
def
[root@localhost tmp]#

To recursively copy a directory from one location to another, use the -r option. This example copies the directory craig, from /root/work to the current directory, tmp.

[root@localhost tmp]# ls /root/work/craig/
example.doc
[root@localhost tmp]# cp -r /root/work/craig .
[root@localhost tmp]# ls craig/
example.doc
[root@localhost tmp]#

Deleting files and directories (rm)

The rm command is used to delete files. Remember that there is no undelete in Linux. So be pretty careful when you delete files or directories.

[root@localhost tmp]# rm abc
rm: remove regular empty file `abc'? y
[root@localhost tmp]# rm a
rm: remove regular file `a'? y
[root@localhost tmp]#

To delete a directory and all the files in it, simply use the -rf command. Remember, this command is potent, and not reversible. So use with care.

[root@localhost tmp]# ls craig
example.doc
[root@localhost tmp]# rm -rf craig
[root@localhost tmp]# ls craig
ls: craig: No such file or directory
[root@localhost tmp]#

Create a new directory (mkdir)

[root@localhost tmp]# mkdir testdir
[root@localhost tmp]# ls testdir
[root@localhost tmp]#

To delete an empty directory, use the rmdir command.

[root@localhost tmp]# rmdir testdir
[root@localhost tmp]# ls testdir
[root@localhost tmp]#

Print name of current directory (pwd)

To know where you are in the directory structure, use the pwd command.

[root@localhost tmp]# pwd
/tmp
[root@localhost tmp]#

Read and write files (cat)

The cat command is one of the most versatile tools available to read and write into files.

This example, prints the contents of the file , def.

[root@localhost tmp]# ls def
def
[root@localhost tmp]# cat def
this is a test
[root@localhost tmp]#

Append more text into a file, using the cat command, followed by the append operator, >>. This example, appends a second line into the file, def.

[root@localhost tmp]# cat def
this is a test
[root@localhost tmp]# cat >> def
this is a second line
Press CTRL-D
[root@localhost tmp]# cat def
this is a test
this is a second line
[root@localhost tmp]#

If you use the cat command, followed by >, it will empty the file and insert the fresh content into it.

[root@localhost tmp]# cat def
this is a test
[root@localhost tmp]# cat > def
this is a second line
Press CTRL-D
[root@localhost tmp]# cat def
this is a second line
[root@localhost tmp]#

Cat is most useful, when used with other Linux commands. For example, suppose you want to append the contents of one file, abc into another file, def, all you need to do is

[root@localhost tmp]# cat abc
this is a test line
[root@localhost tmp]# cat abc > def
[root@localhost tmp]# cat def
this is a test line
[root@localhost tmp]#

Output the last part of a file (tail)

Lets assume you need to see the last part of a very large file, say a log file. At such times, it is best to use a command like tail. Tail command, outputs the last few lines of a file.

For example, this command, outputs the last 3 lines of /var/log/messages

[root@localhost tmp]# tail -3 /var/log/messages
Mar 17 10:40:01 localhost crond(pam_unix)[15513]: session closed
for user root
Mar 17 10:45:01 localhost crond(pam_unix)[16420]: session opened
for user root by (uid=0)
Mar 17 10:45:02 localhost crond(pam_unix)[16420]: session closed
for user root [root@localhost tmp]#

In order to keep checking the updates to a file, use the -f option

For example, this command, will print out the last few lines of the file as it updates. This is especially useful for checking the updates to a large file, like a log file.

[root@localhost tmp]# tail -f /var/log/messages

Search for words or patterns in a file (grep, fgrep)

Searching for words or patterns in a file or files is child’s play in Linux.

[root@localhost tmp]# cat def
this is a test line
this is a second test line
[root@localhost tmp]# fgrep ``second'' def
this is a second test line
[root@localhost tmp]#

Similarly you can search for words in multiple files and even directories, like this.

fgrep ``second'' *

To recursively search files in a directory, use the -r option

fgrep -r ``second'' .

Use the grep command to also search command outputs. For example, in order to search the output of ‘ls -la’ for a pattern, use the grep command. For example, what the Pipe(|) command does below is to take the output of ls -la and feed it to grep, which then proceeds to search for the pattern and output the results of the search.

[root@localhost tmp]# ls -la | grep ‘pattern’

See ‘man grep’ for details.

Displaying and setting Date (date)

Want to know the date on your computer. Simply type ‘date’

[root@localhost tmp]# date
Fri Mar 17 11:29:24 IST 2006

In order to set the date, use the -s option.

[root@localhost tmp]# date -s "2006-3-17 11:30:00"
Fri Mar 17 11:30:00 IST 2006
[root@localhost tmp]# date
Fri Mar 17 11:30:01 IST 2006

Zipping and unzipping, the Linux way (tar)

Its always useful to know how to zip and unzip files in Linux. A common format of packaged software, especially Open source software is tar.gz. Here is how you can unzip it

For example, use the tar zxvf command to unzip and untar the tarball.

[root@localhost tmp]# ls kphone-4.2.tar.gz
kphone-4.2.tar.gz
[root@localhost tmp]# tar zxvf kphone-4.2.tar.gz
kphone/
kphone/CVS/
kphone/CVS/Root
kphone/CVS/Repository
kphone/CVS/Entries
kphone/CVS/Entries.Log
--too long to list --

Similarly, to zip and tar a directory into a tarball, use the tar zcvf command.

[root@localhost tmp]# tar zcvf kphone-4.2.tar.gz kphone/
kphone/
kphone/CVS/
kphone/CVS/Root
kphone/CVS/Repository
kphone/CVS/Entries
kphone/CVS/Entries.Log
--too long to list --

man

If you have trouble figuring out any command, all you need to do is use the man command. This is the F1 or Help of Linux.

For example

man grep
man tar
man ls

All commands, will have a man based help guide, which will list all the options and a few examples.

less

The outputs of some commands or files can be very long, and quickly scroll down the screen. ‘Less’ is a popular command to allow easy page by page scrolling of display output. Less is generally used with a command.

For example

ls -la | less
cat /var/log/messages | less

A similar command is ‘more’. Unix gurus, however agree that ‘less’ is better than ‘more’. Unlike ‘more’, ‘less’ has search features and the ability to scroll up and down.

ping

If you are in a network, most likely you may need to use ping once in a while to check Network or Internet connectivity. For example, to check if you can access yahoo.com, simply use ping.

[root@localhost tmp]# ping yahoo.com
PING yahoo.com (66.94.234.13) 56(84) bytes of data.
64 bytes from w2.rc.vip.scd.yahoo.com (66.94.234.13): icmp_seq=0 ttl=56 time=240 ms
64 bytes from w2.rc.vip.scd.yahoo.com (66.94.234.13): icmp_seq=1 ttl=56 time=252 ms
64 bytes from w2.rc.vip.scd.yahoo.com (66.94.234.13): icmp_seq=2 ttl=56 time=245 ms
-- yahoo.com ping statistics -- 3 packets transmitted, 3 received,
0% packet loss, time 2000ms
rtt min/avg/max/mdev = 240.913/246.133/252.143/4.653 ms, pipe 2
[root@localhost tmp]#

Articles by Sangeetha Naik
About the author:
Sangeetha Naik heads Bobcares.com. She is the co-founder of Poornam Info Vision Ltd., Software and IT services company specializing in Linux based solutions for Webhosts and ISPs. Poornam Info Vision is an ISO 9001:2000 certified company with a team of over 140 engineers.

Sangeetha is a Computer Engineer based in India and has over 7 years of experience in the Hosting industry. Her articles have been published both online as well as in print.



LinuxServer Support
Linux

2 Comments

  1. Hi,
    Sangeetha ,Your site is really usefull and an easy flavor to catch.

    Thanks.

  2. i need to check in which shell i am working

Bobcares
Bobcares is a server management company that helps businesses deliver uninterrupted and secure online services. Our engineers manage close to 51,500 servers that include virtualized servers, cloud infrastructure, physical server clusters, and more.
MORE ABOUT BOBCARES