File System in Unix

advertisement
Files and Directories in UNIX

The first file in UNIX file system is
“root” or “/”
/ (ROOT)
...
Alice
abuzneid
sbenayed
ali
mahmood
UNIX
Documents
a.doc
c.doc
mail
inbox
sendmail
personal
p1
p2
Memos
Files and Directories in UNIX
(continue.)



Home directory (could be named differently) has
subdirectory per user called “User Home
Directory”
Directories can have more subdirectory and files
A file or a directory can be referred to by



Relative path name [a.doc if you are at Documents]
Absolute path name
[/home/abuzneid/UNIX/Documents/a.doc}\]
File and directory names are case sensitive
Files and Directories in UNIX
(continue.)

To display working directory
$ pwd
/home/abuzneid/UNIX
$
 cd command to change directory
$ ls -l
total 8
drwxr-xr-x
2
abuzneid
534
512
Oct
8
13:40
Documents
drwxr-xr-x
2
abuzneid
534
512
Oct
8
13:43
mail
drwxr-xr-x
2
abuzneid
534
512
Oct
8
13:53
memos
drwxr-xr-x
2
abuzneid
534
512
Oct
8
13:53
personal
$ cd Documents
$ pwd
/home/abuzneid/UNIX/Documents
$
Files and Directories in UNIX
(continue.)
$ pwd
/home/abuzneid/UNIX
$ ls -l
total 8
drwxr-xr-x
2
abuzneid
534
512
Oct
8
13:40
Documents
drwxr-xr-x
2
abuzneid
534
512
Oct
8
13:43
mail
drwxr-xr-x
2
abuzneid
534
512
Oct
8
13:53
memos
drwxr-xr-x
2
abuzneid
534
512
Oct
8
13:53
personal
$ cd Documents
$ pwd
/home/abuzneid/UNIX/Documents
$
cd ..
$ pwd
/home/abuzneid/UNIX
$
Files and Directories in UNIX
(continue.)
$ cd /
$ pwd
/
$ cd /home/abuzneid/UNIX/Documents
$ pwd
/home/abuzneid/UNIX/Documents
$ cd ../..
$ pwd
/home/abuzneid
$ cd
$ pwd
/home/abuzneid
$
List Files


ls command is used to list files and directories
ls –l displays more information about every file
and directory

file type









d for directory
- for file
b, c, p for special file
access made for the owner, group and others
number of links
owner of the file
Size
last modification
file name
List Files (continue.)
$ pwd
/home/abuzneid/UNIX/Documents
$ ls
a.doc
c.doc
$ ls -l
total 4
-rw-r--r--
1
abuzneid
534
14
Oct
8
13:37
a.doc
-rw-r--r--
1
abuzneid
534
14
Oct
8
13:38
c.doc
$ cd
$ pwd
/home/abuneid
$ ls UNIX
Documents
$
mail
memos
personal
List Files (continue.)
$ ls -a
.
$
..
Documents
mail Memos
personal
Creating a Directory: the mkdir command

mkdir command
$ pwd
/home/abuzneid/UNIX
$ mkdir test
$ ls
Documents
mail
Memos
$ cd test
$ pwd
/home/abuzneid/UNIX/test
$
personal
test
Copying file from directory to another
$
cp UNIX/personal/p1 UNIX/Documents/p11
$ ls UNIX/Documents
a.doc
c.doc
p11
$ cp UNIX/personal/p1 UNIX/Documents
$ ls UNIX/Documents
a.doc
c.doc
p1
p11
$ cd UNIX/personal
$ pwd
/home/abuzneid/UNIX/personal
$ ls
p1
p2
$ cp p1 p2 /home/abuzneid/UNIX/mail
$
Moving Files between Directories
$ cd /home/abuzneid/UNIX/personal
$ ls -l
total 4
-rw-r--r--
1 abuzneid
534
11 Oct 10 21:46 p1
-rw-r--r--
1 abuzneid
534
11 Oct 10 21:49 p2
$
mv p1 p2 /home/abuzneid/UNIX/Memos
$ cd /home/abuzneid/UNIX/Memos
$ ls
p1
$
p2
Rename File or Directory

