Title: Manipulating files in UNIX
1Manipulating files in UNIX
2Common operations of files
- Common operations
- We will learn to do these operations and
more....
- Create a file
- Print a file
- Delete a file
- Rename a file
- Move a file from one directory into another
directory
3Identifying a file
- We need to identify a file before we can perform
an operation (like delete) on the file. - A file can be identified using
- An absolute (file) path, or
- An relative (file) path
4Absolute file path
- An absolute file path tells the computer how to
find a file starting from the root directory - An absolute file path of a particular file x
consists of a list of directory names starting
from the root directory "/" to the directory
containing the file x and then followed by the
name of the file (x) - The list of names is separated by the "/" symbol
5Absolute file path (cont.)
The path for the indicated file (myFile2) is
- /home/cheung/cs170/myFile2
6Relative file path
- An relative file path tells the computer how to
find a file starting from the current directory - An relative file path of a particular file x
consists of a list of directory names starting
from the current directory to the directory
containing the file x and then followed by the
name of the file (x) - The list of names is separated by the "/" symbol
7Relative file path (cont.)
- Example 1
- If the current directory is /home/cheung, then
the path for the indicated file (myFile2) is
8Relative file path (cont.)
If the current directory is /home/cheung/cs170,
then the path for the indicated file (myFile2)
is
9Relative file path (cont.)
- When working on files, always change the working
directory to the one that contains the files. - It will save you a lot of key strokes (typing)
10Common operations on files
- Common operations on files
- Create a file
- Print a file to the terminal
- Print a file to the printer
- Delete a file
- Rename a file
- Move a file from one directory (folder) to
another directory (folder)
11Create a file
- An electronic file is created using a computer
application (program) called an editor - (An editor in computer lingo is a program !!!)
- Some commonly used editors that you have used on
a PC - We will learn to use gedit (GNU editor) in
another webnote.
12Print the content of a file out to the terminal
- The command ( application) that is used to print
the content of a file is - (cat is an abbreviation of the word catenate)
cat FILE-PATH
13Print the content of a file out to the terminal
(cont.)
14Catenating multiple files
- The cat command can catenate an arbitrary number
of files to the terminal - Example catenate myFile1 and myFile2 to the
terminal
15Redirecting the input and output to a file
- In UNIX, the output that an application prints to
the terminal, can be stored to a file - Also, the input that an application reads from
the keyboard, can be read from a file. This
feature is called
- Input/Output redirection (or IO redirection for
short)
16Redirecting the input and output to a file (cont.)
- Redirecting the output to a file
- The output of any UNIX command can be sent to a
file - by adding " gt FileName" to the command
- In other words
- any UNIX command gt FileName
17Redirecting the input and output to a file (cont.)
- Example redirecting the output of "ls" into a
file
18Redirecting the input and output to a file (cont.)
- Example redirecting the output of cat myFile1
myFile2 to a file (named myFile3)
19Redirecting the input and output to a file (cont.)
- You can see that myFile3 contains the catenation
of the files myFile1 and myFile2 -
- So the cat command can be used to catenate
multiple files together !!! -
- (Hence the name cat)
- We will learn about input re-direction at a later
lecture
20Print a file to the printer
- Use this command to print a file to the default
printer - When you print a file from a computer in the
MathCS lab, the default printer is the printer
located inside the area where the Lab
assistant(s) sits
lpr File-Path
21Print a file to the printer (cont.)
- Example
- will print the file named myFile1 in the current
directory to the printer -
lpr myFile1
22Delete a file
- The command (application) used to delete a file
is - The word rm is an acronym for remove
- All files with names matching the File-Path will
be removed
rm File-Path
23Delete a file (cont.)
rm myFile1 (will delete the file named "myFile1"
in the current directory) rm
/home/cheung/cs170/myFile1 (will delete the file
"myFile1" in the directory/home/cheung/cs17
0)
24Delete a file (cont.)
- Make sure that your current directory is the
correct one when you use a relative path with all
UNIX commands !!!
25Recovering deleted files in UNIX
- When you delete a file (with rm, the file is
really deleted in UNIX - What I mean is the file is not moved into a
trash directory - (That's what happens in Microsoft Windows)
- In other words your file is gone forever
26Recovering deleted files in UNIX
- Restoring a file with a backup version
- Every night, all files in a UNIX system is saved
(backup) - If you deleted a file, you can recover an older
version of the file as of yesterday - In other words any work you do after the backup
was made, will be lost.... - Send an email to help_at_mathcs.emory.edu if you
need to recover a file using a backup copy.
27Rename a file
- The command (application) used to rename a file
with name old-File-Path to the name new-File-Path
is
mv old-File-Path new-File-Path
28Rename a file (cont.)
- The new-File-Path file must not exist otherwise,
2 things can happen - If the new-File-Path exists and it is the name of
a file, then the mv will report an error (and
will not rename the file) - If the new-File-Path exists and it is the name of
a directory, then the mv will move the file
old-File-Path into the directory new-File-Path
29Rename a file (cont.)
- Example
- will rename the file named myFile2 to the new
name decl-of-indep
mv myFile2 decl-of-indep
30Rename a file (cont.)
- Illustrated
- Notice that after the file is renamed, the
content of the file remains unchanged ! -
31Move a file from one directory (folder) to
another directory (folder)
- The command (application) used to mv a file with
name file-Path into the directory dir-Path is
mv file-Path dir-Path
32Move a file from one directory (folder) to
another directory (folder) (cont.)
- This is in fact the same command for renaming a
file - The difference is dir-Path must be the path of
an existing directory. - (If dir-Path, the command will perform a rename
operation !!!)
33Move a file from one directory (folder) to
another directory (folder) (cont.)
mv command with non-existing directory
mv command with existing directory