environment of Unix, Directory commands in UNIX UNIX FILES ,Naming Files in UNIX,FILE RELATED COMMANDS of Unix
FILE RELATED COMMANDS
cat: DISPLAYING AND CREATING FILES
cat command is used to display the contents of a small file on the terminal.
$ cat hello.c
# include <stdio.h>
void main ()
{
printf(“hello”);
}
As like other files cat accepts more than one filename as arguments
$ cat ch1 ch2
It contains the contents of ch1 It contains the contents of ch2
In this the contents of the second files are shown immediately after the first file without any header information. So cat concatenates two files- hence its name.
cat OPTIONS :
Displaying Nonprinting Characters (-v)
cat without any option it will display text files.
Numbering Lines (-n)
-n option numbers lines. This numbering option helps programmer in debugging programs.
Using cat to create a file: cat is also useful for creating a file. Enter the command cat, followed by > character and the filename.
$ cat > new
This is a new file which contains some text, just to Add some contents to the file new.
When the command line is terminated with [Enter], the prompt vanishes. Cat now waits to take input from the user. Enter few lines; press [ctrl-d] to signify the end of input to the system to display the file contents of new use file name with cat command.
This is a new file which contains some text, just to Add some contents to the file new.
$ cat new
cp: COPYING A File :
- The cp command copies a file or a group of files. It creates an exact image of the file on the disk with a different name. The syntax takes two filename to be specified in the command line.
- When both are ordinary files, first file is copied to second. $ cp csa csb
- If the destination file (csb) doesn‘t exist, it will first be created before copying takes place. If not it will simply be overwritten without any warning from the system.
- Example to show two ways of copying files to the cs directory: $ cp ch1 cs/module1 #ch1 copied to module1 under cs $ cp ch1 cs #ch1 retains its name under cs
- cp command can be used to copy more than one file with a single invocation of the command. In this case the last filename must be a directory.
Ex: To copy the file ch1,chh2,ch3 to the module , use cp as
$ cp ch1 ch2 ch3 module
- The files will have the same name in module. If the files are already resident in module, they will be overwritten. In the above diagram module directory should already exist and cp doesn‘t able create a directory.
- UNIX system uses * as a shorthand for multiple filenames.
# Copies all the files beginning with ch$ cp ch* usp
cp options :
Interactive Copying(-i) :
The –i option warns the user before overwriting the destination file, If unit 1 exists, cp prompts for response
$ cp -i ch1 unit1$ cp: overwrite unit1 (yes/no)? Y
Copying directory structure (-R) :
It performs recursive behavior command can descend a directory and examine all files in its subdirectories. -R : behaves recursively to copy an entire directory structure
$ cp -R usp newusp
$ cp -R class newclass
If the new class/new usp doesn‘t exist, cp creates it along with the associated sub directories.
rm: DELETING FILES :
The rm command deletes one or more files.
Ex: Following command deletes three files:
$ rm mod1 mod2 mod3
Can remove two chapters from usp directory without having to cd
$ rm usp/marks ds/marks
To remove all file in a directory use *
$ rm *
Removes all files from that directory .
rm options :
Interactive Deletion (-i) :
Ask the user confirmation before removing each file:
$ rm -i ch1 ch2
rm: remove ch1 (yes/no)? ? n [Enter]
A ‗y‘ removes the file (ch1) any other response like n or any other key leave the file undeleted.
Recursive deletion (-r or -R):
It performs a recursive search for all directories and files within these subdirectories. At each stage it deletes everything it finds.
$ rm -r *#Works as rmdir
It deletes all files in the current directory and all its subdirectories.
Forcing Removal (-f):
rm prompts for removal if a file is write-protected. The –f option overrides this minor protection and forces removal.
rm -rf*Deletes everything in the current directory and below
mv: RENAMING FILES :
The mv command renames (moves) files. The main two functions are:
It renames a file(or directory) It moves a group of files to different directory ,It doesn't create a copy of the file; it merely renames it. No additional space is consumed on disk during renaming.
Ex: To rename the file csb as csa we can use the following command
$ mv csb csa
If the destination file doesn‘t exist in the current directory, it will be created. Or else it will just rename the specified file in mv command. A group of files can be moved to a directory.
Ex: Moves three files ch1,ch2,ch3 to the directory module
$ mv ch1 ch2 ch3 module
Can also used to rename directory
$ mv rename newname
mv replaces the filename in the existing directory entry with the new name. It doesn't create a copy of the file; it renames it
Group of files can be moved to a directory
$ mv chp1 chap2 chap3 Unix
wc: COUNTING LINES,WORDS AND CHARACTERS :
wc command performs Word counting including counting of lines and characters in a specified file. It takes one or more filename as arguments and displays a four columnar output.
$ wc ofile
4 20 97 ofile
Line: Any group of characters not containing a newline
Word: group of characters not containing a space, tab or newline.
Character: smallest unit of information, and includes a space, tab and newline
wc offers 3 options to make a specific count. –l option counts only number of lines, - w and –c
options count words and characters, respectively.
$ wc -l ofile 4 ofile
$ wc -w ofile 20 ofile
Multiple filenames, wc produces a line for each file, as well as a total count.
$ wc -c ofile file 97 ofile 15 file112 total