Reference Using Linux File System
Reference Using Linux File System
File Structure
All of the files in the UNIX file system are organized into a multi-level hierarchy called a
directory tree.
A family tree is an example of a hierarchical structure that represents how the UNIX file
system is organized. The UNIX file system might also be envisioned as an inverted tree or the
root system of plant.
At the very top of the file system is single directory called "root" which is represented by a /
(slash). All other files are "descendents" of root.
The number of levels is largely arbitrary, although most UNIX systems share some
organizational similarities. The "standard" UNIX file system is discussed later.
Example:
File Types
The UNIX filesystem contains several different types of files:
Ordinary Files
o Used to store your information, such as some text you have written or an image you
have drawn. This is the type of file that you usually work with.
o Always located within/under a directory file
o Do not contain other files
Directories
o Branching points in the hierarchical tree
o Used to organize groups of files
o May contain ordinary files, special files or other directories
1 of 5
UNIX File System
o Never contain "real" information which you would work with (such as text). Basically,
just used for organizing files.
o All files are descendants of the root directory, ( named / ) located at the top of the
tree.
Special Files
o Used to represent a real physical device such as a printer, tape drive or terminal,
used for Input/Ouput (I/O) operations
o Unix considers any device attached to the system to be a file including your terminal:
By default, a command treats your terminal as the standard input file (stdin)
from which to read its input
Your terminal is also treated as the standard output file (stdout) to which a
command's output is send
Stdin and stdout will be discussed in more detail later
o Two types of I/O: character and block
o Usually only found under directories named /dev
Pipes
o UNIX allows you to link commands together using a pipe. The pipe acts a temporary
file which only exists to hold data from one command until it is read by another
o For example, to pipe the output from one command into another command:
who |wc -l
This command will tell you how many users are currently logged into the system. The
standard output from the who command is a list of all the users currently logged into
the system. This output is piped into the wc command as its standard input. Used
with the -l option this command counts the numbers of lines in the standard input
and displays the result on its standard output - your terminal.
File Names
UNIX permits file names to use most characters, but avoid spaces, tabs and characters that
have a special meaning to the shell, such as:
&;()|?\'"`[]{}<>$-!/
Case Sensitivity: uppercase and lowercase are not the same! These are three different files:
NOVEMBER November november
Hidden Files: have names that begin with a dot (.) For example:
.cshrc .login .mailrc .mwmrc
2 of 5
Uniqueness: as children in a family, no two files with the same parent directory can have the
same name. Files located in separate directories can have identical names.
Reserved Filenames:
/ - the root directory (slash)
. - current directory (period)
.. - parent directory (double period)
~ - your home directory (tilde)
Pathnames
Specify where a file is located in the hierarchically organized file system
Must know how to use pathnames to navigate the UNIX file system
Absolute Pathname: tells how to reach a file begining from the root; always begins with /
(slash). For example:
/usr/local/doc/training/sample.f
Relative Pathname: tells how to reach a file from the directory you are currently in ( current
or working directory); never begins with / (slash). For example:
training/sample.f
../bin
~/projects/report.001
For example, if your current directory is /usr/home/johnson and you wanted to change to
the directory /usr/home/quattro, you could use either of these commands:
cd ../quattro - relative pathname
cd /usr/home/quattro - absolute pathname
ls - lists files
ls - show contents of working directory
ls file - list file, if it exists in working directory
ls dir - show contents of the directory dir
ls -a - shows all your files, including hidden ones
ls -al - give detailed listing of contents
ls -F - mark directories with "/" and executable
more - browses/displays files one screen at a time. Use h for help, spacebar to page, b for
back, q to quit, /string to search for string
more sample.f
less - similar to more, but with more features. Not available on every system.
less sample.f
3 of 5
UNIX File System
cat - dumps the entire file to the screen without paging. This command is more useful for
concatenating (hence the name "cat") files together than it is for reading files.
cat myprog.c - diplays entire file
cat -b myprog.c - shows line numbers
cat file1 file2 > file3 - adds file1 and file2 to make file3
cp - copies files. Will overwrite unless otherwise specified. Must also have write permission in
the destination directory.
cp sample.f sample2.f - copies sample.f to sample2.f
cp -R dir1 dir2 - copies contents of directory dir1 to dir2
cp -i file.1 file.new - prompts if file.new will be overwritten
cp *.txt chapt1 - copies all files with .txt suffix to directory chapt1
cp /usr/doc/README ~ - copies file to your home directory
cp ~betty/index . - copies the file "index" from user betty's home
directory to current directory
mv - moves files. Will overwrite unless otherwise specified. Must also have write permission
in the destination directory.
mv sample.f sample2.f - moves sample.f to sample2.f
mv dir1 newdir/dir2 - moves contents of directory dir1 to newdir/dir2
mv -i file.1 file.new - prompts if file.new will be overwritten
mv *.txt chapt1 - moves all files with .txt suffix to directory chapt1
pwd - print working directory. Tells you which directory you are currently in.
pwd
mkdir - make directory. Will create the new directory in your working directory by default.
mkdir /u/training/data
mkdir data2
cd - change to specified directory. May specify either the absolute or relative pathname. cd
with no pathname changes to your home directory.
cd /usr/local - change to /usr/local
cd doc/training - change to doc/training in current directory
cd .. - change to parent directory
cd ~/data - change to data directory in home directory
cd ~joe - change to user joe's home directory
cd - change to home directory
4 of 5
rmdir - remove directory. Directories must be empty before you remove them.
rmdir project1
To recursively remove nested directories, use the rm command with the -r option:
rm -r directory_name
Access Permissions
UNIX is a multi-user system. Every file and directory in your account can be protected from
or made accessible to other users by changing its access permissions. Every user has
responsibility for controlling access to their files.
Note: a directory must have both r and x permissions if the files it contains are to be
accessed.
The chmod command is used to change access permissions for files which you own. The
syntax is:
chmod [who][action][permissions] filename
Examples:
chmod a+r sample.f - Adds read permission for all users to the file sample.f.
chmod o-r sample.f - Removes read permission for others to the file sample.f.
chmod og+rx prog*. - Adds read and execute permissions for group and others to all files
which contain "prog" as the first four characters of their name.
chmod +w * - Adds write permission for user to all files in current directory.
5 of 5