Learn Basic UNIX shell command in one day

Satya Unix Tutorial , , , , , , , , , ,

Learn Basic UNIX shell command in one day: It is not easy to learn Unix in one day. Learning is continuous process, which takes some time. It takes some dedication and hard work to become proficient in Unix commands. I have posted this tutorial, so that you can learn basic UNIX commands in just one day, which will be sufficient to kick start your day to day work in UNIX platform. I have kept “Learn Basic UNIX shell command in one day” topic simple and easy to understand. If you find any thing difficult then send me message or feedback.

UNIX operating system

Unix is multitasking, multiuser operating system written in c program.  It is completely different than window and consist of file system, which contains different directories and files. It was developed in Bell lab by Ken Thomson and Dennis Ritchie.  There are different types of files in UNIX. These are normal text file, binary, scripts, device, pipes, interprocess communication, and many more. Directory is like folder in window, which contains different files and sub directories. It does not store actual data but stores pointer or address of the location where data is stored. File system hierarchy is the arrangement where in the top directory is considered root, which contains different directories and files, also called as nodes. See the file system image.

 

Learn Basic UNIX shell command in one day - Unix file system
              Image: Learn Basic UNIX shell command in one day – Unix file system

 File system directories

/ – This is top level directory, called as root.

bin – stores binary/executable files.

dev – all the device files

etc – stores system configuration files and many other important files like disk configuration, valid user list etc.

lib – kernel related and shared library.

boot – system boot related files

home – home directory for all the users

kernel – contains kernel files

proc – contain process files

tmp – stores all the temporary files

mnt – it is used for cdrom, floppy disk(now it is not being used), flash drive and other temporary files mount.

usr – used by many users. Includes administrative commands, shared files, library files etc.

var – contains variable length files like log, print files etc.

sbin – contain system administration related binary files.

Learn Basic UNIX shell command in one day

In Unix, every thing is managed through shell command. Bash is the most popular shell. Shell is UNIX command interpreter, which interact with user and kernel. Therefore, any command run by user is interpreted by shell and then it is sent to kernel to do further processing. Kernel is a operating system program which manage every thing in a system like system call, basic input output device,  manages processes, handles memory and peripherals like keyboard, mouse etc. There are different kind of shell like bash, csh, ksh, and bourne. Among all these most popular is bash shell. To check the shell type, open Unix terminal and run below command

      >echo $SHELL
      >bash

Use “pwd” to check in which path you are currently in or check current working directory .

      >pwd
      /home/satya 

This shows my present working directory and also shows my home directory. By default, when you login as a user you go into “home” directory.

 

To list all the files present in a directory use ‘ls’.

      >ls    

      >ls -l  # is for long listing


Note: Any thing written beyond ‘#’ is comment

Below image shows long listing of files and directories. It shows all attributes of
files and directories. These are file type, file permission, number of blocks, owner, group,
size in bytes, time stamp and file or directory name.

 

Learn Basic UNIX shell command in one day- ls -l command
Image:Learn Basic UNIX shell command in one day- ls -l command
    >ls -lr # for long and reverse listing    

    >ls -lrt # shows with modified time     

    >ls -a  # This shows hidden files, prefixed by dot(.)

Change directory


    >cd <path of the directory>


I have to use absolute path (Entire path starting from root)  or relative path (with relative to the present working directory) with ‘cd’ command in order to go in ‘log’ directory which present in /var/sys. See below –

 
    >cd /var/sys/log   

#Here, I have used absolute path.


Absolute path is a path where we provide entire path starting from root, all the directories are separated by forward slash. In this case, it does not matter in which current working directory we are in. From any location it can take us to target directory.

Relative path is a path which is relative to the current working directory. Lets see how we can navigate using absolute and relative path.

If you are using absolute path with ‘cd’ then it does not matter at which location you are currently in.


      >cd  /root/home/satya/perl  

# it will take you to the perl directory from any of the directory present in system

 

