Sei sulla pagina 1di 41

Chapter 2: File System

UNIX File System

 All of the files in the UNIX file system are organized into a multi-
leveled 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
"descendants" of root.
 The number of levels is largely arbitrary, although most UNIX
systems share some organizational similarities.
UNIX File System : Example
/ (root)
|
---------------------
| | |
/bin /usr /tmp
|
|
------------------------------------------------------------
| | | |
/public /misc /staff /students
| | |
------------ ------------------- ----------------------
| | | | | | | | | |
/software /doc /john /mary /bill /carl /tom /dick /mary /lisa
File

 File is container of information or file contains sequence of


characters or bytes.
 UNIX files does not contain EOF
 File attributes like size, name, etc are stored in a separate area,
accessible only to the kernel.
File Types

 Ordinary / regular files: Contains data which is a sequence of


characters.
 Directory files: Directory contains name of files and directories
present in it. Each entry contains unique id i.e. inode and their
name
 Device file: All devices are represented by files. To read or write a
device, operations are performed on the files.
Ordinary File

 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.
 Always located within/under a directory file
 Do not contain other files
 Two types of ordinary: Text and binary files.
 Text file contain only printable characters. Example: C files, Java
files, etc.
 Binary files contains both printable and unprintable characters.
Example executable files.
Directory File

 Branching points in the hierarchical tree


 Used to organize groups of files
 May contain ordinary files, special files or other directories
 Never contain "real" information which you would work with
(such as text). Basically, just used for organizing files.
 All files are descendants of the root directory, ( named / )
located at the top of the tree.
 A directory file contains an entry for every file and
subdirectory present in it. Each entry has two components:
 Filename
 Unique Identification Number (inode)
Device File
 Used to represent a real physical device such as a printer,
tape drive or terminal, used for Input/Ouput (I/O) operations
 Unix considers any device attached to the system to be a
file - including your terminal:
 Files are found under directories named /dev
File Names
 UNIX permits file names to use any characters
 Case Sensitivity: uppercase and lowercase are not the
same.
 Extensions: may be used to identify types of files
 libc.a - archive, library file
 program.c - C language source file
 alpha2.f - Fortran source file
 xwd2ps.o - Object/executable code
 mygames.Z - Compressed file
 Hidden Files: have names that begin with a dot (.) For
example: .cshrc .login .mailrc .mwmrc
File Names (contd.)
 Uniqueness: 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)
 ~ - home directory (tilde)
UNIX File System tree
UNIX File System tree
 /(root) - The top level directory referred to as root.
 /bin - Executable files for standard UNIX utilities
 /dev - Files that represent input/output devices
 /etc - Miscellaneous and system administrative
files such as the password file and system start up files.
 /lib - UNIX program libraries
 /tmp - Temporary space that can be used by
programs or users.
 /usr - More UNIX utilities.
 /var - Variable sized files - can grow and shrink
dynamically, such a users mail spool and print spool files.
 /sbin – contains administrative tools
Pathnames
 Specify where a file is located in the hierarchically organized
file system
 Absolute Pathname: tells how to reach a file beginning from
the root; always begins with / (slash). For example:
/usr/local/doc/training/sample.txt
 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.doc
Pathnames (contd.)
 For example, if your current directory is /usr/home/abc and
you wanted to change to the directory /usr/home/xyz, you
could use either of these commands:

 cd ../xyz - relative pathname


 cd /usr/home/xyz - absolute pathname
Directory Commands

 pwd - print working directory. Tells you which directory you


are currently in.
 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 ~abc - change to user abc's home directory
 cd - change to home directory
Directory Commands

 mkdir - make directory. Will create the new directory in your


working directory by default.

 mkdir /u/training/data
 mkdir data2
 mkdir -m 444 tech (Sets the access mode for the new
directory.)
 mkdir -p tech/net/faqs (If the parent directories don't exist,
this command creates them.)
 Reasons for faiilure of mkdir command:
 Directory already exists
 Fiile with same name already exists
 Permissions for current directory does not allow creation
of directory.
Ls Commands

 ls is a Linux shell command that lists directory contents of


files and directories.
 List Files using ls with no option:
ls with no option list files and directories in bare format
where we won’t be able to view details like file types, size,
modified date and time, permission and links etc.
$ ls
0001.pcap Desktop Downloads index.html
Ls Commands

 List Files With option –l


ls -l (-l is character not one) shows file or directory, size,
modified date and time, file or folder name and owner of file
and it’s permission.
$ ls -l
total 176
-rw-r--r--. 1 root 683 Aug 19 09:59 0001.pcap
-rw-------. 1 root 1586 Jul 31 02:17 ks.cfg
drwxr-xr-x. 2 root 4096 Jul 31 02:48 qwe
Ls Commands

 View Hidden Files with ls -a

$ ls -a
. .bashrc .gconfd .nautilus
 List Directories with ‘/’ Character at the end
$ ls -F
Desktop/ Downloads/ Pictures/
 Recursively list Sub-Directories
ls -R option will list very long listing directory trees.
Ls Commands

$ls -R
total 1384
-rw-------. 1 root root 33408 Aug 8 17:25 anaconda.log
-rw-------. 1 root root 30508 Aug 8 17:25
anaconda.program.log
./httpd:
total 132
-rw-r--r-- 1 root root 0 Aug 19 03:14 access_log
-rw-r--r--. 1 root root 61916 Aug 10 17:55 access_log-
20120812
./lighttpd:
total 68
-rw-r--r-- 1 lighttpd lighttpd 7858 Aug 21 15:26 access.log
-rw-r--r--. 1 lighttpd lighttpd 37531 Aug 17 18:21 access.log-
20120819
Ls Commands

 Sort Files by File Size


With combination of -lS displays file size in order, will display
big in size first.
# ls -lS
total 176
-rw-r--r--. 1 root 48867 Jul 31 02:17 install.log
-rw-r--r--. 1 root 46701 Jul 31 09:58 index.html
-rw-r--r--. 1 root 21262 Aug 12 12:42 fbcmd_update.php
-rw-r--r--. 1 root 11439 Jul 31 02:13 install.log.syslog
Ls Commands

 Display Inode number of File or Directory


With -i options list file / directory with inode number.
# ls -i
20112 0001.pcap 23610 Documents 23793
index.html
 List Directory Information
# ls -l /tmp
total 408
drwx------. 2 narad narad 4096 Aug 2 02:00
CRX_75DAF8CB7768
-r--------. 1 root root 384683 Aug 4 12:28 htop-1.0.1.tar.gz
drwx------. 2 root root 4096 Aug 4 11:20 keyring-6Mfjnk
drwx------. 2 root root 4096 Aug 16 01:33 keyring-pioZJr
Handling Files cp
cp - copies files. It overwrites target file. If target file does
not exist then it will be created. 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
Handling Files rm
rm - deletes/removes files or directories if file permissions
permit.
 rm sample.f - deletes sample.f
 rm chap?.txt - deletes all files with chap as the
first four characters of their name and with .txt as the last
four characters of their name
 rm -i * - deletes all files in current directory
but asks first for each file
 rm -r /olddir - recursively removes all files in the
directory olddir, including the directory itself
 rm –r * Deletes everything in the current directory
Handling Files mv
mv - moves files and used for moving a group of files to
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
 mv f1 f2 f3 dir – moves f1,f2 and f3 to dir.
Directory status after cp, mv and rm

FileName Inode FileName Inode


Number Cp f1 f2 Number
. 6557 . 6557
.. 7755 .. 7755
F1 3536 F1 3536
F2 54578

mv f1 f3
FileName Inode FileName Inode
Number Number
. 6557 rm f3 . 6557
.. 7755 .. 7755
F2 54578 F3 3536
F2 54578
more
 more - browses/displays files one screen at a time.
 Use f/spacebar to page
 b for back, q to quit
 10f to scroll 10 pages.
 Dot to repeat the last command
 /string to search for string

more sample.f
less is the standard pager of Linux and it behaves same as
more.
lp
 The lp command is used to print files on Unix and Linux
systems. The name "lp" stands for "line printer“
 # lp -m -n 30 -d lp0:lpd0 /etc/motd : Prints 30 copies
