file commands of linux ii

33
page 1 Command Purpose $ cat file1 $ more file1 $ cat file2 To see the contents of the file2 to see the contents of the file1 Suppose file1 contains the text: It is a fun. You are encouraged to work with the Linux. screen by screen view of file if it is lengthy $ cat file1 > file2 file1 sends the output to the standard output but the redirection operator (>) sends (redirects) output of the standard output to the file2. The redirection operator (>) prevents the output from going to the screen. The part after > is executed first. If file2 does not exist, file2 will be created and the contents of file1 will be copied into file2. if the file already exists, then contents of file2 will be destroyed and made fresh and then contents file1 will be copied Then contents of the left hand side file namely file1 is sent to the standard output. The output of the standard output is taken as the input for file2. In short the contents of the file1 is copied into the file2.

Upload: mutharam

Post on 16-Apr-2017

1.163 views

Category:

Education


1 download

TRANSCRIPT

Page 1: File Commands Of Linux  Ii

page 1

Command Purpose Output / activity taken

$ cat file1

file1 is copied into file2 also

The part after > is executed first.

$ cat file2 To see the contents of the file2

to see the contents of the file1Suppose file1 contains the text:It is a fun. You are encouraged to work with the Linux.

It is a fun.You are encouraged to work with the Linux.

$ more file1

screen by screen view of file if it is lengthy

It is a fun.You are encouraged to work with the Linux.

$ cat file1 > file2

file1 sends the output to the standard output but the redirection operator (>) sends(redirects) output of the standard output to the file2. The redirection operator (>) prevents the output from going to the screen.

If file2 does not exist, file2 will be created and the contents of file1 will be copied into file2.

if the file already exists, then contents of file2 will be destroyed and made fresh and then contents file1 will be copied

Then contents of the left hand side file namely file1is sent to the standard output. The output of the standard output is takenas the input for file2. In short the contents of the file1 is copied into thefile2.

It is a fun.You are encouraged to work with the Linux.

Page 2: File Commands Of Linux  Ii

page 2

Command Purpose Output / activity takenfile1 content will be nothing

$ cat

Combination of the cat command with output redirection operator (>)

cat file1 > file1

empties the file1 (Since RHS & LHS name are same). So the command fails.

You can set the noclobber feature to prevent overwriting an existing$ cat file1 >> file2

To overcome the difficulty of overwriting the existing file append (>>) operator is used. The append (>>) operator adds the contents of the file, appearing left side of “>>” operator to the file appearing to the right side of the same operator, at the end of the existing material

appends the contents file1 to the contents of file2.

The cat command without any argument takes the input from the standard input

expects data from the standard input.After receiving it displays immediately in the next line

*** The entered message is taken to the standard input fromthe buffer, which is directed to the cat command. Since there is no redirection operator, cat displays the message on the screen

***SRMSRMChennaiChennai

After completion inform it to computer by pressing ctrl+D keys in a separate line

Note: Data can be compared to the water in a dam. Buffer is like a dam where water is collected before sending for irrigation (the standard output) when there is copious

supply of water (data). It stores and sends water (data) in more orderly manner when there is a request for water or when the dam is full. Ctrl+D character is the end-of–file $ cat > file3

Here LHS name is not mentionedSo from standard input i.e keyboard input is received and redirected to file3On completion in a new line press ctrl+D.

Page 3: File Commands Of Linux  Ii

page 3

Command Purpose Output / activity takenCombination of the cat command with input redirection operator (<)

$ cat | lpr

$ cat < file3

To make the cat command to get data from the file3 (not from keyboard)

Standard input to receive data from files also.

This is made possible by the redirection input operator.

Displays contents of file3 on the screen

cat < file3 > file4

If the standard input is to be redirected to receive its data from file3, and the standard output is to be redirectedto place its data in file4,

file3 contents are redirected to file4

Piping operator(|) is used to redirect output of one command to another command whereas redirection operator used to redirect output of one file into another file.

The piping symbol is the vertical bar “|” (key before to backspace key).

$ cat file3 | lpr

