0% found this document useful (0 votes)
48 views

The Linux File System

The document discusses Linux file systems and directories. It provides 3 key points: 1) In Linux, data is stored in files which are contained within directories on disks. Directories can contain both files and other directories, creating a hierarchy. 2) There are different types of directories, including the root directory (/), home directories, and the current working directory (.). Paths can be used to locate files and directories either relatively or absolutely. 3) Commands like ls, pwd, and mkdir can be used to view and manipulate directories and their contents. Wildcards and file name expansion allow selecting files based on patterns.

Uploaded by

Trương Hoàng
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

The Linux File System

The document discusses Linux file systems and directories. It provides 3 key points: 1) In Linux, data is stored in files which are contained within directories on disks. Directories can contain both files and other directories, creating a hierarchy. 2) There are different types of directories, including the root directory (/), home directories, and the current working directory (.). Paths can be used to locate files and directories either relatively or absolutely. 3) Commands like ls, pwd, and mkdir can be used to view and manipulate directories and their contents. Wildcards and file name expansion allow selecting files based on patterns.

Uploaded by

Trương Hoàng
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

05-Mar-19

File and Directory


• In linux and most of OS data are stored in files
• File
– Contains data
The Linux File System – Stored in (hard) disk
• Directory
– Contains files
– Stored in (hard) disk
– Makes easy for data organizing
• Hierarchy of directories and files = file system
• Single file system for all logical disks
1

1 2

Figure 3-3

A Directory Hierarchy Directory Types


• Root Directory: /
– The first directory in any UNIX file structure
– Always begin with the forward slash (/)
• Home Directory: $HOME or ~
• Created by system administrator
• This is where you are when you first log in!
• Under $HOME, you may create your own directory structure
• Type: cd [Return] takes you $HOME
• Current Working Directory: .
– The Directory you are currently working in
– Also called Current Working Directory (cwd)
• Parent Directory: ..
– The directory immediately above your current working directory.
3 4

3 4

Figure 3-4

Path Absolute Path for file3

Two ways of locating a file or a directory:


• By Using Absolute Path
– Full path
– Traces a path from root to a file or a directory
– Always begins with the root (/) directory!
– Example: /home/ux/krush/unix/assignments/assign1.sp04 Absolute Pathname:
• By Using Relative Path
– Traces a path from the ‘cwd’ to a file or a directory /usr/staff/joan/file3
– No initial forward slash (/)
– Two dots (..) goes up one level on file structure
– Dot (.) points to current working directory (cwd)
– Example: unix/assignments/assign1.sp04

5 6

5 6

1
05-Mar-19

File System
Figure 3-12

Relative Paths for file3 Directory Operations

7 8

Figure 3-14

Display Current Directory’s Full Pathname The ls Command


• To determine the full pathname of the current
working directory, use the command named “pwd”
• pwd stands for print working directory
Example: To display the full pathname of the current
working directory
ux% pwd
/home/ux/krush/unix

9 10

9 10

Figure 3-15

ls Long List Option


• ls
List the content of the current directory
• ls path_name
List the content of the directory in path_name.
• ls –l
Long list
• ls –a
List all hidden files
• ls -la
Combine two options –l and –a together
11 12

11 12

2
05-Mar-19

List Contents of a Specific Directory File Name Expansion & Wildcards


Listing contents of a subdirectory named
ux% ls -l unix/grades “unix/grades”
Allows you to select files that satisfy a particular
total 10 name pattern (wildcards)
-rwxr-xr-x 3 krush csci 72 Jan 19 19:12 330assign-graderun
-rwxr-xr-x 1 krush csci 70 Jan 19 19:13 330exam-graderun Character Description Example
-rwxr-xr-x 2 krush csci 70 Jan 19 19:12 330quiz-graderun
-r-x------ 1 krush csci 468 Feb 1 11:55 test-330grade * Match zero or more char. ls *.c
-r-x------ 1 krush csci 664 Feb 1 11:55 test-330grade,v ? Match any single character ls conf.?
[list] Match any single character in list ls conf.[co]
[lower-upper] Match any character in range ls lib-id[3-7].o
str{str1,str2,…} Expand str with contents of { } ls c*.{700,300}

13 14

13 14

Figure 3-17

The mkdir Command Directory Names


• Use the following characters:
– Uppercase letters (A-Z)
– Lowercase letters (a-z)
– Numbers (0-9)
– Underscore ( _ )
– Period/dot ( . )

15 16

15 16

Directory Names Example: Create a Directory Creation


• When naming a directory, avoid the following
characters: dev etc home usr
tty null skel mp ux bin local ucb
& * \ | [] {} z036473
You are here
csci330 .cshrc .logout Temp
$ <> () # ? /
Data

