2. The Linux / UNIX file-system
• The Linux file-system is hierarchical and is made of
directories, sub-directories and files.
• Directories can contain sub-directories and/or files; this is a
structure used by other file-systems, such as Microsoft-based
ones however the concept originated in UNIX.
• In Linux/Unix, everything is represented as a file; this includes
processes, devices, applications, I/O sockets, etc.
• Directories are a file as well, they contain the information of
any files directly under them, hierarchically.
3. Linux file-system Structure
• The Linux file-system structure is “tree” like.
• The file-system begins at a directory named “/”, which is also
referred to as the “root” directory.
• The “drives” representation is different than in Windows.
There are no C: or D: drives; each drive has a Mount Point,
which is a location under the root (/) directory in which it is
represented.
• Mount points can be created by the sys-admin that serve as
“connection” points of sort to physical devices and/or other
file-systems
Most Unix systems has the default /mnt directory for
arbitrary mounts. Linux systems, has the /media as an
additional default directory for removable storage devices
5. Absolute Directory Paths
• Absolute: the root of Linux’s file-system is represented as “/”;
this slash mark will always be present when we use absolute
pathnames to navigate the file-system, for example:
/var/log/messages.log
• The first / in the example above represents the root dir, “var”
is a directory sitting directly under the root dir and “log” is a
directory under “var”. Finally, “messages.log” is the name of a
file which resides in /var/log/.
6. Relative Directory Paths
• Relative: relative pathnames refers to the current location in
the file-system as the point of origin and not the root
directory. Let’s assume we are in the /var/ directory right
now, in order to reach messages.log file we will use the
following path: log/messages.log
• Note that there is no / at the beginning of the pathname and
that it begins with the “log” directory, since we are already
inside /var/
7. Home Directories
• Every Linux user has a “home” directory; the home dirs reside
in /home/<username>
• The home dir has a few uses and advantages to its owner:
It is the directory a user goes to after logging into the system.
It becomes the current working directory after login.
The owning user can freely create files and/or directories in it.
Other users do not have permissions to access a home directory not
owned by themselves, with the exclusion of the user “root” which is
the administrative user of the system or other users that have been
granted special permissions by the admin.
The home dir contains customization files which are loaded upon
login. (.bash_profile / .bashrc and others)
Contains the history of the shell commands executed by the specific
user (.bash_history)
8. Navigating through directories
• There are two commands that aid us with navigating through
the file-system:
PWD – Print Working Directory; displays the absolute pathname of
the current working directory:
# pwd
/home/nir
CD – change directory; make the specified pathname our current
working directory. can be used with either absolute or relative
pathnames:
# cd /var/log
# pwd
/var/log
9. Navigating through directories
CD with a relative pathname:
# pwd
/var
# cd log
# pwd
/var/log
CD without any pathname will make the user’s home dir the
current working directory:
# pwd
/var/log
# cd
# pwd
/home/nir
10. Navigating through directories
• Pathname navigations has a few shortcuts to make things
simpler:
. : represents the current working directory, for example: if the
current working dir is /var/log/ “ls .” will display the log files in this dir.
.. : represents the parent directory, one level above the current
working directory; following the above example, “ls ..” will display the
contents of /var/
~ : represents the home directory, each user and their own home.
example: “cd ~” will take the user “nir” into /home/nir/
- : return to the previous working directory; “cd –” will return to the
working directory we were in before the last “cd” command we’ve
performed.
11. Listing directory contents
• The “ls” command is used to list the contents of directories. It
has numerous options that allow the displaying and sorting of
information in a few different ways.
• “ls” command syntax is as follows:
ls [-option] [pathname[s]]
• If no options or arguments are given, “ls” by itself will list the
current working directory’s contents, for example:
# ls
file1 file2 file3
12. Listing directory contents
• “ls” has a few additional options to control the listing results
and sorting:
-a : display hidden files
-i : display inode numbers
-R : list sub-directories’ contents recursively
-d : list the directory details, not the files inside of it
-l : display long list
-t : sort by modification time
-r : sort in reverse order
There are numerous additional options, run: ls --help to view them.
13. Displaying long listing
• To see detailed information about the contents of a directory
use the “ls -l” command.
• The various fields of this table contains most of the
information about a file, its permissions and times:
# ls -l
drwxrwxr-x 2 nir nir 4096 Jul 19 13:58 directory
-rw-rw-r-- 1 nir nir 135 Jul 19 13:42 file1
-rwxrwxr-- 1 nir nir 35 Jul 19 13:42 file2
-rw-rw-r-- 1 nir nir 200 Jul 19 13:42 file3
14. Metacharacters
• Metacharacters are characters which are interpreted by the
shell as more than just any regular character.
• These characters include: ! ; * | % $ ? <> []
• It is highly recommended to Avoid using metacharacters
when naming files and/or directories; it will work but is also
likely to cause trouble.
• When executing a command in the shell, the shell scans the
whole command for metacharacters; if any are found, the
shell “expands” these characters to their actual meaning
before the execution of the command, for example, when
running the command: “ls –la ~” the shell will first interpret
the ~ mark into its actual meaning, which is the current user’s
home dir and then execute: “ls –la /home/<username>”.
15. Metacharacters
• The Asterisk “*” character’s special meaning is: zero or more
characters; this character is also known as the “Wildcard”
character, example:
# ls
dfile1 dfile2 directory file1 file2 file3 kfile9 mfile1
# ls k*
kfile9
• Filenames with a leading dot ( . ), such as “.bash_profile” are
categorized as “hidden” by the file-system
16. Metacharacters
• The question mark’s “?” special meaning is: match any single
character (except a leading dot in hidden files). example:
# ls
afile1 afile2 afile123 afile directory file1 file2 file3 kfile9 mfile1
# ls afile?
afile1 afile2
17. Metacharacters
• The square brackets “[]” special meaning is: match a set or a
range of characters in a single position.
example:
# ls
dfile1 dfile2 directory file1 file2 file3 kfile9 mfile1
# ls [mk]*
kfile9 mfile1
18. Basic file management
• ‘cp’ copy a file to a new file or a list of files to a directory
• Syntax:
cp [options] file(s) file|directory
• Options:
-i run interactively, ask before overwriting files
-f force copy, overwrite automatically
-r recursively copy files and sub-directories
$ cp file file_new
$ ls -l file file_new
-rw-r--r-- 1 root root 4 Jul 23 21:02 file
-rw-r--r-- 1 root root 4 Jul 23 22:11 file_new
19. Basic file management
• ‘mv’ move or rename a file or move a list of files to a
directory
• Syntax:
mv [options] file(s) file|directory
• Options:
-i run interactively, ask before overwriting files
-f force copy, overwrite automatically
$ mv file file_new
$ ls -l file file_new
ls: file: No such file or directory
-rw-r--r-- 1 root root 4 Jul 23 22:11 file_new
20. Basic file management
• ‘ln’ creates a new link for a file
• Syntax:
ln [options] file link
• Options:
-s create a symbolic link
-f force linking, overwrite automatically
$ ln -s file file_new
$ ls -l file file_new
-rw-r--r-- 1 root root 5 Jul 23 22:25 file
lrwxrwxrwx 1 root root 4 Jul 23 22:26 file_new -> file
21. Basic file management
• ‘rm’ removes a file or files list
• Syntax:
rm [options] file(s)
• Options:
-r recursively remove files and sub-directories
-f force copy, overwrite automatically
$ rm file_new
$ ls -l file file_new
ls: file: No such file or directory
ls: file_new: No such file or directory
22. Basic file management
• ‘mkdir’ create a new directory
• Syntax:
mkdir [options] directory
• Options:
-p create parent directories, if needed
$ mkdir dir1
$ ls -l
drwxr-xr-x 2 root root 4096 Jul 23 22:20 dir1
Note: ‘mkdir’ is the only command from the above that can create a new
directory. ‘cp’ and ‘mv’ will only copy existing directories with given ‘-r’
Editor's Notes
Make sure everyone take notes (!)
Explain about each one - (!)
Discussion: Explain about hidden files
Explain about MV and why it has no ‘-r’
Explain briefly about non-symbolic links
Explain about ‘ rm -rf ‘ ls: file: No such file or directory