to have the contents of file to be printed. The cat command gets contents of a file and sends it to the standard output. The output of cat is piped to lpr (line print i.e the printer connected to the system currently).

Here cat is one command and lpr is another command

User has to type in the input

While completion press ctrl+D. The typed matter is printed in the current printer

expects data from the standard input.After receiving it prints in the printer immediately in the next line

Page 4: File Commands Of Linux  Ii

page 4

Command Purpose Output / activity taken

$ cat file3 | more

$ cat –n file3 | more .

Sort command helps in arranging the contents in alphabatical order.

$ cat –n file3 | lpr

contents of file3 is sent to the printer (each line will be numbered)

Content of file3 is printed with their line number

The cat command with the option –n sends the contents of the file3 to the standard output after numbering each line.

If the file is lengthy and if you use the cat command, you can see only the last page. To overcome this difficulty the command more is usedThe contents will be displayed

screen by screen, by getting the confirmation from the user

page by page contents are displayed

page page display with line numbering of each line

page by page contents are displayed with each line numbers

$ cat –n file1 file2 file3 | more.

displaying more than one file page by page with line numbered for more than one file

Note: Comma should not be used between file names; only blank space/spaces should separate them.

$ sort file3 | more

sorts each line of the given file alphabetically and sends the sorted version to the standard output.

file3 is sorted & sent to monitor.

Page 5: File Commands Of Linux  Ii

page 5

Command Purpose Output / activity taken

$ sort file3 | cat -n | more$ sort file3 | cat -n | lpr

You can send the sorted output to more, cat –n, lpr or to any of the suitable combinations of these.

file3 is sorted & displayed screen by screenfile3 is sorted & sent to printer

Page 6: File Commands Of Linux  Ii

page 6

Command Purpose Output / activity taken

"

The tee command copies the standard output to a file. It takes as its argument the name of the new file to which the standard output is copied. It seems when the standard

output sees the tee command; it will split into two copies. Normally, one of them is redirected to the file appearing after the tee command and the other goes to the

$sort file5 | tee file6

The sorted contents of the file can be copied into another file and also can be displayed on the screen. *** file5 contents will not be affeted ***

sorted o/p of file5 is copied into file6 & displayed on monitor.

$ sort file5 | tee sfile5$ sort file5 | tee sfile5 | lpr

sfile5 created (sorted content) and displayed on screen, and printed

The output is :AshokChandranMalar

$ sort file5 | cat -n | tee sfile5 | lpr

sfile5 created (sorted content) and displayed with number on screen, and printed

The output is :1 Ashok2 Chandran3 Malar

Copying file - to copy the contents of one file into another in a straightforward manner - use cp command ( one of the command)

Syntax : $ cp [options] <source file/s> <destination directory/file>

Page 7: File Commands Of Linux  Ii

page 7

Command Purpose Output / activity taken

removing files and folders file1 and file2 are deleted

to delete even the subdirectories

gets confirmed and deletes.

$ rm -v alpha1

$ cp file1 file6

The cp command copies the contents of source file after creating destination file. If the destination file already exists then the existing file is destroyed then a new file withsame name is created

file1 contents are copied into file6

**** be careful in giving destination file name ****

$ cp -i file1 file2

-i option gives warning about overwriting of destination file (if exists)

$ cp -i file1 file2overwrite file2 ? n $

If files are not in the current directory, then the full path should be given. If you want to establish a link between file1 and file2, you should replace –i by –l.

$ cp -r alpha alpha1

copies a directory to another directory

If directory alpha1 exists already, all the contents are put inside the directory. If alpha1 des not exist it will be created and all the files and the sub-directories are stored. This alpha1 is created under the current working directory.

copies all the files and sub-directories of the alpha directory to the alpha1 directory recursively.

There are two more options -s and -v. The option -s creates a symboliclink and the option -v (stands for verbose) explains in detail, what is

being done. Removing Files$ rm file1 file2$ rm -r alpha1

removes alpha1 directory along with its subdirectories.

$ rm -i alpha1

removes only after confirmingalpha1 directory.

Page 8: File Commands Of Linux  Ii

page 8

Command Purpose Output / activity taken

char1 char2.ex

main.c fact.c swap.c

