Introduction To Unix: Unit 2:the File System and Some File Handling Commands
Introduction To Unix: Unit 2:the File System and Some File Handling Commands
● File type
● File name
● Physical file size
● File protection
● file time stamp
THE FILE SYSTEM
FILE TYPES
1. Ordinary files : An ordinary file is a file on the system that contains data, text, or
program instructions.
1) Text File
2) Binary file
1. Directory files : UNIX directories are equivalent to folders. A directory file contains
an entry for every file and subdirectory that it houses. If you have 10 files in a
directory, there will be 10 entries in the directory. Each entry has two components.
● $pwd
/home/mint
$cd /abc/xyz
$pwd
/abc/xyz
● $pwd
/home/var/ftp
$cd /var/ftp/pub
$pwd
/var/ftp/pub
cd COMMAND
cd command in linux known as change directory command. It is used to change current working
directory.
Syntax: $ cd [directory]
● cd /: this command is used to change directory to the root directory, The root directory is the first
directory in your filesystem hierarchy.
Ex:$ cd /
● Cd ~
$pwd
● cd .. : this command is used to move to the parent directory of current directory, or the directory
one level up from the current directory. “..” represents parent directory.
Ex: $ cd ..
Mkdir command
It is used to create directory.This command can create multiple directories at
once as well as set the permissions for the directories
SUBOPTIONS
● -v or --verbose: It displays a message for every directory created.
mkdir -v [directories]
● To create directory under current directory
$mkdir mydir
● To create no.of sub directories with one mkdir
Remove the directories mydir1, mydir2, and mydir3, if they are empty. If any of these
directories are not empty, then an error message will be printed for that directory, and
the other directories will be removed.
3. $mkdir mydir
$ls
Cd \mydir
Rmdir mydir
ABSOLUTE PATHNAME
A path is a unique location to a file or a folder in a file system of an OS.A path to a
file is a combination of / and alpha-numeric characters.
Absolute Path-name
An absolute path is defined as the specifying the location of a file or directory from
the root directory(/).
An absolute path is defined as specifying the location of a file or directory from
the root directory(/). In other words,we can say that an absolute path is a complete
path from start of actual file system from / directory.
To write an absolute path-name:
● Start at the root directory ( / ) and work down.
● Write a slash ( / ) after every directory name (last one is optional)
For Example :
$cat abc.sql
will work only if the fie “abc.sql” exists in your current directory. However, if this file is
not present in your working directory and is present somewhere else say in /home/kt ,
then this command will work only if you will use it like shown below:
cat /home/kt/abc.sql
In the above example, if the first character of a pathname is /, the file’s location must be
determined with respect to root. When you have more than one / in a pathname, for
each such /, you have to descend one level in the file system like in the above kt is one
level below home, and thus two levels below root.
RELATIVE PATH
● Relative path is defined as the path
related to the present working
directly(pwd). It starts at your current
directory and never starts with a / .
● To be more specific let’s take a look on
the below figure in which if we are
looking for photos then absolute path
for it will be provided as
/home/jono/photos but assuming that
we are already present in jono
directory then the relative path for
the same can be written as simple
photos.
UNIX offers a shortcut in the relative pathname– that uses either the current or parent
directory as reference and specifies the path relative to it.
/home/kt/abc
$cd ..
$pwd
As shown in the current example, if we are currently in directory /home/kt/abc and now
you can use .. as an argument to cd to move to the parent directory /home/kt
ls COMMAND
ls is a Linux shell command that lists directory contents of files and directories
Options:
Ex:$cat a.txt
● Cat command can display content in reverse order using tac command.
Command: $tac filename
COPYING A FILE
● cp stands for copy. This command is used to copy files or group of files or
directory.
● It creates an exact image of a file on a disk with different file name.
● cp command require at least two filenames in its arguments.
● If the command contains two file names, then it copy the contents of 1st file to the
2nd file. If the 2nd file doesn’t exist, then first it creates one and content is copied
to it. But if it existed then it is simply overwritten without any warning.
$ ls new
$ ls new
a.txt b.txt
Two directory names : If the command contains two directory names, cp copies all
files of the source directory to the destination directory, creating any files or directories
needed. This mode of operation requires an additional option, typically R, to indicate
the recursive copying of directories.
$ cat b.txt
hello
● -b(backup): With this option cp command creates the backup of the
destination file in the same folder with the different name and in different
format.
$ ls
a.txt b.txt
$ cp -b a.txt b.txt
$ ls
a.txt b.txt b.txt~
-f(force): If the system is unable to open destination file for writing operation
because the user doesn’t have writing permission for this file then by using -f
option with cp command, destination file is deleted first and then copying of
content is done from source to destination file.
$ ls -l b.txt
-r-xr-xr-x+ 1 User User 3 Nov 24 08:45 b.txt
User, group and others doesn't have writing permission.
Without -f option, command not executed
$ cp a.txt b.txt
cp: cannot create regular file 'b.txt': Permission denied
With -f option, command executed successfully
$ cp -f a.txt b.txt
Copying using * wildcard: The star wildcard represents anything i.e. all files and
directories.
$ ls
$ cp *.txt Folder1
$ ls Folder1
● Once we delete the files using rm command then it is not able to recover the
contents of files and directories.
Syntax: rm [OPTION]... FILE…
● Removing one file at a time : $rm filename
$ rm a.txt
● Removing more than one file at a time:$rm file1 file2
$ rm b.txt c.txt
● -i (Interactive Deletion): Like in cp, the -i option makes
the command ask the user for confirmation before
removing each file, you have to press y for confirm
deletion, any other key leaves the file un-deleted.
$ rm -i d.txt
rm: remove regular empty file 'd.txt'? Y
$ ls
e.txt
● -f (Force Deletion): rm prompts for confirmation removal if a file is write protected.
The -f option overrides this minor protection and removes the file forcefully.
$ ls -l
total 0
$ rm e.txt
$ ls
e.txt
$ rm -f e.txt
$ ls
-r (Recursive Deletion): With -r(or -R) option rm command performs a tree-walk and will
delete all the files and sub-directories recursively of the parent directory. At each stage it
deletes everything it finds.
$ ls
$ cd A
$ ls
B C
$ ls B
a.txt b.txt
$ ls C
c.txt d.txt
Now, deletion from A directory(as parent directory) will be done as:
$ rm *
$ rm -r *
$ ls
OPTIONS:
● -i option
● -f option
● -n option
● -b option
more COMMAND IN UNIX
more command is used to view the text files in the command prompt, displaying one screen at a
time in case the file is large (For example log files). The more command also allows the user do
scroll up and down through the page.
From above example ,the contents of chap01 on the screen will be visible one page at a
time.At the bottom of the screen we see the filename and the percentage of the file that has
been viewed. ‘q’ is one of the internal command of more Pager from which more pager can
be exited
● NAVIGATION:
1. More uses space bar to scroll forward a page.
2. To move forward 1 page use space bar or ‘f’
3. To move one page back ude ‘b’
● THE REPEAT FEATURES:
By using the repeat factor as a command prefix simply repeat the command that
many times.
$ wc state.txt
5 7 63 state.txt
$ wc state.txt capital.txt
5 7 63 state.txt
5 5 45 capital.txt
10 12 108 total
When more than file name is specified in argument then command will display four-columnar
output for all individual files plus one extra row displaying total number of lines, words and
characters of all the files specified in argument, followed by keyword total.
OPTIONS
● -l: This option prints the number of lines present in a file. With this option wc command displays
two-columnar output, 1st column shows number of lines present in a file and 2nd itself represent
the file name.
EX:
With one file name
$ wc -l state.txt
5 state.txt
With more than one file name
$ wc -l state.txt capital.txt
5 state.txt
5 capital.txt
10 total
● -w: This option prints the number of words present in a file. With this option wc command
displays two-columnar output, 1st column shows number of words present in a file and 2nd is
the file name.
EX:
With one file name
$ wc -w state.txt
7 state.txt
$ wc -w state.txt capital.txt
7 state.txt
5 capital.txt
12 total
● -c: This option displays count of bytes present in a file. With this option it display two-columnar
output, 1st column shows number of bytes present in a file and 2nd is the file name.
EX:
With one file name
$ wc -c state.txt
63 state.txt
$ wc -c state.txt capital.txt
63 state.txt
45 capital.txt
108 total
od COMMAND
● od command in Linux is used to convert the content of input in different formats with
octal format as the default format.
● This command is especially useful when debugging Linux scripts for unwanted changes
or characters.
● If more than one file is specified, od command concatenates them in the listed order to
form the input.
● It can display output in a variety of other formats, including hexadecimal, decimal, and
ASCII.
Syntax :
od [OPTION]... [FILE]...
OPTIONS
SYNTAX: $ od -b input.txt
The first column in the output of od represents the byte offset in file.
SYNTAX :$ od -c input.txt
cmp COMMAND
cmp command in Linux/UNIX is used to compare the two files byte by byte and helps you to find out
whether the two files are identical or not.
Syntax:
EX:
$cat list1.txt
Hello
$cat list2.txt
Hi
EX:
// displaying contents of // displaying contents
file1 // of file2 //
$cat file1.txt $cat file2.txt
Apaar
Apaar
Hemant
Ayush Rajput
Lucky
Deepak
Pranjal Thakral
Hemant
$comm file1.txt file2.txt
Apaar
Ayush Rajput
Deepak
Hemant
Lucky
Pranjal Thakral
diff COMMAND
diff stands for difference. This command is used to display the differences in the files by
comparing the files line by line.
cmp and comm, it tells us which lines in one file have is to be changed to make the two files
identical.
The important thing to remember is that diff uses certain special symbols and instructions
that are required to make two files identical. It tells you the instructions on how to change the
first file to make it match the second file.
c : change
d : delete
Syntax : diff [options] File1 File2
Ex: $cat > a.txt
In the given output of
diff command
Chapter1
1,2c1 can be described
as,lines from 1 to 2 from
Chapter2 first file need to be
changed to be changed
$cat > b.txt to match 1st line in file
2.
Chapters
1,2c1
< chapter1 …... Lines preceded by a < are lines from the first file.
< chapter2
> chapters……. Lines preceded by > are lines from the second file.
Spell and ispell COMMANDS
● The spell command is a spell-checking program which scans a text file for
misspelled words, and prints each misspelled word on its own line.
● spell is a very minimalistic spell-checking program, based on the original UNIX spell
checker. It reads the contents of file FILE, word for word, checking them against its
dictionary. If a word does not correspond with any of spell's dictionary words, the
word is printed.
Checks the spelling of files words.txt and words2.txt, printing the file name (-o)
and the line number (-n) of each misspelled word.
ispell
ispell command is used as a spell checker in Linux. Generally, it will scan the given files or
anything from standard input then it check for misspellings. Finally it allows the user to correct the
words interactively.
Syntax:ispell check [options] filename
Options:
● -a : To check individual words.
● -c : Check a file for specific error.
SYNTAX:$gunzip filename
EX:$gunzip abc.txt.gz
Zip and Unzip COMMAND
zip is used to compress the files to reduce file size and also used as file package utility.
The zip program puts one or more compressed files into a single zip archive, along with
information about the files (name, path, date, time of last modification, protection, and check
information to verify file integrity).
Syntax :
zip [options] zipfile files_list
$zip myfile.zip filename.txt
Unzip will list, test, or extract files from a ZIP archive, commonly found on Unix
systems. The default behavior (with no options) is to extract into the current
directory (and sub-directories below it) all files from the specified ZIP archive.
Syntax :
$unzip myfile.zip
-d Option: Removes the file from the zip archive. After creating a zip file, you
can remove a file from the archive using the -d option.
Syntax :
$zip –d filename.zip file.txt
Command :
$zip –d myfile.zip hello7.c OR $unzip myfile.zip hello7.c
After removing hello7.c from myfile.zip file, the
Suppose we have following files in my current
files can be restored with unzip command
directory are listed below:
Command:
hello1.c
$unzip myfile.zip
hello2.c $ls command
hello3.c Output :
hello1.c
hello4.c
hello2.c
hello5.c hello3.c
hello6.c hello4.c
hello5.c
hello7.c
hello6.c
Hello8.c hello8.c
The hello7.c file is removed from
$zip –d myfile.zip hello7.c
zip file