unit 1 linux
TRANSCRIPT
![Page 1: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/1.jpg)
The fundamental architecture of the GNU/Linux operating system
![Page 2: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/2.jpg)
Short history of major Linux kernel releases
![Page 3: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/3.jpg)
• You've probably noticed that Linux as an operating system is referred to in some cases as "Linux" and in others as "GNU/Linux." The reason behind this is that Linux is the kernel of an operating system. The wide range of applications that make the operating system useful are the GNU software. For example, the windowing system, compiler, variety of shells, development tools, editors, utilities, and other applications exist outside of the kernel, many of which are GNU software. For this reason, many consider "GNU/Linux" a more appropriate name for the operating system, while "Linux" is appropriate when referring to just the kernel.
![Page 4: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/4.jpg)
Linux directory commands
pwd - Display name of current directory• The command >pwd is used to display the full path name
of the current directory.cd - Switch to another directory• To switch to another directory, the command cd is used.Examples What it does
Cd Will place you in your home directorycd / Will move you to the root directorycd etc Will move you to the etc directorycd ../Will move you back one directory
![Page 5: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/5.jpg)
• mkdir and rmdir - Create/Delete directoriesThe command mkdir is used to create a new
directory.The command rmdir or rm -r is used to delete a
directory or directories
Examples What it doesmkdir mydirectory Will create a new
directory named 'mydirectory'rmdir existingdirectory Will delete the existing directory
named 'existingdirectory'rm -r existingdirectories Will delete the existing directory
named 'existingdirectories' and all directories and files below it.
![Page 6: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/6.jpg)
• ls - List the Contents of a directoryThe command ls is used to the contents of a
directoryOptions What it does
-l long listing
-R list current directory and all other directories within current directory
-a list hidden files
-CFlist in column format and append '*' to executable files, '@' to symbolic linked files, '/' to directories
-r list in reverse alphabetically order
-t list more recent accessed files first
![Page 7: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/7.jpg)
Examples What it does
Ls only list file/directory names in current directory
ls –l list all file/directory information in current directory(long version)
ls –R list all files in current directories and below
ls –lt list all files, sorted by most recent accessed first
ls -lt /etc/rc* list files in the '/etc/ directory, only starting with 'rc' and sort results by most recent
![Page 8: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/8.jpg)
• WildcardsWildcard characters are used to help find file or
directory names
Options What it does
* asterisk symbol is used to represent anycharacter(s)
? question mark is used to represent any single character
[from-to ] Values entered within square brackets represent a range(from-to) for a single character
[!from-to ] Values entered within square brackets represent a range(from-to) to exclude for a single character
![Page 9: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/9.jpg)
Examples What it does
a* all files starting with the letter 'a'
*z all files where the last character is a 'z'
a*m all files that start with the letter 'a' and end with 'm'
th?? all files that start with 'th' and are only four characters long
[a-c]* all files that start with 'a, b or c'
x[A-C]* all files that start with the letter 'x' and the second character contains 'A, B or C'
[!M-O]* all files except those that start with 'M, N or O'
![Page 10: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/10.jpg)
File handling utilities
cp - Copy files• To copy a file, the command cp is used• Example:cp oldfile myfile - Will copy the
existing file 'oldfile' to a new file 'myfile'mv - Rename files• The command mv is used to rename a file• Example: mv myfile yourfile - Will rename the
file 'myfile' to 'yourfile'
![Page 11: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/11.jpg)
rm - Delete files
Examples What it does
rm myfile remove the file 'myfile'
rm -i abc* prompt to remove each file in current directory starting with 'abc'
rm abc* remove all files in current directory starting with 'abc' automatically
![Page 12: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/12.jpg)
wc - Count the number of lines or characters• The command wc is used to count lines, words
or characters in a file or piped results from another command.
Options What it does
-c Number of characters
-w Number of words
-l
filename file name(s) to use
Examples What it does
wc /etc/sendmail.cf Lists the number of lines, words and characters in the file 'sendmail.cf'
ls etc wc -l Lists the number of files and directories in the directory 'etc'
![Page 13: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/13.jpg)
file - Display Type-of-File Description• Files can consist of several types. The
command file is used to display a description for the type.
• Example: file a* will list all files in the current directory that start with the letter "a" and provide a description for the file type.
![Page 14: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/14.jpg)
• cat - concatenate files• The command cat is a multi-purpose utility and is mostly used
with TEXT files.• Create a new file and optionally allow the manual entry of
contents– cat >[filename]– Example: cat >myfile will create a file named myfile and allow you to enter
contents.– Press Control-D to exit entry mode.– WARNING: If "myfile" already existed, this command would replace the
old file with the contents of the new file.• Combine text files
– cat file1 file2 >newfile - This will combine file1 and file2 into newfile.• Dissplay the contents of a file
– cat myfile• Delete the contents of a file• cat /dev/null >myfile
![Page 15: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/15.jpg)
Linux file permissions
• The basics of file ownership and permissions on Linux. Learn to understand who are the owners of a file or directory, how the file permissions work and how you can view them, and learn how to set basic file permissions yourself.
Permissions and ownership - why? • you must keep in mind Linux is designed to be a multi-
user environment. • In an environment with more than one users, it is
crucial to have a secure system for deciding which files are yours and who can fiddle with them.
![Page 16: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/16.jpg)
Understanding file ownership
• Every file on your Linux system, including directories, is owned by a specific user and group. Therefore, file permissions are defined separately for users, groups, and others.
• User: the user who creates the file will become its owner.
• Group: The usergroup that owns the file. All users who belong into the group that owns the file will have the same access permissions to the file.
• Other: A user who isn't the owner of the file and doesn't belong in the same group the file does.
![Page 17: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/17.jpg)
Understanding file permissions • There are three types of access permissions on Linux: read, write,
and execute. These permissions are defined separately for the file's owner, group and all other users.
• Read permission. On a regular file, the read permission bit means the file can be opened and read. On a directory, the read permission means you can list the contents of the directory.
• Write permission. On a regular file, this means you can modify the file, and write new data to the file. In the case of a directory, the write permission means you can add, remove, and rename files in the directory.
• Execute permission. In the case of a regular file, this means you can execute the file as a program. On a directory, the execute permission allows you to access files in the directory and enter it
![Page 18: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/18.jpg)
How to view file permissions
ls -l command• This is what a long directory listing might look like:
me@puter: /home/writers$ ls -ldrwxr-xr-x 3 nana writers 80 2005-09-20 21:37 dir-rw-r----- 1 nana writers 8187 2005-09-19 13:35 file-rwxr-xr-x 1 nana writers 10348 2005-07-17 20:31 otherfile
The first character can be any of these:d = directory- = regular file
![Page 19: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/19.jpg)
• The characters are pretty easy to remember.• r = read permission
w = write permissionx = execute permission- = no permission
![Page 20: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/20.jpg)
How to set file permissions - symbolic mode
• You can set file permissions with the chmod command. Both the root user and the file's owner can set file permissions. chmod has two modes, symbolic and numeric.
![Page 21: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/21.jpg)
Which user?
u user/owner
g group
o other
a All
What to do?
+ add this permission
- remove this permission
= set exactly this permission
Which permissions?
r read
w write
x execute
![Page 22: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/22.jpg)
• Wipe out all the permissions but add read permission for everybody:$ chmod a=r testfileAfter the command, the file's permissions would be -r--r--r--
• Add execute permissions for group:$ chmod g+x testfileNow, the file's permissions would be -r--r-xr--
• Add both write and execute permissions for the file's owner. Note how you can set more than one permission at the same time:$ chmod u+wx testfileAfter this, the file permissions will be -rwxr-xr--
• Remove the execute permission from both the file's owner and group. Note, again, how you can set them both at once:$ chmod ug-x testfileNow, the permissions are -rw-r--r--
![Page 23: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/23.jpg)
How to set file permissions - numeric mode
• The other mode in which chmod can be used is the numeric mode. In the numeric mode, the file permissions aren't represented by characters. Instead, they are represented by a three-digit octal number.
• 4 = read (r)2 = write (w)1 = execute (x)0 = no permission (-)
$ chmod 755 testfile$ chmod 640 testfile
![Page 24: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/24.jpg)
Process utilities
• The ps CommandThe ps (i.e., process status) command is used to provide information about
the currently running processes, including their process identification numbers (PIDs).
The basic syntax of ps is• ps [options]$ ps PID TTY TIME CMD 351 pts/1 00:00:00 bash 3523514 pts/1 00:00:00 ps
![Page 25: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/25.jpg)
$ ps aux | lessUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDheyne 691 0.0 2.4 19272 9576 pts/0 S 13:30 0:00 kdeinit: kded
$ps –e | less-e displays all the system processes
Use “spacebar” to move screen forwardHit ‘b’ screen back‘q’ to quit$ps –eo pid,%cpu,%mem,%stat(out put customization)
![Page 26: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/26.jpg)
• Who: know the usersDisplays the users currently logged in the
system.luzar@ubuntu:~$ who root tty1 2009-07-06 22:06 luzar tty7 2009-07-06 19:17 (:0) luzar pts/0 2009-07-06 21:28 (:0.0)
![Page 27: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/27.jpg)
• Whoami: Show you the owner of this account• luzar@ubuntu:~$ who am i Output:• luzar pts/0 2009-07-06 22:39 (:0.0)
![Page 28: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/28.jpg)
• W: Tell you who is logging in and doing what!kucing@ubuntu-laptop:~$ w 09:15:10 up 43 min, 2 users, load average: 0.74, 0.38, 0.24 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT kucing tty7 :0 08:32 43:15m 57.73s 0.18s x-session-
managerkucing pts/0 :0.0 08:48 0.00s 0.24s 0.00s wOptions:
-h skip header -l long listing (default) -s short listing
![Page 29: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/29.jpg)
Networking commands
FTP:file transfer protocol • ftp abc.xyz.edu• ftp> ls• ftp> cd customers• ftp> get image1.jpg• ftp> put image2.jpg• ftp> mget *.jpg• ftp> mput *.jpg• ftp> mdelete *.jpg• ftp> quit
![Page 30: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/30.jpg)
telnet :Communicate with another host• To login into a another computer, the
command telnet is used. You need at least 3 pieces of information to perform a telnet session:
• remote host IP address• login name• passwordExamples:
telnet ahinc.comtelnet 192.168.1.1
![Page 31: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/31.jpg)
hostname:To display the host name, domain name or IP address of your
machine$hostname –i(ip address)
ping - Test network connectionTo test your network connection with another network device,
the command ping is used$ping 192.168.1.1Output:• PING 192.168.1.1 (192.168.1.1): 56 data bytes• 64 bytes from 192.168.1.1: icmp_seq=0 ttl=64 time=0.2 ms• 64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.1 ms• 64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.1 ms
![Page 32: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/32.jpg)
ifconfig - View network configuration• ifconfig Displays...• lo Link encap:Local Loopback• inet addr:127.0.0.1 Bcast:127.255.255.255 Mask:255.0.0.0• UP BROADCAST LOOPBACK RUNNING MTU:3584 Metric:1• RX packets:292320 errors:0 dropped:0 overruns:0• TX packets:292320 errors:0 dropped:0 overruns:0• • eth0 Link encap:10Mbps Ethernet HWaddr 00:60:94:57:D3:55• inet addr:192.168.1.1 Bcast:192.168.1.255 Mask:255.255.255.0• UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1• RX packets:7820768 errors:3465 dropped:0 overruns:0• TX packets:8643039 errors:0 dropped:0 overruns:0• Interrupt:14 Base address:0x5180
![Page 33: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/33.jpg)
Disable an Interface• $ifconfig eth0 down
Enable an Interface• $ ifconfig eth0 up
Assign ip-address to an Interface• Assign 192.168.2.2 as the IP address for the interface eth0.• $ ifconfig eth0 192.168.2.2Change Subnet mask of the interface eth0.• $ ifconfig eth0 netmask 255.255.255.0Change Broadcast address of the interface eth0.• $ ifconfig eth0 broadcast 192.168.2.255Assign ip-address, netmask and broadcast at the same time to
interface eht0.• $ ifconfig eth0 192.168.2.2 netmask 255.255.255.0 broadcast
192.168.2.255
![Page 34: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/34.jpg)
Disk utilities
• When you ever need to know how much space a file or directory is using on your disk, you should use du .
du:Stands for disk usage,and displays how much
disk space is used by file or directory.Syntax:
$du [options] [file/dir]
![Page 35: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/35.jpg)
Options:-a all files not just for directories-b it prints the size in bytes-c total disk space used by directory-h size in human redable format(k,m,g..)-s does not show the size of sub
directories
![Page 36: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/36.jpg)
examples
$du –a venkatan4 venkatan/ravi4 venkatan/abhinav/kalyan8 venkatan/abhinav9 venkatan/foo4 venkatan/file.text24 venkatan
![Page 37: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/37.jpg)
$du –c venkatan24 Venkatan 24 total
$du –h venkatan8.0k venkatan/abhinav24k venkatan
![Page 38: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/38.jpg)
df:Stands for disk free.used to find out empty disk space
$dfFilesystem 1k-blocks used available use% mounted on/dev/sda9 88244004 2382548 81378924 3% //dev/sdb1 964336 964304 32 100%
/media/phonecard
$df –hFilesystem size used available use% mounted on/dev/sda9 85G 2.3G 78G 3% //dev/sdb1 99G 16G 84G 16% /media/disk
![Page 39: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/39.jpg)
Mount:Mounting is done using mount command.$mount /dev/fd0 /mnt/floppy
floppy device mount point$mount /dev/cdrom /mnt/cdromThe default mount points for different devices
are configured in a file called /etc/fstab$umount /mnt/floppy(or)$umount /dev/fd0
![Page 40: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/40.jpg)
filters
A filter is a program that reads a single input stream, transforms it in some way, and writes the result to a single output stream
![Page 41: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/41.jpg)
Filter What it does
cat Copies input direct to output.
head Shows beginning of a file (default 10 lines).
tail Shows end of a file (default 10 lines).
wc Counts characters, words and lines.
sort Sorts input lines.
more Displays contents of a file on screen.
cmp Comparision between two files byte by byte.
diff Difference between two files.
tr Translates or deletes specified character sets.
sed Stream editor.
uniq Discards all but one of successive identical lines.
awk Highly-programmable field-processing.
![Page 42: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/42.jpg)
Tail command
• Tail prints the last N number of lines from given input. By default, it prints last 10 lines of each given file.
• $ tail -5 flavours.txt Debian Redhat Gentoo Fedora core
![Page 43: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/43.jpg)
Head command
• Head prints the first N number of data of the given input. By default, it prints first 10 lines of each given file.
$ head -5 flavours.txt Ubuntu Debian Redhat Gentoo Fedora core
![Page 44: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/44.jpg)
• $ head -5 file1 file2====> file1<====first 5 lines from file1====> file2<====first 5 lines from file1
![Page 45: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/45.jpg)
sort command• The sort Command in linux is one of the most ignored(?) yet
powerful command.Sort is used to sort data or records.if you use with other utils like ls it can give you wide varitety of
data.Syntax:$sort [-options] fieldidentifier filename
let's say the ls -l output of a directory is
-rw-r--r-- 1 nandam nandam 785280 2008-05-27 22:26 gdocs-1.0.4.oxt-rw-r--r-- 1 nandam nandam 4997184 2008-05-27 22:16 Sun_ODF_Template_Pack_en-US.oxt-rwxr-xr-x 1 nandam nandam 360756 2008-01-18 19:30 WriterTemplates.oxt-rwxr-xr-x 1 nandam nandam 48019 2008-01-18 19:30 WriterTools.oxt
![Page 46: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/46.jpg)
$ls -l|sort -k 5 -k ---> instructs sort to sort records by column 5 in ascending order.
and now you will get the correct format as below
-rwxr-xr-x 1 nandam nandam 48019 2008-01-18 19:30 WriterTools.oxt-rwxr-xr-x 1 nandam nandam 360756 2008-01-18 19:30 WriterTemplates.oxt-rw-r--r-- 1 nandam nandam 785280 2008-05-27 22:26 gdocs-1.0.4.oxt-rw-r--r-- 1 nandam nandam 4997184 2008-05-27 22:16 Sun_ODF_Template_Pack_en-US.oxt
![Page 47: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/47.jpg)
• $sort -k 6,7 files.txt
sort the data first by column 6 and then by column 7
![Page 48: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/48.jpg)
More
• The more command in Linux is helpful when dealing with a small xterm window, or if you want to easily read a file without using an editor to do so. More is a filter for paging through text one screenful at a time.
1. To View a File Using moreerik@debian:~$ more fur.sh This will auto clear the screen and display the start
of the file.
![Page 49: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/49.jpg)
• 2. Pipe Output From ‘cat’ Into moreerik@debian:~$ cat data.txt | more
The main difference between more and less is that less allows backward and forward movement using the arrow keys, while more only uses the [Spacebar] and the [B] key for forward and backward navigation
![Page 50: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/50.jpg)
• cmp COMMAND: cmp linux command compares two files and tells you which line numbers are different.
SYNTAX: cmp [options..] file1 file2
![Page 51: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/51.jpg)
Compare two files:$cmp file1 file2The above cmp command compares file1.php with file2.php
and results as follows.file1.php file2.php differ: byte 35, line 3 Compare two files output differing bytes as characters:$cmp -c file1.php file2.phpThe above cmp command compares file1.php with file2.php
and results as follows.file1.php file2.php differ: byte 35, line 3 is 151 i 15
![Page 52: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/52.jpg)
diff
• This command prints the differences between two files by performing the comparison line-by-line and by comparing first file with second file.
$diff file1 file2
![Page 53: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/53.jpg)
File "one" Boo!Hey!Hiwelcome File "two" Boo!Hey!Byegoodmorning
![Page 54: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/54.jpg)
alex@tux ~/test $ diff one two3c3< Hi--->Bye4c4<welcome--->goodmorning
![Page 55: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/55.jpg)
uniq
• The uniq command reads the input file and compares adjacent lines. Any line that is the same as the one before it will be discarded.
my.books file shown here:Atopic Dermatitis for DummiesAtopic Dermatitis for DummiesChronic Rhinitis UnleashedChronic Rhinitis UnleashedChronic Rhinitis UnleashedLearn Nasal Endoscopy in 21 Days
![Page 56: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/56.jpg)
$uniq my.booksAtopic Dermatitis for DummiesChronic Rhinitis UnleashedLearn Nasal Endoscopy in 21 Days
![Page 57: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/57.jpg)
tr
• This command is used for translating one character set to another.
$tr [options] string1 string2$Tr “soatbge” “SOATBGE”small opportunities are often the beginning of
great enterprises.Ctrl+d
![Page 58: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/58.jpg)
tutorial@fuzzy:~$echo my foot | tr o xMy fxxttutorial@fuzzy:~$ echo my foot | tr mo TxTy fxxttutorial@fuzzy:~$ echo leet | tr let 1371337tutorial@fuzzy:~$ echo foo-foo | tr - _Foo_footutorial@fuzzy:~$ echo footbar | tr [:lower:] [:upper:]
FOOTBAR
![Page 59: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/59.jpg)
cut
![Page 60: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/60.jpg)
![Page 61: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/61.jpg)
paste
![Page 62: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/62.jpg)
![Page 63: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/63.jpg)
![Page 64: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/64.jpg)
![Page 65: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/65.jpg)
Backup utilities
cpio: stands for “copy in, copy out”.• cpio performs the following three operations.1. Copying files to an archive2. Extracting files from an archive3. Passing files to another directory tree• cpio takes the list of files from the standard
input while creating an archive, and sends the output to the standard output.
![Page 66: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/66.jpg)
1. Create *.cpio Archive File• You can create a *.cpio archive that contains
files and directories using cpio -ov• $ cd objects • $ ls
file1.o file2.o file3.o • $ ls | cpio -ov > /tmp/object.cpio
![Page 67: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/67.jpg)
2. Extract *.cpio Archive File• cpio extract: To extract a given *.cpio file, use
cpio -iv as shown below.$ mkdir output $ cd output $ cpio -idv < /tmp/object.cpio
![Page 68: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/68.jpg)
tar
c Create a new archive.t List the contents of an archive.x Extract the contents of an archive.f The archive file name is given on the command line (required whenever the tar output is going to a file)v Print verbose output (list file names as they are processed).
![Page 69: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/69.jpg)
$tar cvf alldocs.tar *.doc$tar cvf panda.tar panda/$tar cvf /dev/fd0 panda Archive the files in the panda directory to floppy
disk(s).$tar cvf /dev/rmt0 panda Archive the files in the panda directory to the
tape drive.
![Page 70: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/70.jpg)
• To list the contents of a tar file, use the t (type) flag in a command, like this:
$tar tvf alldocs.tar List all files in alldocs.tar.• To extract the contents of a tar file, use
the x (extract) flag in a command, like this:$tar xvf panda.tar Extract files from panda.tar.
![Page 71: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/71.jpg)
SED
• Sed stands for stream editor.• It reads the standard input ,processes it using
a separate file called sed script and writes the result to the standard output.
• Sed script is file contains list of instructions to be applied to each line in the input file.
![Page 72: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/72.jpg)
The sed command
![Page 73: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/73.jpg)
sed command syntax
![Page 74: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/74.jpg)
Sed script
• It contains set of instructions to be applied to each line in the input file.
• If there is only one instruction it can be included at the command line.
• If there are more instructions ,they should be saved in a separate file with extension sed called sed script or sed program.
![Page 75: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/75.jpg)
sed instruction format
• address determines which lines in the input file are to be processed by the command(s)– if no address is specified, then the command is
applied to each input line• address types:
– Single-Line address– Set-of-Lines address– Range address– Nested address
![Page 76: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/76.jpg)
Example sed script
Demo.sed
# comment one# comment two3d1,10s/friends/buddies
![Page 77: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/77.jpg)
sed Operation
$sed –f myscript.sed frnd.txtMyscript.sed
1,3s/greeting/hello/$s/always/forever/
Frnd.txtgreeting friendMy friendBest friendalways friend
![Page 78: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/78.jpg)
How Does sed Work?
• sed reads line of input– line of input is copied into a temporary buffer
called pattern space– editing commands are applied
• subsequent commands are applied to line in the pattern space• once finished, line is sent to output
(unless –n option was used)– line is removed from pattern space
• sed reads next line of input, until end of file
Note: input file is unchanged
![Page 79: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/79.jpg)
Single-Line Address• Specifies only one line in the input file
– special: dollar sign ($) denotes last line of input fileExamples: 6command
applies command to line number 6 only $command
applies command to last line– show only line 3sed -n -e '3 p' input-file
– show only last linesed -n -e '$ p' input-file
– substitute “endif” with “fi” on line 10sed -e '10 s/endif/fi/' input-file
![Page 80: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/80.jpg)
Set-of-Lines Address• use regular expression to match lines
– written between two slashes– process only lines that match– may match several lines – lines may or may not be consecutives
Examples: • /^A/command matches all lines that starts with ‘A’
• /^$/command applies command to last line
$sed -e ‘/key/ s/more/other/’ input-file$sed -n -e ‘/r..t/ p’ input-file
80CSCI 330 - The Unix System
![Page 81: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/81.jpg)
Range Address
• Defines a set of consecutive linesFormat:
start-addr,end-addr (inclusive)
Examples:10,50 line-number,line-number10,/R.E/ line-number,/RegExp//R.E./,10 /RegExp/,line-number/R.E./,/R.E/ /RegExp/,/RegExp/
81CSCI 330 - The Unix System
![Page 82: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/82.jpg)
Example: Range Address% sed -n -e ‘/^BEGIN$/,/^END$/p’ input-file
• Print lines between BEGIN and END, inclusive
BEGINLine 1 of inputLine 2 of inputLine3 of inputENDLine 4 of inputLine 5 of input
82
addr1 addr2
These lines are printed
![Page 83: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/83.jpg)
Nested Address
• Nested address contained within another address
Example: delete all blank lines between line 20 and 30
20,30{/^$/d
}
![Page 84: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/84.jpg)
Address with !
• address with an exclamation point (!):instruction will be applied to all lines that do not match the address
Example: print lines that do not contain “obsolete”
$sed -e ‘/obsolete/!p’ input-file
![Page 85: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/85.jpg)
sed commands
85CSCI 330 - The Unix System
![Page 86: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/86.jpg)
Line Number
• line number command (=) writes the current line number before each matched/output line
Examples:
$sed -e '/Two-thirds-time/=' tuition.data
$sed -e ‘=' inventory86CSCI 330 - The Unix System
![Page 87: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/87.jpg)
modify commands
87CSCI 330 - The Unix System
Substitute
![Page 88: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/88.jpg)
Insert Command: i
• adds one or more lines directly to the output before the address:– inserted “text” never appears in sed’s
pattern space– cannot be used with a range address; can
only be used with the single-line and set-of-lines address types
Syntax:[address] i\text
88CSCI 330 - The Unix System
![Page 89: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/89.jpg)
Example: Insert Command (i)% cat tuition.insert.sed1 i\ Tuition List\
% cat tuition.dataPart-time 1003.99Two-thirds-time 1506.49Full-time 2012.29% sed -f tuition.insert.sed tuition.data Tuition List
Part-time 1003.99Two-thirds-time 1506.49Full-time 2012.29
89CSCI 330 - The Unix System
Input data
Sed script to insert “Tuition List” as report title before line 1
Output after applyingthe insert command
![Page 90: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/90.jpg)
Append Command: a
• adds one or more lines directly to the output after the address: – Similar to the insert command (i), append
cannot be used with a range address.– Appended “text” does not appear in sed’s
pattern space.
Syntax:[address] a\text
90CSCI 330 - The Unix System
![Page 91: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/91.jpg)
Example: Append Command (a)% cat tuition.append.seda \--------------------------% cat tuition.dataPart-time 1003.99Two-thirds-time 1506.49Full-time 2012.29% sed -f tuition.append.sed tuition.dataPart-time 1003.99--------------------------Two-thirds-time 1506.49--------------------------Full-time 2012.29--------------------------
91CSCI 330 - The Unix System
Input data
Sed script to appenddashed line after each input line
Output after applyingthe append command
![Page 92: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/92.jpg)
Change Command: c
• replaces an entire matched line with new text
• accepts four address types: – single-line, set-of-line, range, and
nested addresses.
Syntax:address c\text
92CSCI 330 - The Unix System
![Page 93: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/93.jpg)
Example: Change Command (c)% cat tuition.change.sed1 c\Part-time 1100.00% cat tuition.dataPart-time 1003.99Two-thirds-time 1506.49Full-time 2012.29% sed -f tuition.change.sed tuition.dataPart-time 1100.00Two-thirds-time 1506.49Full-time 2012.29
93CSCI 330 - The Unix System
Input data
Sed script to change tuition cost from1003.99 to 1100.00
Output after applyingthe change command
![Page 94: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/94.jpg)
Delete Command: d
• deletes the entire pattern space– commands following the delete
command are ignored since the deleted text is no longer in the pattern space
Syntax:address d
94CSCI 330 - The Unix System
![Page 95: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/95.jpg)
Example: Delete Command (d)
• Remove part-time data from “tuition.data” file
% cat tuition.dataPart-time 1003.99Two-thirds-time 1506.49Full-time 2012.29
% sed –e '/^Part-time/d' tuition.dataTwo-thirds-time 1506.49Full-time 2012.29
95CSCI 330 - The Unix System
Input data
Output after applying delete command
![Page 96: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/96.jpg)
Substitute Command (s)
Syntax: address s/search/replace/[flags]
• replaces text selected by search string with replacement string
• search string can be regular expression
• flags: – global (g), i.e. replace all occurrences_ n is for specific occurence
96CSCI 330 - The Unix System
![Page 97: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/97.jpg)
sed i/o commands
98CSCI 330 - The Unix System
![Page 98: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/98.jpg)
Input (next) Command: n and N
• Forces sed to read the next input line– Copies the contents of the pattern space to
output– Deletes the current line in the pattern space– Refills it with the next input line– Continue processing
• N (uppercase) Command– adds the next input line to the current
contents of the pattern space– useful when applying patterns to two or
more lines at the same time99CSCI 330 - The Unix System
![Page 99: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/99.jpg)
cat test.datfirst line second line line number 3$sed '/second/{n; s/3/three/}' test.datfirst line second line line number three
![Page 100: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/100.jpg)
Output Command: p and P
• Print Command (p)– copies the entire contents of the pattern
space to output– will print same line twice unless the option
“–n” is used• Print command: P
– prints only the first line of the pattern space– prints the contents of the pattern space up
to and including a new line character– any text following the first new line is not
printed101CSCI 330 - The Unix System
![Page 101: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/101.jpg)
• cat test.datfirst line second line line number three forth Line$sed -e '/second/{h; d;}' -e '$g' test.datsecond line
![Page 102: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/102.jpg)
Holding: H command
• cat empl.datAlbert Bronx:7187634623:Manhattan:m:56:32:cobol Alex Stachelin:7182347634:Brooklyn:m:60:60:unixElizabethHarrington:7183214567:Brooklyn:f:42:40:cobolFahd Main:7186794751:Queens:m:56:35:java Greg Norman:7182237890:Queens:m:45:0:java
$sed -e '/cobol/{H; d;}' -e '$g' empl.datAlex Bronx:7187634623:Manhattan:m:56:32:cobol Elizabeth
Harrington:7183214567:Brooklyn:f:42:40:cobol
![Page 103: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/103.jpg)
File commands
• allows to read and write from/to file while processing standard input
• read: r command
• write: w command
110CSCI 330 - The Unix System
![Page 104: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/104.jpg)
Read File command
Syntax: r filename
– queue the contents of filename to be read and inserted into the output stream at the end of the current cycle, or when the next input line is read
$sed '/cobol/r line.txt' test.dat
111CSCI 330 - The Unix System
![Page 105: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/105.jpg)
Write File command
Syntax: w filename
– Write the pattern space to filename – The filename will be created (or
truncated) before the first input line is read
112CSCI 330 - The Unix System
![Page 106: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/106.jpg)
Writing: w command
$sed -n '/cobol/ w cobol_empl.dat' empl.datcat cobol_empl.dat
Albert Bronx:7187634623:Manhattan:m:56:32:cobol Elizabeth Harrington:7183214567:Brooklyn:f:42:40:cobol
![Page 107: Unit 1 Linux](https://reader036.vdocuments.mx/reader036/viewer/2022062523/577cce1b1a28ab9e788d54cf/html5/thumbnails/107.jpg)
Example: The quit (q) Command
Syntax: [addr]q– Quit (exit sed) when addr is encountered.
Example: Display the first 50 lines and quit% sed -e ’50q’ datafile
Same as:% sed -n -e ‘1,50p’ datafile% head -50 datafile
116CSCI 330 - The Unix System