Char1

doc1 doc2

Wildcard entries & filename arguments - giving clue by providing partial information* stands for any number of characters

? fixes the number of characters. i.e ? Is = single character [ ] gives you a set of characters to search the file with them

$ ls main.c fact.c swap.c char1 char2.ex doc1 doc2

$ ls ch*

$ ls *.c

$ ls char? While the first four characters are fixed, the last one may be any character including numbersNote: char2.ex will not be displayed, since the length of char2.ex is greater than 5 characters.

$ ls doc[12]

doc[1-5] doc[a-g]

search for doc1, doc2, doc3, doc4, doc5. doca, docb, docc, docd, doce, docf, docg

Page 9: File Commands Of Linux  Ii

page 9

Command Purpose Output / activity takenMoving and Renaming the Files.

renaming

moving

–i, -v and –f options along with this type of commands

The mv (move) command is used Role of mv command 1. to move a file or directory from one location to another. 2. to change the name of a file or a directory.

Moving a file from one location to another is different from copying a file in that no file is created while moving a file.

Syntax: mv [options ] <source> <destination>

mv temp temporary

the temp directory is renamed into a temporary directory.

$ mv file1 /home/ilamathi/personal/file1

file1 of current directory is moved to the location /home/ilamathi/personal/

Page 10: File Commands Of Linux  Ii

page 10

Command Purpose Output / activity takenViewing the System Date and Time

$ date

Displays day of month in digits 5

1

Displays Year(last two digits) 9

Displays Date as mm/dd/yy 01/05/2009

Displays Time as HH:MM:SS 11:43:14

Displays Hour(00 to 23) 11

Displays Seconds(00 to 59) 43

Displays Minutes(00to 59) 14

MON

JAN

11:43:14 AM

Mon Jan 05 11:41:12 EST 2009

The day, the month, the year, the date, the time in hours, in minutes and$ date “+%d”$ date “+%m”

Displays Month of the year (in digits)

$ date “+%y”$ date “+%D”$ date “+%T”$ date “+%H”$ date “+%S”$ date “+%M”$ date “+%a”

Displays Abbreviated weekday(sun to sat)

$ date “+%h”

Displays Abbreviated month(jan to dec)

$ date “+%r”

Displays Time in the AM/PM notation

Note: With the help of the above options, the SA can change any part of the date command.

Page 11: File Commands Of Linux  Ii

page 11

Command Purpose Output / activity takenFile Systems: mount and umount

Device means the device files which are locted in /dev directories /dev/fd0 refers to first floppy drive

/dev/fd1 refers to second floppy drive

/dev/hda2 refers to hard disk second drive

The files themselves are organized into one perfect tree of directories beginning from the root. The floppy or even CD-ROM completely dependent upon the root directory. Establishing the connection between a file system on a storage device and your main directory tree is called mounting the device. This is done with the mount command.The syntax for the mount command is :

# mount /dev/fd0 /destination

mounting first floppy drive in the mentioned destination

Page 12: File Commands Of Linux  Ii

page 12

Command Purpose Output / activity taken

unmounts the floppy drive

Disconnecting a file system on a storage device from your main directory tree is called Unmounting the device. This is done with the umount command.The mounted file systems should be unmounted whenever

Syntax: # umount device (or destination )# umount /dev/fd0

Page 13: File Commands Of Linux  Ii

page 13

Page 14: File Commands Of Linux  Ii

Activity Key

ç ctrl + B

è crtl + F

Erasing characters ctrl + H - backspace

Deleting entire line ctrl + U

Example

$cat file1; sort file1; cp -i file1 file2

Keys used for editing text in the command linealternative key

combinationmoving one character to the leftmoving one character to the right

Back space and Delete key

We can enter more than one command in the same line but we should separate them with a semicolon ( ; ).

We can also enter only one command in several lines by typing a backslash in each line.

$cp \-i \file1 \file2

Page 15: File Commands Of Linux  Ii
Page 16: File Commands Of Linux  Ii

Linux commands for IX std

Command Purposepwd Present Working Directory - to be seencd to change Directorycd .. to move to parent directory

