You are on page 1of 27

Chapter 4

Accessing Files and Directories


Path Names
A path name represents the route
through the hierarchy that is
traversed to reach the desired
file or directory
Types of Pathnames
Absolute Pathname
Specifies the entire file hierarchy
Start at root (/) and list each directory along the path to the
destination
Slash (/) between each directory name in the path
Example:
/home/user3/f1

Relative Pathname
Always starts at your current location in the hierarchy
If a pathname does not begin with a slash, it is a relative
pathname
you must know what directory you are currently in since that
is your starting point

Absolute Path
Absolute Path
Absolute pathname to the user2 directory
/home/user2
Absolute pathname to the dir1 directory
/home/user2/dir1
Absolute pathname to the coffees directory
/home/user2/dir1/coffees

Relative Path
If your current directory is /home:

Relative pathname to the user2 directory
user2
Relative pathname to the dir1 directory
user2/dir1
Relative pathname to the coffees directory
user2/dir1/coffees

Relative Path
Some Special Directories
Absolute Relative to /home/user3

/home ..
/home/user2 ../user2
/home/user1/f1 ../user1/f1
/ ../..

The entry called dot(.) represents your current directory position

The entry called dot dot(..) represents the directory immediately
above your current directory position
Examples of DOT
If you are currently in the directory
/home/user3:

.
./f1
./memo/f1
Examples of dot dot
If you are currently in the directory /home:

..
../..
../tmp
../tmp/f1
If you are currently in the directory
/home/user3:

..
../..
../user2../user1/f1
../../tmp/f1
Using Navigation Shortcuts
pwd (print working directory) command -
no options or arguments
displays directory using absolute path name

cd (change directory) command -
used with absolute or relative pathnames to navigate
by itself takes you to your home directory

cd .. command takes you up one level
cd ~/ command takes you to a directory under your
home directory (tilde = home)
cd- Change Directory
Syntax:
cd [dir-pathname]
Examples:
$ pwd
/home/user3
$ cd memo; pwd
/home/user3/memo
$ cd .. /..; pwd
Using ls Command
ls
(list) listing of files and directories within the current directory or
specified directories
ls a
list all files in a directory, including hidden (.) files and current (.)
and parent (..) directories
ls F
displays listing with a symbol to tell what the type the file is:
directory A forward slash (/) after the name
ASCII Text File - no symbol
Executable asterisk (*) after the name
Symbolic Link An at sign (@)
Displaying Long Listing
Recursive Listing
ls -R (recursive) command
- Displays the contents of all directories, subdirectories and their
contents for a particular part of the directory tree
- If done at a high level in the directory structure, the output can be
substantial!
Creating & Removing Files & Directories

mkdir - creates directories or folders
must have the appropriate permissions to create a directory
p (parent) option creates parent directories while creating lower level
directories, including all the directories in a pathname

rm - removes a single file or multiple files
specify their names or use wildcard metacharacters (*) (?)
files that are deleted are permanent and cannot be recovered!
rm -i (interactive) - prompts the user before removing files
rm r (recursive) - removes directories
removes the directory including all subdirectories and files in it!
rm -ri (or rm -ir) - removes directories interactively
mkdir and rmdir create and remove directories

Syntax: mkdir [-p] dir_pathname (s)
rmdir dir_pathname (s)
Example:
$ pwd
$ mkdir fruit
$ mkdir fruit/apple
$ cd fruit
$ mkdir grape orange
$ rmdir orange
$ cd ..
$ rmdir fruit
$ rmdir fruit/apple fruit/grape fruit
Chapter 5
Basic Directory and File Management
Command Line Control Characters
What can we do with files?
ls -look at the characteristics of a
file
cat -look at the contents of a file
more -look at the contents of a file, one
screenful at a time
lp -print a file
cp -make a copy of a file
mv -change the name of a file or
directory
mv -move a file to another directory
ln -create another name for a file
rm -remove a file
cat more head tail Commands
head
displays the first n lines
first 10 lines are displayed by default if the -n
option is omitted.

tail
displays the last n lines of a file
last 10 lines are displayed by default if the -n
option is omitted.
allows you to check the end result of the backup
without looking at the whole file
-n option allows you to start displaying lines from a
specific point in a file

wc and diff Commands
wc (word count) command
displays line, word, byte or character counts for a text file
without options will give a line, word, and byte count of the contents of
the file
Option Function
-l Counts lines
-w Counts words
-c Counts bytes
-m Counts characters

diff (difference) command compares two text files and finds
differences
Command Format: $ diff [option] file1 file2
i option ignores the case of the letters
c option performs a detailed comparison and produces a listing
of differences with three lines of context
Copy Files
Syntax:
cp [-i] file1 new_file
cp [-i] file [file] dest_dir
cp r [-i] dir [dir] dest_dir

Examples:
$ ls F
$ cp f1 f1.copy
$ ls F
$ cp note remind memo
Move or Rename Files
Syntax:
mv [-i] file1 new_file
mv [-i] file [file] dest_dir
mv [-i] dir [dir] dest_dir
Examples:
ls F
mv f1 file1
ls F
mv f2 memo/file2
ls F
ls F memo

diff Output
Link Files
Syntax:
ln file new_file
ln file [file. . .] dest_dir
Example:
$ ls l f1
$ ln f1 /home/user2/f1.link
$ ls l f1
$ ls l /home/user2
$ ls i /home/user2/f1.link

You might also like