Topic: Working with Files
1) Creation of Files
2) Viewing of Files
3) Copying of Files
4) Comparison of Files
5) Renaming of files
6) Deleting Files
1) Creation of Files:
In Linux, we can create files in the following ways:
1) By using touch command (to create empty file)
2) By using cat command
3) By using editors like gedit, vi, nano etc
cat Command:
cat > file1.txt
Eg:
$ cat > file1.txt
Hello Friends
Listen Carefully
ctrl+d ----->To save and exit
If file1.txt is not already available, then file1.txt will be created with our provided data.
If file1.txt is already available with some content, then old data will be over written with
our provided new data.
Instead of overwriting, if we want append operation then we should use >> with cat
command.
cat >> file1.txt
extra content
ctrl+d
Q1) What is the difference between Touch and Cat?
touch for creating empty file where as cat for creating a file with some content.
Q2) How we can perform overwriting and appending with cat Command?
> meant for overwriting
>> meant for appending/concatenation
Q3) If we are using Touch Comamnd, but the File is already available then
what will happend?
The content of the file won't be changed. But last modified date and time
(i.e., timestamp) will be updated.
2) View Content of the Files
We can view content of the file by using the following commands
1) cat
2) tac
3) rev
4) head
5) tail
6) less
7) more
1. View Content of the File by using cat Command:
$ cat < file1.txt OR $ cat file1.txt
cat < file1.txt
This is first line
This is second line
This is third line
This is extra line
$ cat file1.txt
This is first line
This is second line
This is third line
This is extra line
While viewing file content we can include line numbers by using -n option.
While display file content we can skip blank lines by using -b option.
We can view multiple files content at a time by using cat command.
Various utilities of cat Command:
1) To create new file with some content
$ cat > filename
data
ctrl+d
2) To append some extra data to existing file
$ cat >> filename
extra data
ctrl+d
3) To view content of file
$ cat < filename or $ cat filename
4) Copy content of one file to another file
$ cat input.txt > output.txt
5) To copy content of multiple files to a single file
$ cat file1.txt file2.txt file3.txt > file4.txt
6) Merging/appending of one file content to another file
$ cat file1.txt >> file2.txt
2. tac Command:
It is the reverse of cat.
It will display file content in reverse order of lines. i.e first line will become last line and
last line will become first line.
This is vertical reversal.
3. rev Command:
rev means reverse.
Here each line content will be reversed.
It is horizontal reversal.
Note:
cat command will display total file content at a time. It is best suitable for small files. If the
file contains huge lines then it is not recommended to use cat command. We should go for
head, tail, less and more commands.
4. head Command:
We can use head command to view top few lines of content.
head file1.txt
It will display top 10 lines of file1.txt.
10 is the default value of number of lines.
head -n 30 file1.txt OR head -30 file1.txt
To display top 30 lines of the file.
Instead of 30 we can specify any number.
✽ head -n -20 file1.txt
To display all lines of file1.txt except last 20 lines.
✽ head -c 100 file1.txt
To display first 100 bytes of file content.
5. tail Command:
We can use tail command to view few lines from bottom of the file.
It is opposite to head command.
✽ tail file1.txt
Last 10 lines will be displayed.
✽ tail -n 30 file1.txt OR tail -30 file1.txt OR tail -n -30 file1.txt
It will display last 30 lines.
✽ tail -n +4 file1.txt
It will display from 4th line to last line
✽ tail -c 200 file1.txt
It will display 200 bytes of content from bottom of the file.
6. more Command:
We can use more command to view file content page by page.
✽ more file1.txt
It will display first page.
7. less Command:
By using more command, we can view file content page by page only in forward direction.
If we want to move either in forward direction or in backward direction then we should go
for less command.
ls Command:
We can use ls command to listout all files and directories present in the given directory.
We can get manual documentation for any command by using man.
man ls
It provides complete information about ls command.
Various options of ls Command:
1) ls
It will display all files and directories according to alphabetical order of names.
2) ls -r
It will display all files and directories in reverse of aplhabetical order.
3) ls | more
To display content line by line
(To come out we have to use q)
4) ls | pg
To display content page by page.
Each page contains 20 lines of content.
(To come out we have to use q)
5) ls -l
To display long listing of files
6) ls -t
To display all files based on last modified date and time. Most recent is at top and oldare at
bottom.
7) ls -rt
To display all files based on reverse of last modified date and time. Old files are at topand
recent files are at bottom.
8) ls -a
a means all
To display all files including hidden files. Here . and .. also will be displayed.
9) ls -A
A means almost all
To display all files including hidden files except . and ..
10) ls -F
To display all files by type.
11) ls -f
To disable colors
12) ls -R
R means Recursive.
It will list all files and directories including sub directory contents also. By default lswill
display only direct contents but not sub directory contents.
Copying of Files:
cp file1.txt file2.txt
If file2.txt not available, then file2.txt will be created and the content will be copied.
If file2.txt is already available and contanis some data then that data will be overwritten with
file1.txt data.
Before overwriting if we want confirmation, then we should go for -i option.
i means interactive.
cp -i file1.txt file2.txt
$ cp -i a.txt b.txt
cp: overwrite 'b.txt'?
If we want verbose output then we can use -v option.
cp -v file1.txt file2.txt
$ cp -v a.txt b.txt
'a.txt' -> 'b.txt'
cp file1.txt file2.txt file3.txt file4.txt output
Moving and Renaming Directories:
Both moving and renaming activities can be performed by using single command: mv
1) Renaming of Files:
mv oldname newname
mv file1.txt file2.txt
file1.txt will be renamed to file2.txt
2) Renaming of Directories:
mv dir1 dir2
dir1 will be renamed to dir2
3) Moving of files from one directory to another directory:
mv dir1/* dir2
All files of dir1 will be moved to dir2. After executing this command, dir1 will come empty.
4) Move total directory to another directory:
mv dir1 dir2
dir1 will be moved to dir2
In the case of overwriting, if we want confirmation alert then we can use -i option
with mv command.
$ mv -i a.txt d.txt dir1
mv: overwrite 'dir1/a.txt'?
cmp Comamnd:
It will compare byte by byte.
cmp file1.txt file2.txt
If content is same then we won't get any output.
If the content is different, then it provides information about only first difference. Byte
number and line number will be provided.
$ cmp a.txt c.txt
a.txt c.txt differ: byte 7, line 2
diff Comamnd:
It will show all differences in the content.
diff file1.txt file2.txt
If the content is the same then no output.
If the content is different then it will show all differences.
$ diff a.txt b.txt
$ diff a.txt c.txt
2,3c2,3
< Bunny
< Chinny
---
> bunny
> chinny
For the diff command we can use the following options.
-q shows message when files are different.
-s shows message when files are same | identical
-y shows comparison line by line (parallel comparison)
WC COMMAND:
We can use wc command to count number of lines, words and characters present in the
given file.
wc filename
no_of_lines no_of_words no_of_characters filename
We can use the following options with wc Command
-l -- To print only number of lines
-w ---> To print only number of words
-c -To print only number of characters
-lw----> To print only number of lines and words
-lc-- To print only number of lines and characters
-wc - To print only number of words and characters
Topic: Compression and Uncompression of Files (tar, gzip, gunzip, bzip2, bunzip2)
As the part of admin activity, it is very common requirement to pack and compress a group of files.
The main advanatages are:
1) It improves memory utilization
2) Transportation will become very easy
3) It reduces download times
etc
This process involves the following 2 activities:
1) Creation of Archive file
2) Apply compression algorithms on that archive file
1)Creation of Archive File
We can group multiple files and directories into a single archive file by using tar
command.
tar ====> tape archive
A) To create tar file
tar -cvf demo.tar file1.txt file2.txt file3.txt
tar -cvf demo.tar *
B) To display table of contents of tar file
tar -tvf demo.tar
C) To Extract contents of tar file
tar -xvf demo.tar