Unix Tutorial Part 2

In the previous post, we have seen about the directories. In this Unix Tutorial Part 2 , we will learn the following points.

  • Copying Files
  • Moving Files
  • Removing Files and directories
  • Displaying the contents of a file on the screen
  • Searching the contents of a file

2.1 Copying Files

cp( copy):

cp file1 file2 is the command which makes a copy of file1 in the current working directory and calls it file2

What we are going to do now, is to take a file stored in an open access area of the file system, and use the cp command to copy it to your unixdir directory.

First, cd to your unixdir directory.

% cd ~/unixdir

Then at the UNIX prompt, type,

% cp /myd/tutorial/sample.txt .

Note: Don’t forget the dot (.) at the end, in unix. Dot (.) means the current directory.

2.2 Moving files

mv(move)

mv file1 file2It moves the file1 from one place  to another place file2.

It perfroms a move operation instead of copy. So, we end up with only one file rather than 2.

It can also be used to rename a file, by moving the file to the same directory, but giving it a different name.

We will move a file named as sample.txt to the backup directory.

First, change directories to your unixdir directory (as we have seen in part 1). Then, inside the unixdir directory, type

% mv sample.txt mybackup/.

Type ls and ls mybackup to see the result.

2.3 Removing files and directories

rm (remove), rmdir (remove directory)

To delete (remove) a file, use the rm command. As an example, we are going to create a copy of the sample2.txt file and then delete it.

Inside your unixdir directory, type

% cp sample2.txt tempfile.txt
% ls
% rm tempfile.txt
% ls

You can use the rmdir command to remove a directory (Precondition: It should be empty first). Try to remove the mybackup directory. You will not be able to since UNIX will not let you remove a non-empty directory.

Exercise:

Create a directory called tempfile using mkdir , then remove it using the rmdir command.

2.4 Displaying the contents of a file on the screen

clear (clear screen)

Before you start the next section, you may like to clear the terminal window of the previous commands so the output of the following commands can be clearly understood.

At the prompt, type

% clear

This will clear all text and leave you with the % prompt at the top of the window.

cat (concatenate)

The command cat can be used to display the contents of a file on the screen. Type:

% cat sample.txt

As you can see, the file is longer than than the size of the window, so it scrolls past making it unreadable.

less

The command less writes the contents of a file onto the screen a page at a time. Type

% less science.txt

Press the [space-bar] if you want to see another page, and type [q] if you want to quit reading. As you can see, less is used in preference to catfor long files.

head

By default, the head command prints the first 10 lines from the file which is given followed by the head command. In the case of more than one file with head command displays separate output with a header identifying the file name. Head command has a lot of useful and also very helpful switches.

First clear the screen then type in the following ways

1. head : The head command alone
Which displays the first 10 line by-default.

% head sample.txt

Example:

first 10 line……
………………….
…………………..

2. head : More than one file
Displays separate output with a header identifying the file name.

% head sample.txt sample2.txt

Example:

sample.txt
file content……
……………………

sample2.txt
file content…….
…………………….

3. head -n : Displays first “n” lines from the text file.

% head -5 sample.txt

Will print the first  5 lines.

tail

The tail command writes the last ten lines of a file to the screen.

Clear the screen and type

% tail sample.txt

2.5 Searching the contents of a file

Simple searching using less

Using less, you can search though a text file for a keyword (pattern). For example, to search through sample.txt for the word ‘shekhar’, type

% less sample.txt

then, still in less, type a forward slash [/] followed by the word to search

/shekhar

As you can see, less finds and highlights the keyword. Type [n] to search for the next occurrence of the word.

grep

grep is one of many standard UNIX utilities. It searches files for specified words or patterns. First clear the screen, then type

% grep sample sample.txt

grep will print out each line containing word sample.

Note: The grep command is case sensitive; It treats sample and Sample differently.

To make it case-insensitive, use the option -i

e.g. % grep sample sample.txt

To search for a phrase or pattern, It should be  enclosed with single quotes as shown below. For example to search for ‘learning Unix’, type

% grep -i ‘learning  Unix’ sample.txt

Some of the other options of grep are:

-v display those lines that do NOT match
-n precede each matching line with the line number
-c print only the total count of matched lines

Note: we can use more than one option at a time as shown below in the example. Let’s see how to print the number of lines without the words sample or Sample is

% grep -ivc sample sample.txt

wc (word count)

The command wc is used for word count. To do a word count on sample.txt, type

% wc -w sample.txt

To find out how many lines the file has, type

% wc -l science.txt


Let’s see summary of all the commands learnt above

Command Meaning
cp file1 file2 copy file1 and call it file2
mv file1 file2 move or rename file1 to file2
rm file remove a file
rmdir directory remove a directory
cat file display a file
less file display a file a page at a time
head file display the first few lines of a file
tail file display the last few lines of a file
grep ‘keyword’ file search a file for keywords
wc file count number of lines/words/characters in file
Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...