using printer lpd0 and notifies user using mail.
 # lp -t “Title" /etc/motd : Pints file with title.
wc
 The wc (word count) command is used to find out number
of newline count, word count and characters count in a
files specified by the file arguments.
 wc tecmint.txt
12 16 112 tecmint.txt
Here 12 is number of lines, 16 words and 112 characters
 Option –l, -w, -c.
diff
 diff command to display line-by-line difference between
two files.
 diff FILE1 FILE2
 Diff command will examine file1 and file2 and tells what
changes need to be made for file1 and file2 to match.
 diff command point to which lines need be:
 Added (a)
 Deleted (d)
 Changed (c)
 lines in file1 identified with a less than (<) symbol and
lines in file2 with a greater than (>) symbol.
comm
 Compare two sorted files line-by-line.
 comm produces three-column output. Column one
contains lines unique to FILE1, column two contains lines
unique to FILE2, and column three contains lines
common to both files.
 comm File1 File2
cmp
 cmp - Compare two files byte by byte

 $ cmp file1.txt file2.txt

 output: file1.txt file2.txt differ: byte x, line y


banner

 'banner' is a command which prints a high resolution text


banner on the system console
 banner UNIX
Basic File Attributes
 Ls –l list the basic file attributes.
 Ls looks up the file’s inode to fetch its features.
# ls -l /tmp
total 408
drwx------. 2 narad narad 4096 Aug 2 02:00
CRX_75DAF8CB7768
-r--------. 1 root root group1 384683 Aug 4 12:28 htop-
1.0.1.tar.gz
drwx------. 2 root root group1 4096 Aug 4 11:20 keyring-
6Mfjnk
drwx------. 2 root root group1 4096 Aug 16 01:33 keyring-
pioZJr
 408 is the total number of blocks occupied by these files
on the disk.
 File type: First column shows file type. – for ordinary and
d for directory files.
 This is followed by file permissions.
Basic File Attributes
 Links; Second column displays the number of links of a
file.
 Ownership: Next column shows name of the owner.
 Group ownership: Shows the group owner of the file.
 File Size: Size of file in bytes
 Creation or modification time
File Permissions
 Permission string of a file is broken into three groups:
1. Owner
2. Group
3. Others
 Each category contains three slots, read, write and
execute. Read is represented by r, write by w and
execute by x. – shows absence of permission.
 Different permissions can be set for each category.
Relative Permissions
 chmod category operation permissions filename
 User category:
u – User
g – Group
o – others
a – all
 Chmod operator
+ Adds the designated permission(s) to a file or directory.
- Removes the designated permission(s) from a file or
directory.
= Sets the designated permission(s).
Example
$ls -l testfile
-rwxrwxr-- 1 amrood users 1024 Nov 2 00:10 testfile
$chmod o+wx testfile
$ls -l testfile
-rwxrwxrwx 1 amrood users 1024 Nov 2 00:10 testfile
$chmod u-x testfile
$ls -l testfile
-rw-rwxrwx 1 amrood users 1024 Nov 2 00:10 testfile
$chmod g=rx testfile
$ls -l testfile
-rw-r-xrwx 1 amrood users 1024 Nov 2 00:10 testfile
Absolute Permissions
Absolute mode:
We use octal (base eight) values represented like this:
Letter Permission Value
R read 4
W write 2
X execute 1
- none 0
For each column, User, Group or Other you can set values
from 0 to 7. Here is what each means:
0= --- 1= --x
2= -w- 3= -wx
4= r-- 5= r-x
6= rw- 7= rwx
Absolute Permissions
Example index.html file with typical permission values:
$ chmod 755 index.html
$ ls -l index.html
-rwxr-xr-x 1 root wheel 0 May 24 06:20 index.html

$ chmod 644 index.html


$ ls -l index.html
-rw-r--r-- 1 root wheel 0 May 24 06:20 index.html

$ chmod -R 777 /var


This will give permissions to all files and subdirectories
recursively
Directory Permissions
 read determines if a user can view the directory's
contents, i.e. do ls in it.
 write determines if a user can create new files or delete
file in the directory.
 execute determines if the user can cd into the directory.

Potrebbero piacerti anche