In case of relative path, if you are in /home/satya directory then you can navigate to any of the directory present inside the /home/satya

     >pwd
     /home/satya
     >ls     
     dir_1 perl  dir_2
     >cd dir_1   # Here, I have used relative path,
     >pwd
     /home/satya/dir_1

This is called relative path. Here, I have not used entire path. Instead I have used “dir_1” with “cd” command as it is present in /home/satya.

Parent directory.

     >cd ..   # go to parent directory

If my present working directory is “satya” then cd .. will take one level up, which is /home

Important file and directory command

Create directory.

    >mkdir dir_exp   # it will create directory dir_exp

Create file file.txt by using touch command.

    >touch file.txt

Use standard output redirection ‘>’ sign to create file .

    >echo  "Have a nice day" > example.txt 

Here, echo command is to print message on console. But in this case, we are redirecting the message output, by using “>”, to a file. If the file is not present then it will be created. Need to be caution,  if the file already has any content then it will be wiped out and replaced with new content.

 

    >echo "Have a nice day" >> example.txt

Here, I have used append sign “>>”, this will print message in file but it will not wipe out content of the file. Instead it will append the content.

Edit file using pico or nano.

pico file.txt

nano file.txt

Both pico and nano file editor is self explanatory and has easy menu option to perform file editing.

You can also use vi editor for file editing. It is powerful, and has lot of options to perform some of the complex file editing task.

vi example.txt

For detail,  please refer vi tutorial

Delete file and directory

Delete file by using rm command

    >rm example.txt

 

Remove directory by using rmdir

    >rmdir my_directory

If the directory contains file then it will not delete directory. You will get following message if you try to delete directory which is not empty.

    rmdir: failed to remove 'post': Directory not empty

You need to remove all the files with rm command before you delete directory.

 

cp command

Copy directory from one directory to another:

e.g copy dummy.txt file from /home/satya directory to /tmp directory

     >cp /home/satya/dummy.txt /tmp

Copy post directory and all its subdirectory(recursively) from source /home/satya to /tmp

     >cp -r /home/satya/post /tmp/

Move or rename file or directory using mv

move dummy.txt file from source to target directory. It move dummy.txt file to tmp directory and delete it from source.

mv” command also used for renaming file.

     >mv /home/satya/dummy.txt /tmp
     >mv old_name new_name   #rename file

 Search directory or file in the system using find command

Search file example.txt in a particular directory or path. If you are not sure where the file is situated then use find command to search file in any particular path or directory. For e.g. you can search in root ‘/’ , which is top level directory.

To search example.txt file in root directory run find command. This command will search each and every directory and subdirectory present in root(/).

In order to search in root use below command –

     >find / -type f -name "example.txt"

To search in /home

     >fine /home -type f -name "example.txt"

and same way in order to search “my_directory” directory in  /home use below command. Here,  ‘-type’ specify type of file has to be searched. ‘f’ represent file and ‘d’ represent directory.

     >find /home -type d -name "my_directory"

grep

Search particular world in a file using grep command.

for e.g.  In the below file if you want to search word “apache” then you can use “grep” command. It will print all the lines with apache word. I have used cat command to see the content of file.

     > cat file.txt
     This is just a intro.
     apache is good web server
     it is a line with some content
     Tomcat is also a good web server
     apache http has been used with many famous website
     >          

Run grep command and see the result:

     >grep "apache" file.txt
     apache is good web server
     apache http has been used with many famous website

If you want to do case insensitive search then use “grep -i” .

     >grep -i "apache" file.txt  # it matched both "apache" and "APACHE"

And if you want to exclude lines which does not contain the target world then you can use “grep -v”.

     >grep -v "apache" file.txt
     This is just a intro.  
     it is a line with some content
     Tomcat is also a good web server

You can see that it prints all the lines which does not contains target keyword.

 

Process in Unix:

Check running process by using ‘ps’ command

     >ps
     PID  TTY   TIME     CMD
     3921 pts/2 00:00:00 bash
     5336 pts/2 00:00:00 ps


