CPS 250, 346, &
444/544 Lecture notes: Files & directories
(manipulation & management)
Coverage: [UPE] Chapters 2 and
Chapter 7 (pp. 201-202)
Basic UNIX file nomenclature
- absolute or fully qualified path vs. relative path
- current working directory (stored in PWD variable)
- . and .. (are not shell metacharacters)
- types of files
- see man ls
- ls -l
| b | block special file |
| c | character special file |
| d | directory |
| l | symbolic link |
| s | socket link |
| p | FIFO |
| - | regular file |
ls and cal
- ls [<dir(s)/file(s)>]
(list): list directory contents;
without a directory argument defaults to the
current directory, as do most UNIX commands
ls with file arguments lists information
about only those files
- ls -a [<dir(s)/file(s)>] (all):
list directory contents including hidden (dot) files;
also lists hidden (dot) files, which are
typically configuration files (e.g., .profile)
- ls -l [<dir(s)/file(s)>]
(long listing): list directory contents using long listing format;
explanation of fields
- file permission string (-rwx------)
- owner, group, other
- ls -al
- ls -F [<dir(s)/file(s)>] (fancy):
list directory contents following directories
with a slash, executable files with an asterisk,
links with a @, including other various decorations;
fancier output
- ls -ld <dir>:
lists details of actual directory,
rather than its contents
- cal
- cal 9 (print calendar for year 9, not Sep, current year)
- cal 2004
- cal 9 1752
Explanation of ls -l output
total 6
drwxr-xr-x 6 lucy cps444 480 Dec 6 17:42 .
drwxr-xr-x 26 root system 816 Dec 6 14:45 ..
drwxr-xr-x 2 lucy cps444 80 Oct 25 08:33 bin
drwx------ 2 lucy cps444 144 Nov 30 14:25 C
-rw-rw-r-- 1 lucy cps444 104 Sep 12 19:54 notes
-rwxrwxr-- 1 lucy cps444 85 Jan 4 20:06 run*
drw-rw-rw- 2 lucy cps444 32 Mar 23 13:45 memos
drwx------ 16 lucy cps444 1296 Dec 6 17:33 text
total line gives the number of blocks in the directory; a block in
most UNIX systems = 1/2k (512 bytes), the above directory contains 3k
the first character of each following row indicates the type of file:
d = directory; - = plain file; l = link
characters 2 thru 10 give the file access permissions
(r = read; w = write; x = execute)
characters 2, 3, 4 give the owner's permissions
characters 5, 6, 7 given the group's permissions
characters 8, 9, 10 given the other user's permissions
- denotes absence of the permission
number of links to the file
file owner's user id
group name for the file
size of the file in bytes (characters)
file creation or modification date and time, and the name of the file
UNIX filesystem
- directories (branches) contain either files or
subdirectories (branches of branches)
- the filesystem is usually viewed as an inverted (upside
down) tree
- highest level directory = root /
- user's current directory is known as the working directory
- all of the following directories
are owned by user root (system administrator)
- /tmp (temporary files);
need not have special privileges to write here
- /bin (binaries, executables)
- /sbin
- /src (source code)
- /usr
- /usr/src
- /usr/src/lib
- /usr/bin
- /usr/include ← contains stdio.h and string.h
- /usr/lib
- /usr/lib64 ← contains libc.a
- /home (user home directories)
- /lib (libraries)
- /lib64 (libraries) ← contains libc.so.6
- /etc (configuration files)
- /dev
(devices)
- /dev/null (system trashcan)
- hier (only on some systems)
- tree (only on some systems)
Absolute vs. relative path
- pathname: filename preceded by directories leading to the file
- absolute pathname: the complete pathname of a file
starting with the root /
(e.g., /home/cps444-n1.01/homeworks/hw1/wc.c)
- relative pathname: pathname which implicitly starts at the
working directory (e.g., homeworks/hw1/wc.c)
Two special files in every directory
- . and ..
- . is a link to the current working directory
- .. is a link to the directory containing the
current working directory (i.e., its parent)
- why do we need these?
Navigating through directories
(below pathnames to <file>
or <dir> can be absolute or relative)
- cd <dir> (change directory):
change directory to <dir>
alters working directory; used to traverse
UNIX hierarchical tree file structure
- cd: change to home directory
- cd ..: changes to parent directory of working directory
- cd ~<logname>: change to home directory
of user with login name <logname>
- cd ~-: change to previous working directory
File manipulation and management
- pwd: print working directory;
displays current working directory path
- cp <src> <dest>: copy file(s)
- for instance, cp ../hw1/wc.c wc.bak:
creates a separate file wc.bak; changes
made to one do not affect the other
- for instance, $ cp ../hw1/* .;
copies all files from parent/hw1
to the current directory
- rm <file(s)> (remove): erases file(s)
- be careful, no undelete, no training wheels
- does not notify user of the action that has occurred
- rm accepts a list of files to erase
- for instance, rm *.c erases all of the C files,
- whereas rm * erases all of the files in the working directory
- rm -i wc.c: forces rm to query before erasing files
- interactive
- adds confirmation (training wheels)
- for instance, rm -i ../hw1/wc.c
- some like alias rm="rm -i"
- mkdir <dir(s)>
(make directory): creates one or more directories
- rmdir <dir(s)>
(remove directory): deletes one or more directories which must be empty
- rm -r <file(s)/dir(s)>
- use with extreme caution
- directory need not be empty
- mv <src> <dest> (move):
relocates the file(s) into the specified directory
- for instance, mv wc.c ../backups/
- mv is also used to rename a
file leaving the location unaltered (e.g.,
mv wc.c wc1.c)
- more <file(s)> (or page):
display <file(s)> one screen at a time; use spacebar to
page through
- less <file(s)>:
displays/browse file(s) one screen at a time, move
forward or backward through file, and can search for patterns (unlike more,
browsing begins before entire file loaded into memory)
- spacebar pages through
- enter key scrolls down a line
- searchable through /<keyword>;
n moves cursor to next match
- hit q key to quite
- head, tail:
can specify number of lines with option
- lpr <file(s)>
(line printer): prints one or more files on the default printer
- pushd and popd
References
| [C] |
C Language for Experienced Programmers, Version 2.0.0, AT&T, 1988. |
| [UPE] |
B.W. Kernighan and R. Pike. The UNIX Programming Environment.
Prentice Hall, Upper Saddle River, NJ, Second edition, 1984.
|
|