Module 03.1 - File System

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Navigating the Filesystem

Directory Structure
Directory Structure
● The root directory is the top level of the Linux directory structure.
● The slash (/) character is used to specify the root directory.
Directory Structure
● To view the contents of the root directory, use the ls command with the /
character as the argument:
Home Directory
● The home directory is a dedicated directory for a particular user where that user can store
the personal data.
● In Linux, the home directory is a directory named with the name of the user account and it
is a subdirectory of the /home directory.
● The user has the full control over the contents of the account home directory:
⚫ The user can create, modify, and delete files and directories in their home directory.

● Example: the home directory of the


ivanovn user is:
● /home/oa-adminhome1/ivanovn
Home Directory contd.

● The tilde ~ character is used to represent the home directory.

● The whoami command shows the name of the logged in user.


● The cd ~ command moves to the account home directory.
● And the pwd command prints the location of the current directory
⚫ ivanovn is the home directory in this example.
Current Directory
● The pwd (print name of current/working directory) command shows the user’s location
within the filesystem.

● The working directory is the current location of the user within the filesystem.

pwd [OPTION]...

ivanovn@atlas:~$ pwd
/home/oa-adminhome1/37/ivanovn
Changing Directories
● The cd (change directory) command is used to navigate the filesystem.

cd [options]... [dir]

● To move into the /home directory use the directory name as an argument to
the cd command and then use the pwd command to confirm the new location after
changing directories.

ivanovn@atlas:~$ cd /home
ivanovn@atlas:~$ pwd
/home
Changing Directories
● The cd command will take the user to the home directory if used without arguments.

ivanovn@atlas:/home$ cd
ivanovn@atlas:~$ pwd
/home/oa-adminhome1/37/ivanovn

● The command not found error message will be given if the user tries to change to a non
existing directory:

ivanovn@atlas:~$ cd blahblah
-bash: cd: blahblah: No such file or directory
Paths
Paths
● A path is a list of directories separated by the / character.
● There are two types of paths: absolute and relative.
● For example, /home/oa-adminhome1/37/ivanovn is an absolute path to the home
directory of the ivanovn user.
Absolute Paths
● Absolute path is used to specify the exact location of a directory regardless of what the
current directory is.
● Absolute path always starts at the root directory.
● Absolute path always begins with the / character.
● The path /home/oa-adminhome1/37/ivanovn is an absolute path.
⚫ Begin at the root / directory > then move into the home directory > then move to the oa-
adminhome1 directory > then move to the directory named 37, and then move to the ivanovn
directory

● Example of using an absolute path with the cd command:


ivanovn@atlas:/$ cd /home/oa-adminhome1/37/ivanovn
ivanovn@atlas:~$ pwd
/home/oa-adminhome1/37/ivanovn
Relative Paths

● A relative path is always relative to the current location in the filesystem.


● Relative paths start with the ./ followed by name of a directory; ./ can be omitted (as
shown in the screen shot)

ivanovn@atlas:/$ pwd
/
ivanovn@atlas:/$ cd home
ivanovn@atlas:/home$ pwd
/home
Relative Paths: the .. shortcut
● The .. characters represents the directory above the current directory (parent
directory).
● For example, to move from the ivanovn home directory to the directory named 37:

ivanovn@atlas:/home/oa-adminhome1/37/ivanovn$ cd ..
ivanovn@atlas:/home/oa-adminhome1/37$

● The following example will move from the directory named 37 to the root directory :

ivanovn@atlas:/home/oa-adminhome1/37$ cd ../../..
ivanovn@atlas:/$
Relative Path: the . shortcut
● The single period . character represents the current directory.

● The single period . shortcut is very useful in certain situations (examples will be given
later in the course).
● The next two examples produce the same result:

ivanovn@atlas:/$ cd home
ivanovn@atlas:/home$

ivanovn@atlas:/$ cd ./home
ivanovn@atlas:/home$
Listing Files in a Directory
Listing Files in a Directory
● The ls command is used to show the content of directories and to show the detailed
information about files and directories.
ls [OPTION]... [FILE]...
● When used with no options or arguments, the ls command lists the files in the current
directory:
ivanovn@atlas:~$ ls
Desktop Documents Downloads Music

● The ls command can also be used to list the contents of any directory. For this, use
the path to the directory as an argument to the ls command.
ivanovn@atlas:~$ ls /home
aquota.user lost+found oa-adminhome1 oa-adminhome2 oa-adminhome3 oa-homer1 oa-homer2
oa-homer3
ls - Listing Hidden Files
● A hidden file or directory is any file or directory that has a name that begins with a
dot . character.
● The ls command does not show the hidden files by default.
● The -a option is used display all files, including hidden files:

ivanovn@atlas:~$ ls -a
. .bashrc .selected_editor Downloads Public
.. .cache Desktop Music Templates
.bash_logout .profile
ls - Long List (details)
● Use the -l option to show the file/folder details (l for long listing).
● Example: a detailed listing of the /home directory:

ivanovn@atlas:~$ ls -l /home
total 3764
-rw------- 1 root root 3812352 May 4 23:21 aquota.user
drwx------ 2 root root 16384 Jan 3 2022 lost+found
drwxr-xr-x 25 root root 4096 May 15 10:49 oa-adminhome1
drwxr-xr-x 19 root root 4096 Apr 20 19:53 oa-adminhome2
...
ls - Long List (details)
● The following describes each of the fields of data in the output of the ls -l command:
File Type:
ivanovn@atlas:~$ ls -l /home
total 3764
-rw------- 1 root root 3812352 May 4 23:21 aquota.user
drwx------ 2 root root 16384 Jan 3 2022 lost+found

The first character on each line indicates the type. The types are:

Symbol File Type Description


d directory A file used to store other files.
- regular file Includes readable files, images files, binary files, and compressed files.
l symbolic link Points to another file.
s socket File that represents a network connection.
p pipe Allows for communication between processes.
b block file Used to communicate with hardware.
c character file Used to communicate with hardware.
ls - Long List (details)
○ Permissions:

ivanovn@atlas:~$ ls -l /home
total 3764
-rw------- 1 root root 3812352 May 4 23:21 aquota.user

■ The next nine characters demonstrate the permissions of the file.

■ Permissions indicate how certain users can access a file.

○ Hard Link Count:

ivanovn@atlas:~$ ls -l /home
total 3764
-rw------- 1 root root 3812352 May 4 23:21 aquota.user

■ This number indicates how many hard links point to this file.
ls - Long List (details)
○ User Owner:

■ Every file is owned by a user account. The user owner has the rights to set permissions on a file.

ivanovn@atlas:~$ ls -l /home
total 3764
-rw------- 1 root root 3812352 May 4 23:21 aquota.user

○ Group Owner:

■ Indicates which group owns the file. Group ownership gives all members of the chosen group
access to the file or directory according to the item’s group permission settings.

ivanovn@atlas:~$ ls -l /home
total 3764
-rw------- 1 root root 3812352 May 4 23:21 aquota.user
ls - Long List (details)
○ File Size:
ivanovn@atlas:~$ ls -l /var/log/syslog
-rw-r----- 1 syslog adm 1607582 May 18 16:25 /var/log/syslog

■ The size of files in bytes.

○ Timestamp:
ivanovn@atlas:~$ ls -l /var/log/syslog
-rw-r----- 1 syslog adm 1607582 May 18 16:25 /var/log/syslog

■ Indicates when the file was modified.

○ File Name:
ivanovn@atlas:~$ ls -l /var/log/syslog
-rw-r----- 1 syslog adm 1607582 May 18 16:25 /var/log/syslog

■ Gives the name of the file.


ls - Human Readable Sizes
● The -h option displays file sizes in bytes.
● For larger files, it is hard to read the size when it is shown in bytes.
● The -h option is used to show the size in a human readable format.
● For example:
⚫ K for kilobytes
⚫ M for megabytes
⚫ G for gigabytes, and etc.

ivanovn@atlas:~$ ls -l /var/log/syslog
-rw-r----- 1 syslog adm 1.6M May 18 16:29 /var/log/syslog
ls - Listing Directories
● The option -d is used to refer to the current directory, and not to the contents of the
current directory.
● Without the option -d, the ls command shows the contents of the directory:
ivanovn@atlas:~$ ls -l /home
total 3764
-rw------- 1 root root 3812352 May 4 23:21 aquota.user
drwx------ 2 root root 16384 Jan 3 2022 lost+found
drwxr-xr-x 25 root root 4096 May 15 10:49 oa-adminhome1
...

● With the -d option, the ls command shows the directory itself:


ivanovn@atlas:~$ ls -ld /home
drwxr-xr-x 9 root root 4096 Apr 18 14:29 /home
ls - Recursive Listing
● Recursive listing shows the contents of a directory as well as of all subdirectories in that
directory.
● The -R option is used to show the recursive listing of a directory:
ivanovn@atlas:~$ ls -R testdir
testdir:
dir1 dir2 file1.txt

testdir/dir1:
file2.txt

testdir/dir2:
file3.txt

● The above example shows the contents of the testdir and its subdirectories dir1 and dir2.
Using this option with directories that contain a lot of files and subdirectories will result in a very long list as
every file and directory will be shown.
ls - Sort a Listing
● The ls command, by default, sorts files (alphabetically) by names.
● Use the -S option (capital letter s) to sort files by size.
● Use the -t option (lower case letter t) to sort files based on the time files were
modified.
● Use the -r option to reverse the sort order.
ivanovn@atlas:~$ ls -lSh /var/log
total 26M
-rw-rw-r-- 1 root utmp 23M May 18 16:44 lastlog
-rw-rw-r-- 1 root utmp 1.8M May 18 16:48 wtmp
-rw-r----- 1 syslog adm 1.6M May 18 16:48 auth.log
-rw-r----- 1 syslog adm 1.6M May 18 16:48 syslog
-rw-rw---- 1 root utmp 1.5M May 18 15:39 btmp
-rw-r--r-- 1 root adm 53K May 4 23:23 dmesg
ls – show detailed modification time
● Use the --full-time option to display the complete timestamp (including hours,
minutes, seconds):

ivanovn@atlas:~$ ls -l --full-time /etc/passwd


-rw-r--r-- 1 root root 2018 2021-05-15 10:05:47.800000000 -0400 /etc/passwd

You might also like