UNIX
UNIX
SYLLABUS:
Introduction to unix: architecture of unix, features of unix , unix commands
path, man, echo, printf, script, passwd, uname, who, date, stty, pwd, cd,
mkdir, rmdir, ls, cp, mv, rm, cat, more, wc, lp, od, tar,gzip.
What is UNIX?
The UNIX operating system is a set of programs that act as a link between the computer and the
user.
The computer programs that allocate the system resources and coordinate all the details of the
computer's internals is called the operating system or kernel.
Users communicate with the kernel through a program known as the shell. The shell is a
command line interpreter; it translates commands entered by the user and converts them into a
language that is understood by the kernel.
Unix was originally developed in 1969 by a group of AT&T employees at Bell Labs,
including KenThompson, Dennis Ritchie, Douglas McIlroy, and Joe Ossanna.
There are various Unix variants available in the market. Solaris Unix, AIX, HP Unix and
BSD are few examples. Linux is also a flavor of Unix which is freely available.
Several people can use a UNIX computer at the same time; hence UNIX is called a
multiuser system.
A user can also run multiple programs at the same time; hence UNIX is called
multitasking
FEATURES OF UNIX:
Portable
Portability means softwares can works on different types of hardwares in same way. Unix
kernel and application programs supports their installation on any kind of hardware platform.
UNIX can be installed on many hardware platforms. Its widespread use can be traced to the
decision to develop it using the C language.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 1
UNIT-1
Multiuser
Unix is a multiuser system means multiple users can access system resources like memory/
ram/ application programs at same time. The UNIX design allows multiple users to
concurrently share hardware and software
Multitasking
UNIX allows a user to run more than one program at a time. In fact more than one program
can be running in the background while a user is working foreground.
Networking
While UNIX was developed to be an interactive, multiuser, multitasking system, networking
is also incorporated into the heart of the operating system. Access to another system uses a
standard communications protocol known as Transmission Control Protocol/Internet Protocol
(TCP/IP).
Hierarchical File System
UNIX has a very organized file and directory system that allows users to organize and
maintain files.
Device Independence
UNIX treats input/output devices like ordinary files. The source or destination for file input
and output is easily controlled through a UNIX design feature called redirection.
Utilities
UNIX provides a rich library of utilities that can be use to increase user productivity.
Shell
Unix provides a special interpreter program which can be used to execute commands of the
operating system. It can be used to do various types of operations, call application programs etc.
Open Source
Unix source code is freely available and it is community based development project. Multiple
teams works in collaboration to enhance the capability of Linux operating system and it is
continuously evolving.
Security
Unix provides user security using authentication features like password protection/ controlled
access to specific files/ encryption of data
P. R. S. NAIDU-DEPARTMENT OF CSEPage 2
UNIT-1
UNIX ADVANTAGES:
1. Low cost: You dont need to spend time and money to obtain licenses since Linux and much
of its software come with the GNU General Public License. You can start to work immediately
without worrying that your software may stop working anytime because the free trial version
expires. Additionally, there are large repositories from which you can freely download high
quality software for almost any task you can think of.
2. Stability: Unix doesnt need to be rebooted periodically to maintain performance levels. It
doesnt freeze up or slow down over time due to memory leaks and such. Continuous up-times of
hundreds of days (up to a year or more) are not uncommon.
3. Performance: Unix provides persistent high performance on workstations and on networks. It
can handle unusually large numbers of users simultaneously, and can make old computers
sufficiently responsive to be useful again.
4. Network friendliness: Unix was developed by a group of programmers over the Internet and
has therefore strong support for network functionality; client and server systems can be easily set
up on any computer running Unix. It can perform tasks such as network backups faster and more
reliably than alternative systems.
5. Flexibility: Unix can be used for high performance server applications, desktop applications,
and embedded systems. You can save disk space by only installing the components needed for a
particular use. You can restrict the use of specific computers by installing for example only
selected office applications instead of the whole suite.
6. Compatibility: It runs all common Unix software packages and can process all common file
formats.
7. Choice: The large number of Unix distributions gives you a choice. Each distribution is
developed and supported by a different organization. You can pick the one you like best; the core
functionalities are the same; most software runs on most distributions.
8. Fast and easy installation: Most Unix distributions come with user-friendly installation and
setup programs. Popular Linux distributions come with tools that make installation of additional
software very user friendly as well.
9. Full use of hard disk: Unix continues work well even when the hard disk is almost full.
10. Multitasking: Unix is designed to do many things at the same time; e.g., a large printing job
in the background wont slow down your other work.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 3
UNIT-1
11. Security: Unix is one of the most secure operating systems. Walls and flexible file access
permission systems prevent access by unwanted visitors or viruses. Unix users have to option to
select and safely download software, free of charge, from online repositories containing
thousands of high quality packages. No purchase transactions requiring credit card numbers or
other sensitive personal information are necessary.
12. Open Source: If you develop software that requires knowledge or modification of the
operating system code, Unixs source code is at your fingertips. Most Unix applications are Open
Source as well.
UNIX ARCHITECTURE:
The main concept that unites all versions of UNIX is the following four basics:
Kernel: The kernel is the heart of the operating system. It interacts with hardware and most
of the tasks like memory management, task scheduling and file management.
Shell: The shell is the utility that processes your requests. When you type in a command at
your terminal, the shell interprets the command and calls the program that you want. The
P. R. S. NAIDU-DEPARTMENT OF CSEPage 4
UNIT-1
shell uses standard syntax for all commands. C Shell, Bourne Shell and Korn Shell are most
famous shells which are available with most of the Unix variants.
Commands and Utilities: There are various command and utilities which you would use in
your day to day activities. cp, mv, cat and grep etc. are few examples of commands and
utilities. There are over 250 standard commands plus numerous others provided through 3rd
party software. All the commands come along with various optional options.
Hardware layer - Hardware consists of all peripheral devices (RAM/ HDD/ CPU etc).
Files and Directories: All data in UNIX is organized into files. All files are organized into
directories. These directories are organized into a tree-like structure called the file system.
Accessing UNIX:
When you first connect to a UNIX system, you usually see a prompt such as the following
To log in:
1. Have your userid (user identification) and password ready. Contact your system administrator
if you don't have these yet.
2. Type your userid at the login prompt, then press ENTER. Your userid is case-sensitive, so be
sure you type it exactly as your system administrator instructed.
3. Type your password at the password prompt, then press ENTER. Your password is also case
sensitive.
4. If you provided correct userid and password then you would be allowed to enter into the
system.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 5
UNIT-1
UNIX COMMANDS:
path:
WHAT IS A PATH?
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 alphanumeric characters.
WHAT IS AN ABSOLUTE PATH?
An absolute path is defined as the specifying the location of a file or directory from the root
directory (/). In other words we can say absolute path is a complete path from start of actual file
system from / directory.
SOME EXAMPLES OF ABSOLUTE PATH:
/var/ftp/pub
/etc/samba.smb.conf
/boot/grub/grub.conf
If you see all these paths started from / directory which is a root directory for every Linux/Unix
machines.
WHAT IS THE RELATIVE PATH?
Relative path is defined as path related to the present working directory (pwd). Suppose I am
located in /var/log and I want to change directory to /var/log/kernel. I can use relative path
concept to change directory to kernel changing directory to /var/log/kernel by using relative path
concept.
pwd
/var/log
cd kernel
Note: If you observe there is no / before kernel which indicates its a relative directory to
present working directory.
Changing directory to /var/log/kernel using absolute path concept.
cd /var/log/kernel
Note: We can use an absolute path from any location where as if you want to use relative
path we should be present in a directory where we are going to specify relative to that
present working directory.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 6
UNIT-1
P. R. S. NAIDU-DEPARTMENT OF CSEPage 7
UNIT-1
Example 2: My present location is /etc/apt and now I want to change directory to /etc.
Using relative path:
cd ..
Using absolute path:
cd /etc
P. R. S. NAIDU-DEPARTMENT OF CSEPage 8
UNIT-1
man:
It is the interface used to view the system's reference manuals. If you need to know about any
particular command, you can find the manual of that particular command through man.
Some of the options of man with commands are dipected in the below screenshot.
Example-1:
In the example-1, it clearly depicts the complete manual of ls command.
Example-2:
To view all the man pages for a particular topic, use the a option. Youll see the lowest number
man page first. When you exit that page, and press Enter the next man page will appear.
$ man a printf
The above command will display the man page of printf(1) command first. When you press q
and press Enter, it will display the man page of printf(3) library function.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 9
UNIT-1
Example-3:
-k option searches the short manual page descriptions for keywords and display any matches.
Example-4
Display a short description from the manual page.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 10
UNIT-1
passwd:
passwd [options] [LOGIN]
The passwd command changes passwords for user accounts. A normal user may only change the
password for his/her own account, while the super user may change the password for any
account. passwd also changes the account or associated password validity period.
Example-1:
Example-2:
P. R. S. NAIDU-DEPARTMENT OF CSEPage 11
UNIT-1
uname:
uname command is used to display the software and hardware information in current running
Linux system. uname command is default shell command in Linux.
OPTIONS:
-a = Display the all possible information about your system.
-o = Display the operating system name.
-s = Display the current using kernel name.
-v = Display the current using kernel version.
-r = Display the current using kernel release information.
-m = Display the machine information.
-n = Display the host name
-p = Display the current using processor information
-i = Display the hardware platform information
--help = Display the uname help
--version = Display the uname version.
Syntax:
uname [option]
who:
who command is a tool print information about users who are currently logged in. who command
only see a real user who logged in. It wont see a user that doing su command (switch user).
P. R. S. NAIDU-DEPARTMENT OF CSEPage 12
UNIT-1
whoami:
Print the user name associated with the current effective user ID.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 13
UNIT-1
date:
Date command is helpful to display date in several formats. It also allows you to set systems date
and time.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 14
UNIT-1
P. R. S. NAIDU-DEPARTMENT OF CSEPage 15
UNIT-1
pwd:
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 and is available on most of the shell bash, Bourne shell, ksh,zsh, etc.
Basic syntax of pwd:
# pwd [OPTION]
Options Description
-L (logical) Use PWD from
environment, even
if it contains
symbolic links
-P Avoid all symbolic
(physical) Links
help Display this help
and exit
version Output version
Information and exit
2. To show the full path when symlinks are there(pwd OR pwd -L):
P. R. S. NAIDU-DEPARTMENT OF CSEPage 16
UNIT-1
cd:
cd stands for change directory is a Linux command line utility for navigating to file system.
Absolute Path is the full path of directory and always starts with / in Linux system. For
example: /tmp, /usr/bin, /var/www etc.
Relative Path is the partial path of any directory or subdirectory. It can be started from any
location of file path except /. For example: www, ./script.sh, ../../www etc.
Symbols meaning when used with the cd command:
Single dot (.) : current directory (present directory).
Double dot (..) : parrent directory.
Tiled (~) sign : home directory of current users.
$HOME variable: home directory of current users.
Examples:
1. Change from current directory to another directory
P. R. S. NAIDU-DEPARTMENT OF CSEPage 17
UNIT-1
4.
a. Move one directory back from where you are now.
5. Show last working directory from where we moved (use switch) as shown.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 18
UNIT-1
mkdir:
mkdir - make directories
Syntax:
mkdir [OPTION]... DIRECTORY...
DESCRIPTION
Create the DIRECTORY(ies), if they do not already exist.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 19
UNIT-1
Examples:
1. Creates a new directory called mydir whose parent is the current directory.
2. Create the mydir1 directory, and set its permissions such that all users may read,
write, and execute the contents.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 20
UNIT-1
rmdir:
Examples:
1. How to Delete Empty Directories in Unix?
rmdir command will delete the empty directories. i.e directory without any sub-
directories or files.
3. Delete Directory Which has Content (i.e Directory with Files and Sub-directories)
Some times you may want to delete directory which has contents in it. You can do it with
rm command as shown below.
This will delete the directory including all the files and sub-directories. This is very
dangerous when you use it accidentally as you cannot recover those files easily. So it is
P. R. S. NAIDU-DEPARTMENT OF CSEPage 21
UNIT-1
strongly recommended that you pay attention and think twice before executing the rm -rf
command.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 22
UNIT-1
All the following rm options are valid and does the same functionality. i.e recursive and forceful
delete.
rm -fr
rm -rf
rm -r -f
rm -f -r
ls:
ls command is used to list directory contents.
1. List Files using ls with no option
ls with no option list files and directories in bare format where we wont be able to view
details like file types, size, modified date and time, permission and links etc.
Here, ls -l (-l is character not one) shows file or directory, size, modified date and time,
file or folder name and owner of file and its permission.
3. View Hidden Files
P. R. S. NAIDU-DEPARTMENT OF CSEPage 23
UNIT-1
Using -F option with ls command, will add the / Character at the end each directory.
6. List Files in Reverse Order
The following command with ls -r option display files and directories in reverse order.
7. Recursively list Sub-Directories
ls -R option will list very long listing directory trees. See an example of output of the
command.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 24
UNIT-1
With combination of -ltr will shows latest modification file or directory date as last.
9. Sort Files by File Size
With combination of -lS displays file size in order, will display big in size first.
10. Display Inode number of File or Directory
We can see some number printed before file / directory name. With -i options list file /
directory with inode number.
11. Shows version of ls command
P. R. S. NAIDU-DEPARTMENT OF CSEPage 25
UNIT-1
With ls -l command list files under directory /tmp. Wherein with -ld parameters displays
information of /tmp directory.
14. Display UID and GID of Files
To display UID and GID of files and directories. use option -n with ls command.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 26
UNIT-1
We have made alias for ls command, when we execute ls command itll take -l option by
default and display long listing as mentioned earlier.
Note: We can see number of alias available in your system with below alias
command and same can be unalias as shown below example.
To remove an alias previously defined, just use the unalias command.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 27
UNIT-1
rm:
rm removes each specified file. By default, it does not remove directories.
Syntax:
rm [OPTION] FILE
1. Remove the file myfile.txt. If the file is write-protected, you will be prompted to
confirm that you really want to delete it:
2. Remove the file myfile.txt. You will not be prompted, even if the file is write-
protected; if rm can delete the file, it will:
4. Remove all files in the working directory. rm will not prompt you for any reason
before deleting them:
P. R. S. NAIDU-DEPARTMENT OF CSEPage 28
UNIT-1
5. Attempt to remove every file in the working directory, but prompt before each file
to confirm:
6. Remove every file in the working directory; prompt for confirmation if more than
three files are being deleted:
7. Remove the directory mydirectory, and any files and directories it contains. If a file
or directory that rm tries to delete is write-protected, you will be prompted to make
sure that you really want to delete it:
P. R. S. NAIDU-DEPARTMENT OF CSEPage 29
UNIT-1
8. Same as the above command, but you will never be prompted; if rm can delete the
files, it will:
wc:
The wc (word count) command in Unix/Linux operating systems is used to find out number of
newline count, word count, byte and characters count in a files specified by the file arguments.
The syntax of wc command as shown below.
Syntax:
wc [options] filenames
wc -l : Prints the number of lines in a file.
wc -w : prints the number of words in a file.
wc -c : Displays the count of bytes in a file.
wc -m : prints the count of characters from a file.
wc -L : prints only the length of the longest line in a file.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 30
UNIT-1
split:
Split a file into pieces.
Syntax:
split [-linecount | -l linecount ] [ -a suffixlength ] [file [name] ]
-linecount | -l
linecount Number of lines in each piece. Defaults to 1000 lines.
-a suffixlength Use suffixlength letters to form the suffix portion of the filenames of the
split file. If -a is not specified, the default suffix length is 2. If the sum of
the name operand and the suffixlength option-argument would create a
filename exceeding NAME_MAX bytes, an error will result; split will exit
with a diagnostic message and no files will be created.
-b n Split a file into pieces n bytes in size.
-b n k Split a file into pieces n*1024 bytes in size.
-b n m Split a file into pieces n*1048576 bytes in size.
File The path name of the ordinary file to be split. If no input file is given or
file is -, the standard input will be used.
name The prefix to be used for each of the files resulting from the split
operation. If no name argument is given, x will be used as the prefix of the
output files. The combined length of the basename of prefix and
suffixlength cannot exceed NAME_MAX bytes; see OPTIONS.
Example-1:
P. R. S. NAIDU-DEPARTMENT OF CSEPage 31
UNIT-1
Example-2:
lp:
The lp command is used to print files on Unix and Linux systems. The name "lp" stands
for "line printer".
lp /etc/passwd
This command prints the "/etc/passwd" file to the default printer.
lp -dSales .profile
This command prints the ".profile" file to the printer named "Sales". The d option specifies the
destination.
lp -dSales file1 file2 file3
This command prints the three files "file1", "file2", and "file3" to the printer named "Sales".
lp -i Sales-101 -H hold
This command places the print request Sales101 on hold.
lp -i Sales-101 -H resume
This command resumes the print request Sales101.
ps -ef | lp
Print the output of the "ps -ef" command to the default printer.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 32
UNIT-1
cat:
cat command allows us to create single or multiple files, view contain of file, concatenate files
and redirect output in terminal or files.
Syntax:
cat [OPTION] [FILE]...
Examples:
1. Display Contents of File
P. R. S. NAIDU-DEPARTMENT OF CSEPage 33
UNIT-1
P. R. S. NAIDU-DEPARTMENT OF CSEPage 34
UNIT-1
P. R. S. NAIDU-DEPARTMENT OF CSEPage 35
UNIT-1
od:
od dump files in octal and other formats
DESCRIPTION
octal dump displays contents as octal numbers. This can be useful when the output contains non-
printable characters. For example, a filename may contain non-printable characters. It can also be
handy to view binary files.
The name is an acronym for octal dump since it defaults to printing in the octal data format. It
can also displaying output in a variety of other formats, including hexadecimal, decimal, and
ASCII. It is useful for visualizing data that isnt in a human-readable format, like the executable
code of a program.
OPTIONS:
-A, address-radix=radix
Select the base in which file offsets are printed.
radix can be one of the following:
P. R. S. NAIDU-DEPARTMENT OF CSEPage 36
UNIT-1
d decimal
o octal
x hexadecimal
n none (do not print offsets)
The default is octal.
-j, skip-bytes=bytes
Skip bytes input bytes before formatting and writing. If bytes begins with 0x or 0X, it is
interpreted in hexadecimal; otherwise, if it begins with 0, in octal; otherwise, in decimal.
Appending b multiplies it by 512, k by 1024, and m by 1048576.
-N, read-bytes=bytes
Only output up to bytes bytes of each input file. Any prefixes and suffixes on bytes are
interpreted as for the -j option.
-t, format=type
Select the format in which to output the file data. type is a string of one or more of the below
type indicator characters. If you include more than one type indicator character in a single type
string or use this option more than once, od writes one copy of each output line using each of the
data types that you specified, in the order that you specified.
a named character
c ASCII character or backslash escape
d signed decimal
f floating point
o octal
u unsigned decimal
x hexadecimal
C char
S short
I int
L long
P. R. S. NAIDU-DEPARTMENT OF CSEPage 37
UNIT-1
-v, output-duplicates
Output consecutive lines that are identical. By default, when two or more consecutive output
lines would be equal, od outputs only the first line, and puts just an asterisk on the following line
to indicate that identical lines have been elided.
-s, strings[=bytes]
Instead of the normal output, output only string constants in the input, which are a run of at least
bytes ASCII graphic (or formatting) characters, terminated by a NUL. If bytes is omitted, it
defaults to 3.
-w,width[=bytes]
The number of input bytes to format per output line. It must be a multiple of the least common
multiple of the sizes associated with the specified output types. If bytes is omitted, it defaults to
32. If this option is not given, it defaults to
16.
-a Output as named characters. Equivalent to -t a.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 38
UNIT-1
Examples:
The first column in the output of od represents the byte offset in file.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 39
UNIT-1
P. R. S. NAIDU-DEPARTMENT OF CSEPage 40
UNIT-1
P. R. S. NAIDU-DEPARTMENT OF CSEPage 41
UNIT-1
P. R. S. NAIDU-DEPARTMENT OF CSEPage 42
UNIT-1
tar:
The Linux tar stands for tape archive, which is used by large number of Linux/Unix system
administrators to deal with tape drives backup. The tar command used to rip a collection of les
and directories into highly compressed archive file commonly called tarball or tar, gzip and
bzip in Linux. The tar is most widely used command to create compressed archive files and that
can be moved easily from one disk to anther disk or machine to machine.
tar command examples including how to create archive files using (tar, tar.gz and tar.bz2)
compression, how to extract archive file, extract a single file, view content of file, verify a file,
add files or directories to archive file, estimate the size of tar archive file, etc.
c - Creates a new .tar archive file.
v - Verbosely show the .tar file progress.
f - File name type of the archive file.
x - Extracts from archive file (.tar/.tar.gz)
z - create compressed gzip file
C - untar in different directory
j - Extracts from archive file (/.tar.tb2)
Examples:
1. Create tar Archive File
P. R. S. NAIDU-DEPARTMENT OF CSEPage 43
UNIT-1
P. R. S. NAIDU-DEPARTMENT OF CSEPage 44
UNIT-1
P. R. S. NAIDU-DEPARTMENT OF CSEPage 45
UNIT-1
P. R. S. NAIDU-DEPARTMENT OF CSEPage 46
UNIT-1
15. Check the Size of the tar, tar.gz and tar.bz2 Archive File
gzip:
gzip is a command used to compress or decompress the files or a folder with the extension
.gz.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 47
UNIT-1
stty:
stty command is used to manipulate the terminal settings.
1. Display All Settings
-a option displays all the stty settings in a user friendly readable format as shown below.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 48
UNIT-1
3. Specify Device
You can specify a device file as an argument to stty command. In that case, it will use the
device that youve specified instead of using the standard input.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 49
UNIT-1
cp:
1. Copy a file or directory from source to destination (cp source_path
destination_path):
Here as you can see that file 1.txt is copied from with the same name in folder
sample_folder.
If you want to copy the file in another file name, you can mention the name of the file in
the destination path.
2. A directory (and all its content) can be copied from source to destination with the
recursive option -r(cp -r source destination):
P. R. S. NAIDU-DEPARTMENT OF CSEPage 50
UNIT-1
From the above example, you can see that all the folder named sample* are copied in a
folder final in a single step.
4. To avoid overwriting the the existing file(cp -n source destination):
If you want to copy only when the destination file doesnt exist, use option -n.This
wont overwrite the existing file.
Here as we can see that after copying file 1.txt in sample folder the original file 1.txt in
sample folder is not changed since of -n option.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 51
UNIT-1
6. To make a backup of files if copying file has the same name(cp backup source
destination ):
As you can see here that when 1.txt is copied from source to destination in sample folder, a
backup of the original file 1.txt in the sample folder is made as 1.txt~ and new file is copied
in the 1.txt as usual.
7. To show copying details in a verbose manner(cp -v source destination):
You need to pass the -p option to save File modification time, access time, modes, flags
of the files.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 52
UNIT-1
As you can see from the above example, that after adding -d option a link is preserved
while copying.
10. To create hard link to a file(cp -l source destination):
You can create a hard link to a file, instead of copying it.
As you can see, that after adding -loption in cp the innode, 585339 is same for the file i.e a
hard link is created.
Note
To create a soft link while copying Use -s option.
cp -s file1 file2
P. R. S. NAIDU-DEPARTMENT OF CSEPage 53
UNIT-1
mv:
mv command is used to move files and directories from one location to another. Apart from
moving the files, it can also rename a file or directory.
1. Rename a File
While renaming a file using mv command, it keeps the inode number same even after
moving it to a different name. If you move the file to a different filesystem, the inode number
will be different.
2. Rename a Directory
Just like renaming a file, you can rename a directory using mv command as shown below.
This also keeps the inode number of the directory same after renaming.
If you just do ls -l dir, it will display the files in the directory. To display the directory
properties, use -d option. -i option displays the inode number of the directory.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 54
UNIT-1
When the destination file permission is different than the source file, mv -i command will
display the following confirmation.
$ mv -i sample.txt sample1.txt
mv: try to overwrite `sample1.txt', overriding mode 0644 (rw-r--r--)? y
4. Move Multiple Files to a Specific Directory
You can move multiple files using mv command. The following example moves the content
of the current directory to a different directory.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 55
UNIT-1
Note: You might want to create an alias for mv suffix, which will take a backup
automatically anytime you use mv command and if the destination file exist.
6. Move only the files that dont exist in the destination directory
When you do mv *, it will move all the files to the destination directory. However, if you
want to move only the files from the source directory that dont exist in the destination
directory, use the mv -u option as shown below.
The following command will move only the ex2 and ex2 from chap1 to chap2, as ex1 file
already existing in chap2, which will not be moved.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 56
UNIT-1
ln:
Hard Link acts like a mirror copy of the original file. These links share the same inodes.
Changes made to the original or hard linked file will reflect in the other. When you delete Hard
Link nothing will happen to the other file. Hard links can't cross file systems.
What are Hard Links?
1. Hard Links have same inodes number.
2. ls -l command shows all the links with the link column showing the number of links.
3. Links have actual file contents
4. Removing any link, just reduces the link count but doesn't affect the other links.
5. You cannot create a Hard Link for a directory.
6. Even if the original file is removed, the link will still show you the contents of the file.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 57
UNIT-1
Soft Link is an actual link to the original file. These Links will have a different Inodes value.
Soft link points to the original file so if the original file is deleted then the soft link fails. If you
delete the Soft Link, nothing will happen to file. The reason for this is, the actual file or
directorys inode is different from the "soft link" created file's inodes. Soft links can cross file
systems.
What are Soft Links?
1. Soft Links have different inodes numbers.
2. ls -l command shows all links with second column value 1 and the link points to original file.
3. Soft Link contains the path for original file and not the contents.
4. Removing soft link doesn't affect anything but when the original file is removed, the link
becomes a 'dangling' link that points to non-existent file.
5. A Soft Link can link to a directory.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 58
UNIT-1
echo:
echo is one of the most commonly and widely used built-in command for Linux bash and C
shells, that typically used in scripting language and batch files to display a line of text/string on
standard output or a file.
Syntax:
echo [options (s)] [string (s)]
1. Input a line of text and display on standard output
2. Declare a variable and echo its value. For example, Declare a variable of x and assign
its value=10.
3. Using option \b backspace with backslash interpretor -e which removes all the
spaces in between.
Note: The -e option in Linux acts as interpretation of escaped characters that are
backslashed.
4. Using option \n New line with backspace interpretor -e treats new line from where
it is used.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 59
UNIT-1
5. Using option \t horizontal tab with backspace interpretor -e to have horizontal tab
spaces.
6. How about using option new Line \n and horizontal tab \t simultaneously.
7. Using option \v vertical tab with backspace interpretor -e to have vertical tab
spaces.
8. How about using option new Line \n and vertical tab \v simultaneously.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 60
UNIT-1
10. Using option \c suppress trailing new line with backspace interpretor -e to
continue without emitting new line.
12. Print all the files/folder using echo command (ls command alternative).
13. Print files of a specific kind. For example, lets assume you want to print all .jpeg
files, use the following command.
14. The echo can be used with redirect operator to output to a file and not standard
output.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 61
UNIT-1
printf:
printf format and print data
DESCRIPTION
printf prints the FORMAT string, interpreting `% directives and `\ escapes to format numeric
and string arguments in a way that is mostly similar to the C `printf function.
printf FORMAT [ARGUMENT]...
printf OPTION
\ double quote
\0NNN character with octal value NNN (0 to 3 digits)
\\ backslash
\a alert (BEL)
\b backspace
\c produce no further output
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\xNNN byte with hexadecimal value NNN (1 to 3 digits)
\uNNNN character with hexadecimal value NNNN (4 digits)
\UNNNNNNNN character with hexadecimal value NNNNNNNN (8 digits)
%% a single %
%b ARGUMENT as a string with `\ escapes interpreted
and all C format specifications ending with one of diouxXfeEgGcs, with ARGUMENTs
converted to proper type first. Variable widths are handled.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 62
UNIT-1
script:
The Unix script command. script is used to take a copy of everything which is output to the
terminal and place it in a log file. It should be followed by the name of the file to place the log in,
and the exit command should be used to stop logging and close the file.
P. R. S. NAIDU-DEPARTMENT OF CSEPage 63