“ ‘ ; ^ ! ~ Create a directory called Data under csci330


a) Using Absolute Pathname:
mkdir /home/mp/z036473/csci330/Data
Space Tab
a) Using Relative Pathname:
mkdir csci330/Data
17 b) Make also missing parent directory, directory Data does not exist.18
mkdir -p csci330/Data/subData

17 18

3
05-Mar-19

Figure 3-18

The cd Command Changing Directory

dev etc home usr


tty null skel mp ux bin local ucb
z036473

csci330 .cshrc .logout Temp

You are here Data

In the Data directory, go to $HOME directory


a) Using Absolute Pathname:
cd /home/mp/z036473
a) Using Relative Pathname:
19 20
cd $home cd ../.. cd cd ~ cd ~z036473

19 20

File System

Remove Directories
Exercises
• To remove an empty directory – a directory that
does not contain user-created files, use the • In your home directory (example
command named “rmdir” /home/students/2017xxxx) create directory dir1 then dir2
Example: To remove a directory called “test”, inside dir1
which does not contain user-created files. • List the contents of dir1, dir2
ux% rmdir test • Move to dir2
• Getback to your home directory
• To remove a non-empty directory, use the command • Remove dir1 and dir2
named “rm –r”
Example: To remove a non-empty directory called
“old-data”
ux% rm –r old-data
21

21 22

Figure 3-22 Operations Common to Directories and


Regular Files Copying Files
• To copy a file, use the command named “cp”
• Syntax: cp source-file new-file
• Commonly used options:
-i if “new-file” exists, the command cp prompts for
confirmation before overwriting
-p preserve permissions and modification times
-r recursively copy files and subdirectories

23 24

23 24

4
05-Mar-19

Copying Files Moving Files


• “source-file” must have read permission. • To move files from one directory to another
• The directory that contains “source-file” must have directory, or to re-name a file, use the command
execute permission. named “mv”.
• The directory that contains “new-file” must have • The directory that contains the source file and the
write and execute permissions. destination directory must have write and execute
• Note that if “new-file” exists, you do not need the access permissions.
write permission to the directory that contains it,
but you must have the write permission to “new-
file”.

25 26

25 26

Figure 3-31

Moving Files Moving a File


• Syntax: mv source-file destination-file

Example: Move “assign1.txt” a different directory


and rename it to “assign1.save”
ux% mv assign1.txt ~/archive/assign1.save
ux% mv assign1.txt ~/archive

27 28

27 28

Figure 3-30

Rename Directories The mv Command


• To change the name of an existing directory, use
the command named “mv”
Example: To rename the file called “unix” to
“csci330”
ux% mv unix csci330

• For the above example, what happens if “csci330”


already exists in the current directory and it is the
name of a directory?

29 30

29 30

5
05-Mar-19

Removing/Deleting Files Removing/Deleting Files


• You should remove un-needed files to free up disk • If “file-list” contains pathname, the directory
space. components of the pathname must have execute
• To remove/delete files, use the command named permission.
“rm”. • The last directory that contains the file to be
• Syntax: rm file-list deleted must have execute and write permissions.
• Commonly used options:
-f force remove regardless of permissions for “file- Example: Remove the file named “old-assign”
list”
ux% rm unix/assign/old-assign
-i prompt for confirmation before removing
-r removes everything under the indicated directory

31 32

31 32

Recap: Common Operations on Files


Examples
$ ls -l
-rw-r--r-- 1 tuananh user1 16 Feb 10 19:12 test.txt
drwxr-xr-- 2 tuananh user1 512 Feb 10 19:14 mydir
$ cp test.txt mydir
$ ls -l mydir
-rw-r--r-- 1 tuananh user1 16 Feb 12 20:03 test.txt
$ rm –R mydir
$ ls -l
-rw-r--r-- 1 tuananh user1 16 Feb 10 19:12 test.txt
$ rm test.txt
$ ls -l
$

34

33 34

Finding Files Finding Files

• The command named “find” can be used to locate Example 1: Find all files, in your directory hierarchy,
a file or a directory. that have a name ending with “.bak”.
• Syntax: find pathname-list expression ux% find ~ “*.bak” –print
• “find” recursively descends through pathname-list
and applies expression to every file. Example 2: Find all files, in your directory hierarchy,
• Expression that were modified yesterday.
• -name file_name
ux% find ~ –mtime –1 -print
• -perm permission_mod
• -type d/f/...
• -size N: N is the minimum number of block (512B)
• -atime N, -mtime N, -ctime N, where is by defaut the number of
day. 35 36

35 36

6
05-Mar-19