cd /usr/local/lib to move to some other directory named to be /usr/local/lib

ls to list files of current directoryls /usr/local to list files of mentioned directory /usr/localls a* listing files whose names are starting with letter a

ls a??

ls subject[0-9]

ls -d listing only directory namesls -l long format listingls -mls -a listing all files including hidden filesls -c listing files column wise ls -F identity directions, links and executablesls -R list Directory contents recursivelyls -S listing by sorting files by sizels -color use color to identify files

cat

cat >nineth

cat ninethcat file1.txt file2.t file1.txt and file2.txt combined and stored into file3.txtcat fil1>>file3 file3 contents are appended with the fil1 contentsmkdir srm creates the directory namely srm

cp srm rms

cp -i srm rms

cp -r dir1 dir2 Copies whole directory dir1 into another name dir2mv abc marks renames abc into marksmv -I abc marks gets confirmed and then replacesmv marks /tmp moves the file marks to the directory tmp

rm student deletes the file student rm -I students gets confirmed and then deleteesrm -r tmp deletes the directorylocate file* shows the directory names of files named file*whereis man displays manual page for man commandchown to change ownershipchmod to chane the permissionto individual files

chmod 644 file1.tx

chown arasu tiger Helps to change the ownership toarasu for tiger file

listing files whose names are starting with letter a and following which only 2 charactyers which may be anythinglisting files named subject and ending with any numbers from 0-9

listing names separated by commas

gets i/p by keyboarddisplays on monitorgets i/p by keyboardand stores on file named ninethdisplays the contents of file named nineth

copies srm into another name rmsif rms already exists then overwrites the old content with new entry

copies srm into another name rmsif rms already exists then gets confirmation before overwriting

cp -b student marks

makes the student file to be the back up in the name marks back up files ended with ~ symbol

Only owner has rwx permission others read only

Page 17: File Commands Of Linux  Ii

Linux commands for IX std

Page 18: File Commands Of Linux  Ii

Command Purpose Command Purpose

pwd Present Working Directory - to be seen ls subject[0-9]

cd to change Directory ls -d listing only directory names

cd .. to move to parent directory ls -l long format listing

cd /usr/local/lib ls -m

ls to list files of current directory ls -a listing all files including hidden files

ls /usr/local ls -c listing files column wise

ls a* ls -F

ls a?? ls -R list Directory contents recursively

listing files named subject and ending with any numbers from 0-9

to move to some other directory named to be /usr/local/lib listing names separated by commas

to list files of mentioned directory /usr/local

listing files whose names are starting with letter a

identity directions, links and executables

listing files whose names are starting with letter a and following which only 2 charactyers which may be anything

Page 19: File Commands Of Linux  Ii

Command Purpose Command Purpose

ls -S listing by sorting files by size cp srm rms

ls -color use color to identify files cp -i srm rms

cat cp -r dir1 dir2

cat >nineth mv abc marks renames abc into marks

cat nineth mv -I abc marks gets confirmed and then replaces

cat file1.txt file2.t mv marks /tmp

cat fil1>>file3

mkdir srm creates the directory namely srm rm student deletes the file student

copies srm into another name rmsif rms already exists then overwrites the old content with new entry

copies srm into another name rmsif rms already exists then gets confirmation before overwriting

gets i/p by keyboarddisplays on monitor

Copies whole directory dir1 into another name dir2

gets i/p by keyboardand stores on file named nineth

displays the contents of file named nineth

file1.txt and file2.txt combined and stored into file3.txt

moves the file marks to the directory tmp

file3 contents are appended with the fil1 contents

cp -b student marks

makes the student file to be the back up in the name marks back up files ended with ~ symbol

Page 20: File Commands Of Linux  Ii

Command Purpose Command Purpose

rm -I students gets confirmed and then deletees

rm -r tmp deletes the directory

locate file*

whereis man displays manual page for man command

chown to change ownership

chmod to chane the permissionto individual files

chmod 644 file1.tx

chown arasu tiger

shows the directory names of files named file*

Only owner has rwx permission others read only

Helps to change the ownership toarasu for tiger file

Page 21: File Commands Of Linux  Ii