mv is used to rename a file of a
directory
$ ls -l
total 14
drwxr-xr-x
2 abuzneid 534
512 Oct 10 21:47 Documents
drwxr-xr-x
2 abuzneid 534
512 Oct
drwxr-xr-x
2 abuzneid 534
512 Oct 10 21:54 Memos
drwxr-xr-x
2 abuzneid 534
512 Oct
drwxr-xr-x
2 abuzneid 534
512 Oct 10 21:54 personal
drwxr-xr-x
2 abuzneid 534
512 Oct
drwxr-xr-x
2 abuzneid 534
512 Oct 10 21:39 test
$ mv Memos memos
$
8 13:43 mail
8 23:42 memos
8 23:42 TEST
Rename File or Directory (continue.)
$ ls -l
total 12
drwxr-xr-x
2 abuzneid 534
512 Oct 10 21:47 Documents
drwxr-xr-x
2 abuzneid 534
512 Oct
drwxr-xr-x
3 abuzneid 534
512 Oct 10 21:57 memos
drwxr-xr-x
2 abuzneid 534
512 Oct 10 21:54 personal
drwxr-xr-x
2 abuzneid 534
512 Oct
drwxr-xr-x
2 abuzneid 534
512 Oct 10 21:39 test
$
8 13:43 mail
8 23:42 TEST
Memos
renamed to
memos
Removing a directory: the rmdir command

To remove a directory:


rmdir <directory name> if the directory is
empty
rm –r <directory name> if it’s not empty
Removing a directory: the rmdir command
(continue.)
$ rmdir /home/abuzneid/UNIX/memos
rmdir: directory "/home/abuzneid/UNIX/memos": Directory not empty
ALL SUBDIRECTORIES AND FILES IN memos MUST BE DELETED FIRST
$ cd /home/abuzneid/UNIX/memos
$ ls -l
total 4
-rw-r--r--
1 abuzneid
534
11 Oct
8 23:23 p1
-rw-r--r--
1 abuzneid
534
11 Oct
8 23:29 p2
$ rm *
$ ls
$ cd ..
$ rmdir memos
$ ls -l
total 10
drwxr-xr-x
2 abuzneid
534
512 Oct 10 21:47 Documents
drwxr-xr-x
2 abuzneid
534
512 Oct
drwxr-xr-x
2 abuzneid
534
512 Oct 10 21:54 personal
drwxr-xr-x
2 abuzneid
534
512 Oct
drwxr-xr-x
2 abuzneid
534
512 Oct 10 21:39 test
$
8 13:43 mail
8 23:42 TEST
Linking Files: the ln command

To have two copies of any file, you can use:



The disadvantages of cp





cp to copy it to another file, or
ln to link the file to another file
Consumes twice as much disk space
Both files always has to be changed if any of them is
updated to keep them identical
Number of links to a file is, normally 1 for nonlinked, non-directory files
More than one link to a file is possible
Most often, ln is used to link files between
directories
Linking Files: the ln command
(continue.)

There is two types of links
Hard disk
ln <form to>
“to” can be file or directory
Soft link
ln <–s from to>
“to” can be file or directory
“to” and “from” must resides
on the same file system
“to” and “from” may resides
on different file system
“to” and “from” will have the
same size
If “to” is deleted “from” will
work fine and vice versa
“to” will have a size of link
(pointer) to “from”
If “from” is deleted, “to” will
not work
Linking Files: the ln command
(continue.)
$ cat aaa
Bridgeport
Massashusets
New Hampshire
Vermont
$ ls -l
total 12
-rw-r--r--
1 abuzneid 534
-rw-r--r--
1 abuzneid 534
drwxr-xr-x
2 abuzneid 534
512 Oct 10 21:47 Documents
drwxr-xr-x
2 abuzneid 534
512 Oct
drwxr-xr-x
2 abuzneid 534
512 Oct 10 21:54 personal
drwxr-xr-x
2 abuzneid 534
512 Oct
drwxr-xr-x
2 abuzneid 534
512 Oct 10 21:39 test
$
46 Oct 10 23:14 aaa
34 Oct 10 23:12 bbb
8 13:43 mail
8 23:42 TEST
Linking Files: the ln command
(continue.)
$ ln aaa bbb
$ ls -l
total 14
-rw-r--r--
2 abuzneid
-rw-r--r--
2 abuzneid 534
drwxr-xr-x
2 abuzneid 534
512 Oct 10 21:47 Documents
drwxr-xr-x
2 abuzneid 534
512 Oct
drwxr-xr-x
2 abuzneid 534
512 Oct 10 21:54 personal
drwxr-xr-x
2 abuzneid 534
512 Oct
drwxr-xr-x
2 abuzneid 534
512 Oct 10 21:39 test
$ cat bbb
Bridgeport
Massashusets
New Hampshire
Vermont
$
534
46 Oct 10 23:14 aaa
46 Oct 10 23:14 bbb
8 13:43 mail
8 23:42 TEST
Linking Files: the ln command (continue.)
$ echo one more line >> aaa
$ cat aaa
Bridgeport
Massashusets
New Hampshire
Vermont
one more line
$ cat bbb
Bridgeport
Massashusets
New Hampshire
Vermont
one more line
$ rm aaa
$ cat bbb
Bridgeport
Massashusets
New Hampshire
Vermont
one more line
$
Linking Files: the ln command (continue.)
$ cp bbb ccc
$ ln -s ccc ddd
$ ls -l
total 16
-rw-r--r--
1 abuzneid 534
60 Oct 10 23:20 bbb
-rw-r--r--
1 abuzneid 534
60 Oct 10 23:25 ccc
lrwxrwxrwx
1 abuzneid 534
drwxr-xr-x
2 abuzneid 534
512 Oct 10 21:47 Documents
drwxr-xr-x
2 abuzneid 534
512 Oct
drwxr-xr-x
2 abuzneid 534
512 Oct 10 21:54 personal
drwxr-xr-x
2 abuzneid 534
512 Oct
drwxr-xr-x
2 abuzneid 534
512 Oct 10 21:39 test
$ rm ccc
$ cat ddd
cat: cannot open ddd
$
3 Oct 10 23:26 ddd -> ccc
8 13:43 mail
8 23:42 TEST
File Name Substitution
•The asterisk ”*”: substitutes zero or more characters
$ ls
bbb
test
ddd
Documents
mail
$ echo *
Documents TEST bbb ddd mail personal test
$ echo *.*
*.*
$ echo a*
a*
$ echo m*
mail
$ echo *a*
mail personal
$
personal
TEST
File Name Substitution (continue.)