The “ln” command


Example
• $find /usr -name toto
• Allows file to listed in multiple directories
• $find /usr -name " *.c »
• 2 types:
• $find / -mtime 3 – Hard link
– Symbolic link
• $find / -size 2000 -print
• All files with size more than 1 MB (= 2000 block 512 B)
• First: understand Unix file storage and organization

• $find / -type f -user olivier -perm 755

38

37 38

Figure 3-5

Unix file organization Blocks in a file system

• Computer has one or more physical hard drives


• Hard drive is divided into partitions

• Partition holds file system


– File system is set of data blocks
– Data blocks contain
• general information
• actual file data
• directory information

39 40

39 40

Figure 3-6

inode Inodes in a filesystem

• Index (or information) node: one inode per file


• Each inode has unique number
• contents:
– File type, access permissions, link count
– UID, GID
– Date and time of the file’s last
• Data access (read and execute)
• Data modification (written)
• I-node modification (permission change)
– Data blocks assigned to the file

41 42

41 42

7
05-Mar-19

File System

inode Contents: where is the file data ? Inode structure


I-node Structure
Inode may store:
– 10 addresses of data blocks that belong to file
– 1 address of a block that contains data block addresses I-node blocks blocks

– 1 address of a block that contains addresses of blocks Access, Links, and other
information

that contain data block addresses 1


2
– 1 address of a block that contains addresses of blocks blocks
. blocks

that contain addresses of blocks that contain data .


9 indirect
block addresses 10
block

11 double indirect
block blocks
12
blocks
13

blocks
43
triple indirect 42
block

43 44

File System

Directory representation Example structure


Directory is a file:
– Has inode like regular file, but different file type
.
I-node
– Data blocks of directory contains simple table: list 2763
Contents of dir1
2764
1076
. 2765

Name Inode number .. 2083 .


myfile 2764 .

Data blocks
on disk

44
45

45 46

File System

Example: user view vs. system view Output: ls -li

ux% ls -li crontab.cron


118282 -rw-r--r-- 1 krush csci 80 Feb 27 12:23 crontab.cron

I-node

47 46

47 48

8
05-Mar-19

Linking Files Hard Link

• To share a single file with multiple users, a link can Advantages Disadvantages
be used. Allow access to original file name
via the file name or the I-node Cannot link to a file in a different
• A link is: number file system
– A reference to a file stored elsewhere on the system. The original file continues to exist
as long as at least one directory
– A way to establish a connection to a file to be shared. contains its I-node Prevents owner from truly deleting
it, and it counts against his/her disk
• Two types: Checks for the existence of the quota
– Hard link original file
– Symbolic link (a.k.a. “soft link”)

49 50

49 50

Figure 3-32

Hard
Hard Link
Link The ln Command

home Syntax: ln shared-file link-name

z036473
From dir3, link to the file ‘aa’ in dir1
name it ‘bb’:
dir1 dir2
% ln /home/z036473/dir1/aa bb
aa dir3

bb

54
51 52

51 52

Figure 3-8 Figure 3-8

A Hard Link A Hard


Hard Link
Link

Contents of dir1
.
home 1076
. 2406
2083 2407
z036473 ..
2408
aa 2407
dir1 dir2 .

aa Contents of dir3 .
dir3
1070
.
bb
2050
..
bb 2407
57
53 54

53 54

9
05-Mar-19

Symbolic Link Symbolic Link


• A hard link may not be created for a file on a
Advantages Disadvantages different file system
Allow access to original file name Created without checking the • Use symbolic link
existence of the shared file
Can use either relative or absolute
• The linked files do not share the same I-node
path to access the original file Cannot access the shared file if its number
path has restricted permissions
Can cross partition and drives
Can be circular linked to another
Allows the creation of a link to a symbolic linked file
Syntax: ln –s shared-file link-name
directory

Also called source-file Also called target-file

55 56

55 56

Figure 3-10

Symbolic Links to Different File Systems


Examples
$ ls -l
-rw-r--r-- 1 tuananh user1 8 Feb 10 1:12 test.txt
$ ln test.txt link1
$ ln -s test.txt link2
$ ls -l link*
-rw-r--r-- 2 tuananh user1 16 Feb 10 1:12 link1
lrw-r--r-- 1 tuananh user1 16 Feb 10 1:13 link2->test.txt

57

57 58

User’s Disk Quota

• A disk quota is set for each user account


• The command: quota –v
displays the user’s disk usage and limits
• 2 kinds of limits:
– Soft limit: ex. 3MB
• Maybe exceeded for one week
• System will nag
– Hard limit: ex. 4MB
• Cannot be exceeded

59

59

10

You might also like