Creating a File
Many people create files using a text editor, but you can use the command cat to create files without learning a text editor. To create a practice file (named firstfile) and enter one line of text in it, type the following at the % prompt:
cat > firstfile
(Press the Enter/Return key.)
This is just a test.
(Press the Enter/Return key.)
Stop file entry by typing Control-d on a line by itself. (Hold down the Control key and type d.) On your screen you will see:(Press the Enter/Return key.)
This is just a test.
(Press the Enter/Return key.)
% cat > firstfile
This is just a test.
^D
One way to examine the contents of the file you've just created is to enter this at the % prompt:This is just a test.
^D
cat firstfile
Copying a File
To make a duplicate copy of a file, use the command cp. For example, to create an exact copy of the file called firstfile, you would type:
cp firstfile secondfile
The result is two files with different names, each containing the same information. The cp command works by overwriting information. If you create a different file called thirdfile and then type the following command:
cp thirdfile firstfile
you will find that the original contents of firstfile are gone, replaced by the contents of thirdfile.Renaming a File
Unix does not have a command specifically for renaming files. Instead, the mv command is used both to change the name of a file and to move a file into a different directory.To change the name of a file, use the following command format (where thirdfile and file3 are sample file names):
mv thirdfile file3
The result of this command is that there is no longer a file called thirdfile, but a new file called file3 contains what was previously in thirdfile.Like cp, the mv command also overwrites existing files. For example, if you have two files, fourthfile and secondfile, and you type the command
mv fourthfile secondfile
mv will remove the original contents of secondfile and replace them with the contents of fourthfile. The effect is that fourthfile is renamedsecondfile, but in the process secondfile is deleted.Removing a File
Use the rm command to remove a file. For example,
rm file3
deletes file3 and its contents. You may remove more than one file at a time by giving a list of files to be deleted. For example,
rm firstfile secondfile
You will be prompted to confirm whether you really want to remove the files:
rm: remove firstfile (y/n)? y
rm: remove secondfile (y/n)? n
Type y or yes to remove a file; type n or no to leave it.rm: remove secondfile (y/n)? n