“?” matches single character
$ ls
bbb
TEST
ddd
test
$ echo ???
bbb ddd
$
Documents
mail
personal
File Name Substitution (continue.)




Matching a single character using square
brackets []
[abc] matches one letter
[!a-z] matches any character except a lower
case letter
*[!X] matches any file that doesn't end with
the upper case X
Standard Input/Output


Standard input is the terminal (keyboard)
Standard output is the terminal (display)
standard input
command
standard output
Standard Input/Output (continue.)
who

skan
wiem
samir
tty01 Jan 07 07:56
tty52 Jan 07 06:15
tty03 Jan 07 09:26
If a sort command is executed without a file
name argument, then the command will take
its input from standard input
Standard Input/Output (continue.)

Example:
$ sort
nouha
mahdi
malek
issam
salwa
issam
mahdi
malek
nouha
salwa
$
Standard Input/Output (continue.)
skan
wiem
samir
sort
skan
wiem
samir
Output Redirection

Output directed to a file instead of standard
output



command > ofile directs output to the file ofile
command >> ofile appends output to the file ofile
> ofile
creates an empty file “ofile”
Output Redirection (continue.)

Example:
$ who > users
$ cat users
bgeorge
pts/16
Oct
5 15:01
(216.87.102.204)
abakshi
pts/7
Oct 10 22:56
(216.87.102.210)
abuzneid
pts/9
Oct 10 19:29
(avicenna.102.87.216.in-addr.arpa)
xiafeng
pts/10
Oct 10 23:16
(Shiva-RAS-Pool-13.uhmc.sunysb.edu)
$ echo add one more line >> users
$ cat users
bgeorge
pts/16
Oct
5 15:01
(216.87.102.204)
abakshi
pts/7
Oct 10 22:56
(216.87.102.210)
abuzneid
pts/9
Oct 10 19:29
(avicenna.102.87.216.in-addr.arpa)
xiafeng
pts/20
Oct 10 22:11
(Shiva-RAS-Pool-53.uhmc.sunysb.edu)
xiafeng
pts/21
Oct 10 22:15
(Shiva-RAS-Pool-55.uhmc.sunysb.edu)
xiafeng
pts/10
Oct 10 23:16
(Shiva-RAS-Pool-13.uhmc.sunysb.edu)
add one more line
$ > users
$ cat users
$
Input Redirection


Input of a file is redirected from a file
 command < infile
get the input from the file infile
Example:
$ who > users
$ wc -l users
15 users
$ wc -l < users
15
$
Pipes


Connects the output of one command to the input of
another command
Example:
$ who | wc -l
14
$ ls | wc -l
8
$
Pipes (continue.)
who
wc -l
5
Filters


Any program that can take input from
standard input, perform some operation on
that input, and write the results to standard
output
Example: cat and sort are filters
Standard Errors


Terminal is the standard error
In most cases, you never know the
difference between standard output and
standard error

command 2> efile directs the error to the file
efile
Standard Errors (continue.)

Examples:
$ ls n*
n*: No such file or directory
$ ls n* > foo
n*: No such file or directory
$ ls 2> foo
bbb
Documents
mail
TEST
ddd
foo
personal
test
$ cat foo
users
References



UNIX SHELLS BY EXAMPLE BY ELLIE
QUIGLEY
UNIX FOR PROGRAMMERS AND USERS
BY G. GLASS AND K ABLES
UNIX SHELL PROGRAMMING BY S.
KOCHAN AND P. WOOD
Download