to check the full lisk of running process.
    >ps -ef
  
Learn Basic UNIX shell command in one day - ps -ef command to check process long listing
                              Image: Learn Basic UNIX shell command in one day – ps -ef command

UID is owner of the process.

PID is process id, PPID is process id of the parent process.

STIME is start time of the process.

TTY is terminal.

CMD is command or process name.

 

To check the process running as particular user, use below command. So if I want to check all the process running as “satya” user then i will use this command:

     >ps -fu satya
 

To kill the process use below commands, here PID is the process id of a process.

     >kill PID

Kill process forcefully.

     >kill -9 PID

sort

To sort the content of a file.

     >sort file.txt
 

Above command will sort file content in ascending order.

 

     >sort -r file.txt

sort -r will sort file content in descending order

Use sort -n if you want to do numeric sort

 

Unix pipe

Unix pipe, represented by ‘|’, takes the input of one command and redirect it to another command.

     > ls -lrt | grep "med"
     drwxr-xr-x 4 root root 4096 Dec 19 08:37 media

Use “uniq” command to print the unique records of a file. But before getting unique records from file, you have to sort the file to arrange all the data together. So that uniq command show expected result.

     >sort file.txt | uniq

Other useful UNIX or LINUX commands

head

Head command shows top 10 lines (by default) of a file. You can define any number to see that many lines. For .e.g head -2 shows top two lines

     >head file.txt   #shows top 10 lines
     >head -2 file.txt


tail

Tail command shows last 10 lines of a file (by default). You can define any number to see that many lines. For e.g tail -1 shows last line of a file.

     >tail -1 file.txt
 

To see log file content dynamically, which grows continuously, use tail -f.

     >tail -f app.log

 

wc

To check word counts of a file use wc command


     > wc file.txt
     5 28 143 file.txt

Here, in this e.g. 5 is number of lines,  28 is number of words, and 143 represent number of characters present.

 

df and du

‘df’ command shows disk use and availability of a file system, and also shows device on which it is mounted.

Learn Basic UNIX shell command in one day - df command
  Image: Learn Basic UNIX shell command in one day – df command

‘du’ is estimate file space usage. To check the space usage of /home/satya, you can use ‘du’. Here, ‘-s’ is summary and ‘-h’ is human readable. It shows /home/satya has occupied 1.6G space.

        >du -hs /home/satya 2>/dev/null
        1.6G /home/satya


In coming days I will cover few more basic commands under “Learn Basic UNIX shell command in one day”.

CUT command to extract string or characters from each line in a file.

Let see the example demonstrating cut command using below file.

cat hello.txt 
hello world xyz.
An apple a day keeps a doctor away.
jack of all master of none.
Grass on the other side always looks green.

Get nth character of each line in hello.txt file. Here, cut command extract 4th character of hello.txt file.

cut -c4 hello.txt
l
a
k
s

In below example, cut command extract string starting from 2nd position and end at 4th.

cut -c2-4 hello.txt 
ell
n a
ack
ras

Display string/characters starting from 2nd position and end at the last position of each line.

cut -c3- hello.txt 
llo world xyz.
apple a day keeps a doctor away.
ck of all master of none.
ass on the other side always looks green

 

Print third field from a file. ‘In this, -d’ option is to specify field delimiter.

cut -d" " -f3 hello.txt 
xyz.
a
all
the

 

Get 2nd and 3rd field.

cut -d" " -f2,3 hello.txt 

world xyz.
apple a
of all
on the

Get 2nd to 5th field.

cut -d" " -f2-5 hello.txt 
world xyz.
apple a day keeps
of all master of
on the other side

 

4 Comments

  1. You shouldn’t have vi on “learn shell commands in a day”. And especially not without further instruction. I suggest ‘gedit’ or ‘pico’ or ‘nano’ or some such where arrow keys work and the menu is visible.

Leave a Reply

Your email address will not be published. Required fields are marked *