Module 2
Files and Directories
Table of Content
In this section, we will learn:
❑ Linux File System
❑ Relative Path and Full Path
❑ Navigating the File System
❑ Symbolic Link and Hard Link
Linux File System
In Linux, everything is a file; if not, it is a process.
There are 7 types of files associated with Linux.
Examples:
❑ Regular Files
❑ Directory
❑ Block Files
❑ Links
Linux File System
File System Tree
❑ Files and directories are organized into a single-
rooted inverted tree structure.
❑ The file system begins at the root directory,
represented by a lone forward slash character “/”.
❑ Names are case-sensitive.
❑ Hidden files start with a “..”
❑ Paths are delimited by “/”.
$ cat /home/yahia/myfile.txt
Linux File System
File System Tree
Root
/
/bin/ /boot/ /dev/ /etc/ /home/ /lib/ /media/ /mnt/
/opt/ /root/ /sbin/ /srv/ /tmp/ /usr/ /var/
/bin/ /include/ /lib/ /sbin/ /cache/ /log/ /spool/ /tmp/
Relative Path and Full Path
Navigating the File System
❑ Essential Navigation Commands:
pwd: is used to print the current directory.
ls [-l]: is used to list files.
cd: is used to change directories..
Relative Path vs. Full Path
Navigating the File System
Two Types of Paths
❑ Absolute:
The absolute path is the full path to a directory or file; begins with /.
❑ Relative:
The relative path is a partial path that is relative to the current working directory; does not begin with /.
/
Root Directory
/
bin etc users tmp usr
users
/users/john john carol
john
work work play
Relative Path vs. Full Path
Absolute Path:
❑ The absolute path is a path that contains the root directory and all other subdirectories you need to
access a file.
❑ It points to the same location in the directory tree, regardless of the current working directory.
Example:
/users/john/work/linux.txt
Starting
With “/”
Relative Path vs. Full Path
Relative Path:
❑ The relative path is a partial path to a file, in relation to the current working directory.
If inside of the home directory in the previous directory example, a relative path would be:
work/linux.txt
Not Starting With “/”
Navigating the File System
Listing Directory Contents:
ls -: lists the contents of the current directory or a specified directory.
Usage:
❍ ls [options] [files_or_dirs]
Example:
❍ ls –a: includes hidden files.
❍ ls –l: displays extra information.
❍ ls –R: lists files and directories recursively.
❍ ls –ld: displays directory and symlink information.
Navigating the File System
Copying Files and Directories
cp -: copies files and directories.
Usage:
❍ cp [options] file destination
If the destination is a directory, more than one file may be
copied at a time.
❍ cp [options] file1 file2 dest
• If the destination is a directory, the copy is placed there.
• If the destination is a file, the copy overwrites the destination.
• If the destination does not exist, the copy is renamed.
Navigating the File System
Creating and Removing Files
• touch -: creates empty files or updates file timestamps.
• rm -: removes files.
Usage:
rm [options] <file>...
Example:
❍ rm –i: File (Interactive)
❍ rm –r: Directory (Recursive)
❍ rm –f: File (Force)
Navigating the File System
Moving and Renaming Files and Directories
mv -: moves and/or renames files and directories.
Usage:
mv [options] file destination
If the destination is a directory, more than one file
may be copied at a time.
mv [options] file1 file2 destination
The destination works like the “cp” command.
Navigating the File System
Creating and Removing Directories
● mkdir: creates directories.
● rmdir: removes empty directories.
● rm –r: recursively removes directory trees.
Navigating the File System
Examples:
❑ cd /usr/local/lib: changes directory to /usr/local/lib.
❑ cd ~: changes to the home directory (could also just type ‘cd’).
❑ pwd: prints the current working directory.
❑ cd ..: moves up one directory level.
❑ cd /: changes to the root directory.
❑ ls –d pro*: lists only the directories starting with "pro".
❑ ls –a /home/yahia: lists all files including hidden files in /home/yahia.
Symbolic Link and Hard Link
What Is Inode Number?
“
An inode number is an entry in inode table that contains information about a file and directory.
”
Links in Linux
Original Soft Original Hard
File Link File Link
Data on Disk Data on Disk
Soft Link Hard Link
Links in Linux
Creating Links
❑ ln: creates hard links.
❑ ln -s: creates soft links.
❑ ls–li: displays inode number.
Links in Linux
Soft Links vs. Hard Links