Module 1 Part2
Module 1 Part2
A directory is a binary file used to track and locate other files and directories. The binary
format is used so that directories containing large numbers of filenames can be search
quickly.
3) Device (Special) Files
Device or special files are used for device I/O on UNIX systems. They appear in a file
system just like an ordinary file or a directory. On UNIX systems there are two flavors of
special files for each device, character special files and block special files. When a
character special file is used for device I/O, data is transferred one character at a time.
This type of access is called raw device access. When a block special file is used for
device I/O, data is transferred in large fixed-size blocks. This type of access is called
block device access.
In fact, such a thing is not really very hidden…the ls command simply does not show it by
default.
One can readily see it by specifying “ls -a”
“ls -d .*” (note that without the “-d” one would see the content of any hidden directories as
well).
Everything on your Unix system is located under the / directory, known as the root directory..
The /bin directory contains the essential user binaries (programs) that must be present when the
system is mounted in single-user mode. Applications such as Firefox are stored in /usr/bin, while
important system programs and utilities such as the bash shell are located in /bin. The /usr
directory may be stored on another partition – placing these files in the /bin directory ensures the
system will have these important utilities even if no other file systems are mounted. The /sbin
directory is similar – it contains essential system administration binaries.
The /boot directory contains the files needed to boot the system –
The /dev directory contains a number of special files that represent devices. These are not actual
files as we know them, but they appear as files – for example, /dev/sda represents the first SATA
drive in the system. If you wanted to partition it, you could start a partition editor and tell it to
edit /dev/sda. This directory also contains pseudo-devices, which are virtual devices that don’t
actually correspond to hardware. For example, /dev/random produces random numbers. /dev/null
is a special device that produces no output and automatically discards all input – when you pipe
the output of a command to /dev/null, you discard it.
The /etc directory contains configuration files, which can generally be edited by hand in a text
editor. Note that the /etc/ directory contains system-wide configuration files – user-specific
configuration files are located in each user’s home directory.
The /lib directory contains libraries needed by the essential binaries in the /bin and /sbin folder.
Libraries needed by the binaries in the /usr/bin folder are located in /usr/lib.
The /root directory is the home directory of the root user. Instead of being located at /home/root,
it’s located at /root. This is distinct from /, which is the system root directory.
Every file, apart from root must have a parent. Home directory is the parent of mthomas,
mthomas is the parent of bin, class_stuff, .profile. In these parent-child relationships, the parent
is always a directory.
1.17 The Home Directory and The HOME variable
When you log on to the system, UNIX automatically places you in a directory called HOME
directory. It is created by the system when a user account is opened
The shell variable HOME knows your home directory.
$ echo $HOME
/home/kumar
The /bin , /usr/bin , and /usr/local/bin directories are typically included in most
users' $PATH setting (although this varies from implementation to implementation).
The superuser also typically has /sbin and /usr/sbin entries for easily executing system
administration commands. The current directory ( . ) is sometimes included by users as well,
allowing programs residing in the current working directory to be executed directly. System
administrators as a rule do notinclude it in $PATH in order to prevent the accidental execution
of scripts residing in the current directory, such as may be placed there by a malicious tarbomb.
In that case, executing such a program requires specifying an absolute
( /home/userjoe/bin/script.sh ) or relative path ( ./script.sh ) on the command line.
When a command name is specified by the user or an exec call is made from a program, the
system searches through $PATH , examining each directory from left to right in the list, looking
for a filename that matches the command name. Once found, the program is executed as a child
process of the command shell or program that issued the command.
To find out what your path is, at the Unix shell prompt, enter:
PATH echo $
/usr2/username/bin:/usr/local/bin:/usr/bin:.
If you are using sh, ksh, or bash, at the shell prompt, enter:
Absolute Path-name
An absolute path is defined as the specifying the location of a file or directory from the root
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 file “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.
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.
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. A relative path-name uses one of these cryptic
symbols:
.(a single dot) - this represents the current directory.
..(two dots) - this represents the parent directory.
1.21 Directory Commands
1) Pwd: As the name states, command 'pwd' prints the current working directory or simply
the directory user is, at present. It prints the current directory name with the complete
path starting from root (/). This command is built in shell command
2) Cd: The cd command is used to change the current directory (i.e., the directory in which
the user is currently working)
3) Mkdir: the mkdir command in Linux/Unix allows users to create or make new
directories. mkdir stands for “make directory
4) Rmdir: rmdir command is used remove empty directories from the filesystem
in Linux. The rmdir command removes each and every directory specified in
the command line only if these directories are empty. So if the specified directory has
some directories or files in it then this cannot be removed by rmdir command.
1.22 Dot (.) and double dots(..) notations to represent present and
parent directories and their usage in relative path names.
Eg:
$ pwd
/home/kumar/progs/data/text
$cd ..
$pwd
/home/kumar/progs/data
$ cd ../..
$pwd
/home/kumar
Dot (.)
Eg:
$cp ../sharma/profile .
Copies the file profile to the current working directory
1.23 File related commands
1) cat: The cat (short for “concatenate“) command is one of the most frequently used command
in Linux/Unix like operating systems. cat command allows us to create single or multiple files,
view contain of file, concatenate files and redirect output in terminal or files.
General Syntax
# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
narad:x:500:500::/home/narad:/bin/bash
In below example, it will display contents of test and test1 file in terminal.
Hello everybody
Hi world,
# cat >test2
Awaits input from user, type desired text and press CTRL+D (hold down Ctrl Key and type ‘d‘)
to exit. The text will be written in test2 file. You can see content of file with
following cat command.
# cat test2
If file having large number of content that won’t fit in output terminal and screen scrolls up very
fast, we can use parameters more and less with cat command as show above.
With -n option you could see the line numbers of a file song.txt in the output terminal.
# cat -n song.txt
2) mv command:
mv stands for move. mv is used to move one or more files or directories from one place to
another in file system like UNIX. It has two distinct functions:
(i) It rename a file or folder.
(ii) It moves group of files to different directory.
No additional space is consumed on a disk during renaming. This command normally works
silently means no prompt for confirmation.
Syntax:
mv [Option] source destination
Let us consider 5 files having name a.txt, b.txt and so on till e.txt.
To rename the file a.txt to geek.txt(not exist):
$ ls
a.txt b.txt c.txt d.txt
$ mv a.txt geek.txt
$ ls
b.txt c.txt d.txt geek.txt
If the destination file doesn’t exist, it will be created. In the above command mv simply replaces
the source filename in the directory with the destination filename(new name). If the destination
file exist, then it will be overwrite and the source file will be deleted. By default, mv doesn’t
prompt for overwriting the existing file
Options:
1. -i (Interactive): Like in cp, the -i option makes the command ask the user for confirmation
before moving a file that would overwrite an existing file, you have to press y for confirm
moving, any other key leaves the file as it is. This option doesn’t work if the file doesn’t exist, it
simply rename it or move it to new location.
$ ls
b.txt c.txt d.txt geek.txt
$ cat geek.txt
India
$ cat b.txt
geeksforgeeks
$ mv -i geek.txt b.txt
mv: overwrite 'b.txt'? y
$ ls
b.txt c.txt d.txt
$ cat b.txt
India
2. -f (Force): mv prompts for confirmation overwriting the destination file if a file is write
protected. The -f option overrides this minor protection and overwrite the destination file
forcefully and delete the source file.
$ ls
b.txt c.txt d.txt geek.txt
$ cat b.txt
geeksforgeeks
$ ls -l b.txt
-r--r--r--+ 1 User User 13 Jan 9 13:37 b.txt
$ mv geek.txt b.txt
mv: replace 'b.txt', overriding mode 0444 (r--r--r--)? n
$ ls
b.txt c.txt d.txt geek.txt
$ mv -f geek.txt b.txt
$ ls
b.txt c.txt d.txt
$ cat b.txt
India
3. -n (no-clobber): With -n option, mv prevent an existing file from being overwritten.
In the following example the effect is for nothing to happen as a file would be overwritten.
$ ls
b.txt c.txt d.txt geek.txt
$ cat b.txt
geeksforgeeks
$ mv -n geek.txt b.txt
$ ls
b.txt c.txt d.txt geek.txt
$ cat b.txt
geeksforgeeks
4. –b (backup): With this option it is easier to take a backup of an existing file that will be
overwritten as a result of the mv command. This will create a backup file with the tilde
character(~) appended to it.
$ ls
b.txt c.txt d.txt geek.txt
$ mv -b geek.txt b.txt
$ ls
b.txt b.txt~ c.txt d.txt
5. –version: This option is used to display the version of mv which is currently running on your
system.
$ mv --version
mv (GNU coreutils) 8.26
Packaged by Cygwin (8.26-2)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later .
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
3) rm command:
rm stands for remove here. rm command is used to remove objects such as files, directories,
symbolic links and so on from the file system like UNIX. To be more precise, rm removes
references to objects from the filesystem, where those objects might have had multiple references
(for example, a file with two different names). By default, it does not remove directories.
This command normally works silently and you should be very careful while
running rm command because once you delete the files then you are not able to recover the
contents of files and directories.
Syntax:
rm [OPTION]... FILE...
Let us consider 5 files having name a.txt, b.txt and so on till e.txt.
$ ls
a.txt b.txt c.txt d.txt e.txt
Removing one file at a time
$ rm a.txt
$ ls
b.txt c.txt d.txt e.txt
$ ls
d.txt e.txt
Options:
1. -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
2. -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
-r--r--r--+ 1 User User 0 Jan 2 22:56 e.txt
$ rm e.txt
rm: remove write-protected regular empty file 'e.txt'? n
$ ls
e.txt
$ rm -f e.txt
$ ls
3. -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. Normally, rm wouldn’t delete the directories but when used with this option,
it will delete.
Below is the tree of directories and files:
$ ls
A
$ cd A
$ ls
B C
$ ls B
a.txt b.txt
$ ls C
c.txt d.txt
$ rm -r *
$ ls
Every directory and file inside A directory is deleted.
4. –version: This option is used to display the version of rm which is currently running on your
system.
$ rm --version
rm (GNU coreutils) 8.26
Packaged by Cygwin (8.26-2)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later .
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ rm -file.txt
rm: unknown option -- l
Try 'rm ./-file.txt' to remove the file '-file.txt'.
Try 'rm --help' for more information.
$ rm -- -file.txt
$ ls
3) cp command
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.
Syntax:
cp [OPTION] Source Destination
cp [OPTION] Source Directory
cp [OPTION] Source-1 Source-2 Source-3 Source-n Directory
First and second syntax is used to copy Source file to Destination file or Directory.
Third syntax is used to copy multiple Sources(files) to Directory.
cp command works on three principal modes of operation and these operations depend
upon number and type of arguments passed in cp command :
1. Two file names : 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. So be
careful when you choose destination file name.
2. cp Src_file Dest_file
Suppose there is a directory named geeksforgeeks having a text file a.txt.
Example:
$ ls
a.txt
$ cp a.txt b.txt
$ ls
a.txt b.txt
3. One or more arguments : If the command has one or more arguments, specifying file
names and following those arguments, an argument specifying directory name then this
command copies each source file to the destination directory with the same name, created if
not existed but if already existed then it will be overwritten, so be careful !!.
4. cp Src_file1 Src_file2 Src_file3 Dest_directory
Suppose there is a directory named geeksforgeeks having a text file a.txt, b.txt and a
directory name new in which we are going to copy all files.
Example:
1. $ ls
2. a.txt b.txt new
3.
4. Initially new is empty
5. $ ls new
6.
7. $ cp a.txt b.txt new
8.
9. $ ls new
10. a.txt b.txt
11. Note: For this case last argument must be a directory name. For the above command to
work, Dest_directory must exist because cp command won’t create it.
12. 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.
13. cp -R Src_directory Dest_directory
In the above command, cp behavior depend upon whether Dest_directory is exist or not. If
the Dest_directory doesn’t exist, cp creates it and copies content
of Src_directory recursively as it is. But if Dest_directory exists then copy
of Src_directorybecomes sub-directory under Dest_directory.
Options:
There are many options of cp command, here we will discuss some of the useful options:
Suppose a directory named geeksforgeeks contains two files having some content named
as a.txt and b.txt. This scenario is useful in understanding the following options.
$ ls geeksforgeeks
a.txt b.txt
$ cat a.txt
GFG
$ cat b.txt
GeeksforGeeks
1. -i(interactive): i stands for Interactive copying. With this option system first warns the user
before overwriting the destination file. cp prompts for a response, if you press y then it
overwrites the file and with any other option leave it uncopied.
$ cp -i a.txt b.txt
cp: overwrite 'b.txt'? y
$ cat b.txt
GFG
2. -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~
3. -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
$ ls gfg/
a.txt b.txt b.txt~ Folder1 Folder2
5. -p(preserve): With -p option cp preserves the following characteristics of each source file in
the corresponding destination file: the time of the last data modification and the time of the last
access, the ownership (only if it has permissions to do this), and the file permission-bits.
Note: For the preservation of characteristics you must be the root user of the system, otherwise
characteristics changes.
$ ls -l a.txt
-rwxr-xr-x+ 1 User User 3 Nov 24 08:13 a.txt
$ cp -p a.txt c.txt
$ ls -l c.txt
-rwxr-xr-x+ 1 User User 3 Nov 24 08:13 c.txt
As we can see above both a.txt and c.txt(created by copying) have same characteristics.
Examples:
Copying using * wildcard: The star wildcard represents anything i.e. all files and directories.
Suppose we have many text document in a directory and wants to copy it another directory, it
takes lots of time if we copy files 1 by 1 or command becomes too long if specify all these file
names as the argument, but by using * wildcard it becomes simple.
Initially Folder1 is empty
$ ls
a.txt b.txt c.txt d.txt e.txt Folder1
$ cp *.txt Folder1
$ ls Folder1
a.txt b.txt c.txt d.txt e.txt
5) wc command
wc stands for word count. As the name implies, it is mainly used for counting purpose.
It is used to find out number of lines, word count, byte and characters count in the files
specified in the file arguments.
By default it displays four-columnar output.
First column shows number of lines present in a file specified, second column shows
number of words present in the file, third column shows number of characters present in
file and fourth column itself is the file name which are given as argument.
Syntax:
wc [OPTION]... [FILE]...
Let us consider two files having name state.txt and capital.txt containing 5 names of the Indian
states and capitals respectively.
$ cat state.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh
$ cat capital.txt
Hyderabad
Itanagar
Dispur
Patna
Raipur
Passing only one file name in the argument.
$ wc state.txt
5 7 63 state.txt
OR
$ wc capital.txt
5 5 45 capital.txt
Passing more than one file name in the argument.
$ wc state.txt capital.txt
5 7 63 state.txt
5 5 45 capital.txt
10 12 108 total
Note : 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:
1. -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.
With one file name
$ wc -l state.txt
5 state.txt
The first column in the output of od represents the byte offset in file.
2. -c Option :It displays the contents of input in character format.
SYNTAX :
$ od -c input.txt
3. -An Option :It displays the contents of input in character format but with no offset
information.
SYNTAX :
$ od -An -c input.txt
4. -A Option :It displays the contents of input in different format by concatenation some special
character with -A.
For example:
1. -Ax for Hexadecimal format(we concatenate x with -A)
2. -Ao for Hexadecimal format(we concatenate o with -A)
3. -Ad for Hexadecimal format(we concatenate d with -A)
SYNTAX :
$ od -Ax input.txt
$ od -Ao input.txt
$ od -Ad input.txt