Exploring Linux File
system
Some More Commands
• The man command
• Command Grouping
• Creating Files
• File Details Using ls -l -i -a
• The Linux File system
• Absolute and Relatives Path References
• Copying,Moving, Deleting Files
• Browsing the file content- More, less, tail, head,
• Sorting Files
• Finding Files
July 10, 2003 2
Serguei A. Mokhov,
mokhov@[Link]
The Pipe ( Inter Process Communication
Mechanism- IPC)
The | (pipe) Symbols
Pipe
Some times it might be important to combine the execution
of a set of commands in such away that the output of one
command becomes the input of another command .
This enables us to create command chaining which
simplify the would be a complicated task.
Such computation strategies are common in computing
environments.
{james@localhost ~]$ cat < mylist
July 10, 2003 3
Pipes
• What's a pipe?
– is a method called refered to as interprocess
communication (IPC) that enables to process to
communicate with each other in such a way that the output
of one command becomes the input of another method.
– in shells the '|' symbol used of IPC
– a set of "piped" commands is often called a pipeline
• Why it's useful?
– Because by combining simple OS utilities one can easily
solve more complex tasks
July 10, 2003 4
The man Command
• The man command (for manual or manual
pages) Opens the manual pages for a command.
• This is part of Linux Documentation which makes
it possible to learn how to any command
• Practically every command has a corresponding
manual pages.
• A man page gives the description of the
command, its author, its options and where
possible examples among other details.
July 10, 2003 Serguei A. Mokhov, 5
mokhov@[Link]
The man Command
How to use manual pages' Enter the man
command followed by the command itself.
• Examples
• % man ls
• % man date
July 10, 2003 6
Command Grouping
• Using several commands separated by a
Semicolon: “;”
• Often grouping acts as if it were a single
command, so an output of different commands
can be redirected to a file:
• % (date; cal; date) > [Link]
July 10, 2003 Serguei A. Mokhov, 7
mokhov@[Link]
Command Exercises
Command Questions
• Which command will you use to:
– Show you your current directory?
– List the content of the var directory?
– List the content of all top level directories?
– Shut down the computer now?
– Clear the current list of history command
– Execute the last command that start with mou
• Distinguish between the operators > and >> as used with
commands.
• How can you use the command echo to create a file.
July 10, 2003 8
Command Exercises
Command Questions
• You remember that you had issued a command mount
/data /backup/data
• You want to re-run the command again using a short cut.
How ?
• Some one tells of a command that can achieve a certain
task but you do not know how to run the command. What do
you as a Linux student?
• You want to get out the out put of the commands comm1,
comm2, and comm3 to the file [Link]. How can you do
this in the most effective way.
• Discuss ways in which you can use past commands more
fast.
July 10, 2003 9
The Linux Directory Structure
10
The Linux Directory Structure
• Linux Directory structure is a tree like structure
showing how files in Linux are organized.
• Look at directory structure as a tree that is upside
down with the root at the top and the branches
connecting to the leaves.
• In Linux everything is a file. Even a directory if a file
• The leaves are the files in Linux directory structure.
• The root is a directory where all other directories
are mounted.
11
The Linux Directory Structure
Directory: Used to organize other files into a logical
tree structure
• Absolute pathname: Pathname from the root
directory to a certain file or directory
• Root: The top level directory
– referred to using the / character
12
The Linux Directory Structure (continued)
Figure 4-1: The Windows filesystem structure
Figure 4-2: The Linux filesystem structure
13
The Linux Directory Structure (continued)
Top Level Directories
Root Directory
Sub directories
14
• Linux File System
Linux+ Guide to Linux Certification, 2e
Notice that the topmost directory in the structure
is the / directory, also called the
root directory.
Beneath the root directory are a series of sub-
directories.
Specifications for how these directories are to be
named are contained in the File system Hierarchy
Standard (FHS)
Linux+ Guide to Linux Certification, 2e
The Linux Directory Structure (continued)
Important Directories
1. / – Root
• Every single file and directory starts from the root directory.
• Only root user has write privilege under this directory.
• Please note that /root is root user’s home directory, which is not
same as /.
2. /bin – User Binaries
• Contains binary executables.
• Common linux commands you need to use in single-user modes are
located under this directory.
• Commands used by all the users of the system are located here.
• For example: ps, ls, ping, grep, cp.
17
The Linux Directory Structure (continued)
Important Directories
3. /sbin – System Binaries
• Just like /bin, /sbin also contains binary executables.
• But, the commands located under this directory are used
typically by system aministrator, for system maintenance
purpose.
• For example: iptables, reboot, fdisk, ifconfig, swapon
4. /etc – Configuration Files
• Contains configuration files required by all programs.
• This also contains startup and shutdown shell scripts used to
start/stop individual programs.
• For example: /etc/[Link], /etc/[Link]
18
The Linux Directory Structure (continued)
Important Directories
5. /dev – Device Files
• Contains device files.
• These include terminal devices, usb, or any device attached to
the system.
• For example: /dev/tty1, /dev/usbmon0
6. /proc – Process Information
• Contains information about system process.
• This is a pseudo filesystem contains information about running
process. For example: /proc/{pid} directory contains information
about the process with that particular pid.
• This is a virtual filesystem with text information about system
resources. For example:/proc/uptime
19
The Linux Directory Structure (continued)
Important Directories
7. /var – Variable Files
• var stands for variable files.
• Content of the files that are expected to grow can be
found under this directory.
• This includes — system log files (/var/log); packages and
database files (/var/lib); emails (/var/mail); print queues
(/var/spool); lock files (/var/lock); temp files needed across
reboots (/var/tmp);
20
The Linux Directory Structure (continued)
Important Directories
9. /usr – User Programs
• Contains binaries, libraries, documentation, and source-code for
second level programs.
• /usr/bin contains binary files for user programs. If you can’t find a
user binary under /bin, look under /usr/bin. For example: at, awk, cc,
less, scp
• /usr/sbin contains binary files for system administrators. If you can’t
find a system binary under /sbin, look under /usr/sbin. For example:
atd, cron, sshd, useradd, userdel
• /usr/lib contains libraries for /usr/bin and /usr/sbin
• /usr/local contains users programs that you install from source. For
example, when you install apache from source, it goes under
/usr/local/apache2
21
The Linux Directory Structure (continued)
Important Directories
10. /home – Home Directories
• Home directories for all users to store their personal files.
• For example: /home/john, /home/nikita
11. /boot – Boot Loader Files
• Contains boot loader related files.
• Kernel initrd, vmlinux, grub files are located under /boot
• For example: [Link]-2.6.32-24-generic, vmlinuz-2.6.32-24-generic
22
The Linux Directory Structure (continued)
Important Directories
12. /lib – System Libraries
• Contains library files that supports the binaries located
under /bin and /sbin
• Library filenames are either ld* or lib*.so.*
• For example: [Link], [Link].5.7
13. /opt – Optional add-on Applications
• opt stands for optional.
• Contains add-on applications from individual vendors.
• add-on applications should be installed under either /opt/
or /opt/ sub-directory.
23
The Linux Directory Structure (continued)
Important Directories
14. /mnt – Mount Directory
• Temporary mount directory where sysadmins can mount
filesystems.
15. /media – Removable Media Devices
• Temporary mount directory for removable devices.
• For examples, /media/cdrom for CD-ROM; /media/floppy
for floppy drives; /media/cdrecorder for CD writer
16. /srv – Service Data
• srv stands for service.
• Contains server specific services related data.
• For example, /srv/cvs contains CVS related data. 24
• Types of Files Used by Linux
With a Windows file system you basically have two
entry types in the file system: Directories & Files.
You can have normal files, hidden files, shortcut
files, word processing files, executable files, and so
on.
With Linux, however, there are a variety of different
file types used by the file system.
Linux+ Guide to Linux Certification, 2e
Linux File Types
Regular files: These files are similar to those used by
the file systems of other operating systems -
forexample, executable files, [Link] files,
images, text configuration files, etc.
Directories: Files used to contain pointers to other
files
Links : These files are pointers that point to other files
in the file system.
Linux+ Guide to Linux Certification, 2e
Linux File Types
FIFOs: FIFO stands for First In First Out. These
are special files used to move data from one
running process on the system to another. A FIFO
file is basically a queue where the first chunk of
data added to the queue is the first chunk of data
removed from the queue. Data can only move in
one direction through a FIFO.
Sockets: Sockets are similar to FIFOs in that they
are used to transfer information between sockets(
Networks). With a socket, however, data can
move bi-directionally.
Linux+ Guide to Linux Certification, 2e
Viewing Files and Directories: File Types
• Text files: Store information in a readable text
format
• Binary data files: Store information associated with
executable programs
• Executable program files
• Directory files
28
Viewing Files and Directories: File Types
(continued)
• Linked files: Associated with another file
• Special device files: Represent system devices
• Named pipes: Identify channel that passes
information between processes
• Socket files: Allow a process on another computer
to write to a local file
29
Working With Directories
Remember the following Concepts and Command
• /home directory:
• /root directory
• pwd
• cd (change Directory)
• ~ Home directory metacharacter:
• mkdir
• . and ..
30
2. Complete Common File System
Tasks
Navigating the file system
Managing files and directories
Running executable files
Searching the file system
Linux+ Guide to Linux Certification, 2e
The cd Command
The cd command is at the heart of file system
navigation in Linux
If you enter cd at the shell prompt without specifying a
path, it will automatically change directories to the
home directory of the currently logged-in user.
Linux+ Guide to Linux Certification, 2e
Working With Directories (continued)
Absolute and Relative Path Reference
• We need a way of specifying which file/directory
that we want to work with and or its location.
• Every file is located at a given location in the
directory location.
• We specify the location by using path references
• There are two way of doing so.
– Absolute Path Reference
– Relative Path Reference
33
Working With Directories (continued)
Absolute Path Reference
• An absolute file path tells the computer where to
locate the file starting from the root directory.
• It is a list of directory names starting from the root
directory "/" to the directory containing the file x and
then followed by the name of the file (x).
• The directories are separated by the "/".
• Example
/home/Abi/Scripts/[Link]
34
Working With Directories (continued)
Absolute Path Reference
• Your current working directory has no effect on an
absolute pathname, because an absolute
pathname always starts at the ROOT and doesn’t
depend on your current directory.
35
Working With Directories (continued)
Relative Path Reference
• An relative file path tells the computer how to find a
file starting from the current directory
• A relative path reference is a list of directory names
starting from the root directory the current working
directory to the directory containing the file x and
then followed by the name of the file (x) all
separated by the "/" symbol .
• Because relative pathnames start in the current
working directory, relative pathnames act as if the
current working directory were inserted in front of
the pathname. 36
Working With Directories (continued)
• Relative Path Reference
– Remember you directory structure
– The tree will look as below after you created the
directory Expenses and the sub-directories:-
root home boot bin
Michael James Joyce
Desktop Pictures Music Public Expenses
July 10, 2003
January February March April May
37
July 10, 2003
Understanding Absolute and Relative Paths
Exercise
Referring to the slide before,
• Log in as root and create the directory structure as
shown below.
• Michael, James and Joyce are users
• What will be the output of the command ls -l /home
• As root move to the directory May of user Michael.
• What is the effect of running the commands
– pwd
– Cd ../
– pwd
– Cd ../../../
– pwd
– Cd ../../
July 10, 2003 38
Understanding Absolute and Relative Paths
Exercise
Referring to the slide before,
• How many users are in the system
• Suppose you logged in as Michael
– What is the relative path to the file [Link] in the
May directory
– What is the absolute path to the file [Link] in the
May directory
– How do you move to the May directory using (I)
relative path and (II) absolute path
– Can you copy the expenses directory with its sub
directories to the directory Joyce?
– What is the procedure that you will need to follow to
undertake the above task
July 10, 2003 39
Understanding Absolute and Relative Paths
Exercise
Referring to the slide before,
• Suppose you logged in as the root
– Can you copy the expenses directory with its sub
directories to the home directories of all the users?
Using (I) relative path and (II) absolute path
references.
– You issue out the command ls -al /Michael. what will
be the output
– What will the command cd~ do
July 10, 2003 40
Filenames
• Filename: Identifier given to a file
• Filename extensions: Identifiers following a dot (.)
at end of filename
– Denote file type
– Most files on Linux do not necessarily have file
extensions
41
Filenames extension
Table 4-1: Common filename extensions
42
Files Based Commands
July 10, 2003 44
The ls Command
• List directory contents
• Has whole bunch of
options, see man ls for • % ls -F
details. – append “/” to dirs and “*”
• % ls to executables
– all files except those • % ls -l
starting with a “.”
– long format
• % ls -a • % ls -al
– all
• % ls -lt
• % ls -A – sort by modification time
– all without “.” and “..” (latest - earliest)
• % ls -ltr
– reverse
July 10, 2003 45
The cat Command
• Display and concatenate files.
• % cat
– Will read from STDIN and print to STDOT every line you
enter.
• % cat file1 [file2] ...
– Will concatenate all files in one and print them to STDOUT
• % cat > filename
– Will take whatever you type from STDIN and will put it into
the file filename
• To exit cat or cat > filename type Ctrl+D to
indicate EOF (End of File).
July 10, 2003 Serguei A. Mokhov, 46
mokhov@[Link]
touch
• By touching a file you either create it if it did not
exists (with 0 length).
• Or you update it’s last modification and access
times.
• There are options to override the default
behavior.
• % touch filename
• % ls filename
• % man touch
July 10, 2003 Serguei A. Mokhov, 47
mokhov@[Link]
Managing files and directories
Creating files and directories
Viewing file contents
Deleting files and directories
Copying and moving files and directories
Creating links
Linux+ Guide to Linux Certification, 2e
Creating Files and Directories
touch new_file
gedit new_file
vi new_file
echo ....... > new_file
mkdir new_directory
Linux+ Guide to Linux Certification, 2e
Viewing Text File Contents
Commands to use to view File content
– cat
– less
– head
– Tail
– sort
– more
Linux+ Guide to Linux Certification, 2e
cat
The cat filename command will display the specified
text file on screen.
This command doesn’t pause the output, so if you
use it to view a long file, you may need to append |
more to the command to pause the output a page a
time.
cat options-
Linux+ Guide to Linux Certification, 2e
less
The less filename command can also be used to
display the specified text file on screen, much like cat.
However, the less command automatically pauses a
long text file one page at time.
cat options-: Many Options
Linux+ Guide to Linux Certification, 2e
less
• less ("less is more") a bit more smart than the more
command
• to display contents of a file:
– % less filename
• To display line numbers:
– % less -N filename
• To display a prompt:
– % less -P"Press 'q' to quit" filename
• Combine the two:
– % less -NP"Blah-blah-blah" filename
• For more information:
– % man less
July 10, 2003 Serguei A. Mokhov, 53
mokhov@[Link]
head
The head filename command is used to display the
first couple of lines of a text file on the screen.
head options
Linux+ Guide to Linux Certification, 2e
tail
The tail filename command is used to display the last
couple of lines of a text file on screen.
The tail command is particularly useful when
displaying a log file on screen.
When viewing a log file, you probably only want to
see the end of the file.
The tail command also includes the –f option, which
is very useful. You can use this to monitor the file
specified in the command
Linux+ Guide to Linux Certification, 2e
Deleting Files and Directories
rmdir
rm
Linux+ Guide to Linux Certification, 2e
rmdir
This utility can be used to delete an existing
directory. To use it, simply enter rmdir directory_name
—for example,
rmdir MyFiles. Be aware, however, that rmdir
requires that the directory be empty before it will
delete it.
Linux+ Guide to Linux Certification, 2e
rm
The rm utility is a more powerful deletion utility that
can be used to delete either a file or a populated
directory. To delete a file, simply enter rm filename. To
delete a directory, enter rm –r directory_name.
Linux+ Guide to Linux Certification, 2e
rm
Be careful with rm! By default, it won’t prompt you to
confirm a deletion operation. It assumes that you
really meant to delete the file or directory. If you want
rm to prompt you before deleting a file or directory,
include the –i option.
Linux+ Guide to Linux Certification, 2e
rm
• Removes file(s) and/or directories.
• % rm file1 [file2] ...
• % rm -r dir1 [dir2] ...
• % rm -r file1 dir1 dir2 file4 ...
July 10, 2003 60
Copying and Moving Files and
Directories
cp
mv
Linux+ Guide to Linux Certification, 2e
cp
This utility is used to copy files or entire directory
structures from one location in the file system to
another. For example, to copy a file named
/tmp/[Link] to your home directory, you could
enter
cp /tmp/[Link] ~.
Linux+ Guide to Linux Certification, 2e
cp
• Copies files / directories.
• % cp [options] <source> <destination>
• % cp file1 file2
• % cp file1 [file2] … /directory
• Useful option: -i to prevent overwriting existing
files and prompt the user to confirm.
July 10, 2003 63
mv
The mv command is used much like cp. However, it
will copy the specified file to the new location in the
file system and then delete the original. For example,
to move a file named [Link] from /tmp to /var/log,
you would enter mv /tmp/[Link] /var/log
Linux+ Guide to Linux Certification, 2e
mv
The mv command is also used to rename files.
Simply enter mv followed by the file to be renamed
and then the new file name. For example, to rename
[Link] to [Link], you would enter
mv [Link] [Link].
Linux+ Guide to Linux Certification, 2e
mv
• Moves or renames files/directories.
• % mv <source> <destination>
– The <source> gets removed
• % mv file1 dir/
• % mv file1 file2
– rename
• % mv file1 file2 dir/
• % mv dir1 dir2
July 10, 2003 66
Creating Links
Two types of Links
Hard
Symbolic
Linux+ Guide to Linux Certification, 2e
Hard Link
A hard link is a file that points directly to the inode of
another file.
An inode stores basic information about a file in the
Linux file system, including its size, device, owner,
and permissions.
Because the two files use the same inode, you can’t
tell which file is the pointer and which is the pointee
after the hard link is created.
Linux+ Guide to Linux Certification, 2e
Symbolic Link
A symbolic link file also points to another file in the file
system.
However, a file that is a symbolic link has its own
inode.
Because the pointer file has its own inode, the pointer
and the pointee in the file system can be easily
identified.
For example, in the previous chapter, you saw that
the vi file is symbolic-linked to the vim file.
Linux+ Guide to Linux Certification, 2e
syntax
ln pointee_ file pointer_file
Using ln without any options creates a hard link
If you want to create a symbolic link, you use the –s
option
ln pointee_ file pointer_file
Linux+ Guide to Linux Certification, 2e
Searching the File System
Using find
Using locate
Using grep
Linux+ Guide to Linux Certification, 2e
Using find
The find utility is fantastic tool that you can use
to search the Linux file system.
To use find, enter at the shell prompt
find path –name “filename”
For example, suppose you wanted to find all of
the log files stored in your file system that have a
.log extension. You could enter
find / –name “*.log”
Linux+ Guide to Linux Certification, 2e
The find utility is flexible. You can also use the –user
“username” option to search for files owned by a
specific user, or use the
–size “size” option to search for files of a specified
size.
You can use a + sign before the size value to search
for files larger than the specified size, or a – sign
before the size value to search for files smaller than
the specified size. The find utility has many other
options
Linux+ Guide to Linux Certification, 2e
Using locate
The locate utility functions in much the same manner
as find.
However, it has one distinct advantage over find.
Whenever you execute a search with find, it manually
walks through each directory in the path you name in
the command looking for the specified files.
This process can take some time.
Linux+ Guide to Linux Certification, 2e
locate
Alternatively, the locate utility builds an index of the
files in the file system.
Then, when you execute a search, locate simply runs
a query of the index.
It doesn’t actually search the file system directly.
The result is that locate runs much faster than find in
most situations.
To use locate, you must first install the findutils-locate
package on your system.
Linux+ Guide to Linux Certification, 2e
With the index updated, you can search for files by
simply entering:
locate filename at the shell prompt.
For example, if you wanted to search for a file named
[Link], you could enter:
locate [Link]
Linux+ Guide to Linux Certification, 2e
Using grep
Linux also provides a utility called grep that you
can use to search for content within a file. Using
grep, you can search through a file for a
particular text string. To use grep, you would
enter grep search_text file. For example, let’s
suppose you want to want to search through
your /var/log/messages file for any log entries
related to the VNC service running on your
Linux system. You would enter
grep vnc /var/log/messages
Linux+ Guide to Linux Certification, 2e
The Sort Command
Commands
sort Sorts using the first field of each line.
-n Sorts considering the numeric value of the strings
-k3 Sorts using the third field of each line
-rnk3 Sorts in reverse order, using the numeric value of the third field
ln
• Symbolic link or a “shortcut” in M$ terminology.
• % ln –s <real-name> <fake-name>
July 10, 2003 79
The grep Command
• grep (global regular expression print) command:
Displays lines in a text file matching a given regexp
• grep requires two arguments
– Text to search for and files to search
80
grep
• Searches its input for a pattern.
• The pattern can be a simple substring or a complex
regular expression.
• If a line matches, it’s directed to STDOUT; otherwise,
it’s discarded.
• % echo “blah-foo” | grep blah
– Will print the matching line
• % echo “blah-foo” | grep zee
– Will not.
• grep XXX File show lines matching pattern XXX in File
July 10, 2003 81
Grep Based Command Exercises
Command Questions
• Refer to attached file for exercises on Pipe, Grep,
sort and file browsing commands
July 10, 2003 83
Grep Based Command Exercises
Command Questions
• Populate the list with more proteins and grains
• Enter the commands below and note the outputs
– Grep Veg Myshopping
July 10, 2003 84
File Based Command Exercises
Command Questions
• Explain what the following commands do
– ls -l |grep suid
– ls -l |grep vegtables
– List the content of the var directory?
– List the content of all top level directories?
– Shut down the computer now?
– Clear the current list of history command
– Execute the last command that start with mou
• Distinguish between the operators > and >> as used with
commands.
• How can you use the command echo to create a file.
July 10, 2003 85
File Based Command Exercises
Command Questions
• You remember that you had issued a command mount
/data /backup/data
• You want to re-run the command again using a short cut.
How ?
• Some one tells of a command that can achieve a certain
task but you do not know how to run the command. What do
you as a Linux student?
• You want to get out the out put of the commands comm1,
comm2, and comm3 to the file [Link]. How can you do
this in the most effective way.
• Discuss ways in which you can use past commands more
fast.
July 10, 2003 86