Title: Getting Started with Linux: Novells Guide to CompTIAs Linux Course 3060
1Getting Started with Linux Novells Guide to
CompTIAs Linux (Course 3060)
- Section 5
- Manage Directories and Files in Linux
2Objectives
- Understand the Filesystem Hierarchy Standard
(FHS) - Identify File Types in the Linux System
- Change Directories and List Directory Contents
- Create and View Files
3Objectives (continued)
- Manage Files and Directories
- Find Files
- Search File Content
- Archive, Back up, Compress, and Decompress Files
4Understand the Filesystem Hierarchy Standard (FHS)
- The file system concept of Linux (and, in
general, of all UNIX systems) is considerably
different than that of other operating systems - To understand the concept of the Linux file
system, you need to know the following - The Hierarchical Structure of the File System
- FHS (Filesystem Hierarchy Standard)
- Root Directory /
- Essential Binaries for Use by All Users (/bin/)
- Boot Directory (/boot/)
5Understand the FHS (continued)
- To understand the concept of the Linux file
system, you need to know the following
(continued) - Device Files (/dev/)
- Configuration Files (/etc/)
- User Directories (/home/)
- Libraries (/lib/)
- Mount Points for Removable Media (/media/)
- Application Directory (/opt/)
- Home Directory of the Administrator (/root/)
- System Binaries (/sbin/)
- Data Directories for Services (/srv/)
6Understand the FHS (continued)
- To understand the concept of the Linux file
system, you need to know the following
(continued) - Temporary Area (/tmp/)
- The Hierarchy below /usr/
- Variable Files (/var/)
- Process Files (/proc/)
- System Information Directory (/sys/)
- Mount Point for Temporarily Mounted File Systems
(/mnt/) - Directories for Mounting Other File Systems
7The Hierarchical Structure of the File System
8The Hierarchical Structure of the File System
(continued)
9FHS (Filesystem Hierarchy Standard)
- The structure of the file system is described in
the Filesystem Hierarchy Standard (FHS) - It specifies which directories must be located on
the first level after the root directory and what
they contain - It does not specify all details
- FHS defines a two-layered hierarchy
- The directories in the top layer (immediately
below the root directory /) - As a second layer, the directories under /usr/
and /var/
10Root Directory /
- The root directory refers to the highest layer of
the file system tree - Only directories are located here, not files
- When the system is booted, the partition on which
this directory is located is the first one
mounted - All programs that are run on the system start
must be available on this partition - The following directories always have to be on
the same partition as the root directory - /bin/, /dev/, /etc/, /lib/, and /sbin/
11Essential Binaries for Use by All Users (/bin/)
12Boot Directory (/boot/)
- /boot/ contains static files of the boot loader
- These are files required for the boot process
(with the exception of configuration files) - The backed-up information for the Master Boot
Record (MBR) and the system map files are also
stored here - These contain information about where exactly the
kernel is located on the partition - This directory also contains the kernel
- According to FHS, the kernel can also be located
directly in the root directory
13Device Files (/dev/)
- Each hardware component existing in the system is
represented as a file in /dev/ - An exception is network cards
- Character-oriented device files
- Block-oriented device files
- Major device numbers The connection to device
drivers in the kernel is implemented via numbered
channels, which correspond to the number of the
device driver in question - Minor device number A driver might be
responsible for several devices of the same type - /dev/null is also located here
14Device Files (/dev/, continued)
Use mknod to generate special device files for
specific devices.
15Device Files (/dev/, continued)
16Configuration Files (/etc/)
17User Directories (/home/)
- Every user on a Linux system has his own area in
which to create and remove files its home
directory - Individual configuration files can be found in
the users home directory - If there are no special settings, the home
directories of all users are located beneath
/home/ - The home directory of a user be addressed via
18Libraries (/lib/)
- Shared libraries are removed from the actual
program, stored in the system, and only called up
when the program runs - The directory /lib/ contains the libraries that
are used by programs in the directories /bin/ and
/sbin/ - The kernel modules (hardware drivers not compiled
into the kernel) are located in the directory
/lib/modules/ - You can find additional libraries below the
directory /usr/
19Mount Points for Removable Media (/media/)
- SUSE Linux creates directories such as the
following in the directory /media/ (depending on
your hardware) for mounting removable media - /media/cdrom/
- /media/cdrecorder/
- /media/dvd/
- /media/floppy/
20Application Directory (/opt/)
- Installed programs can store their static files
in the directory /opt/ - First, a directory with the name of the
application is created - The files are then stored in that directory
- Examples include GNOME (/opt/gnome/) and KDE3
(/opt/kde3/)
21Home Directory of the Administrator (/root/)
- The home directory of the system administrator is
not located beneath /home/ like that of a normal
user - Preferably, it should be on the same partition as
the root directory,/ - Only then is it guaranteed that the user root can
always log in without a problem and have her own
configured environment available
22System Binaries (/sbin/)
- Contains important system administration programs
- Programs in /sbin/ can also, as a rule, be run by
normal users, but only to display configured
values
23System Binaries (/sbin/, continued)
24Data Directories for Services (/srv/)
- The directory /srv/ contains subdirectories
filled with data of various services - For example
- The files of the Apache web server are located in
the directory /srv/www/ - The FTP server files are located in the directory
/srv/ftp/
25Temporary Area (/tmp/)
- Various programs create temporary files that are
stored in /tmp/ until they are deleted
26The Hierarchy below /usr/
- According with the FHS, represents a second
hierarchical layer
27The Hierarchy below /usr/ (continued)
28Variable Files (/var/)
- Contains a hierarchy described in the FHS
- This directory and its subdirectories contain
files that can be modified while the system is
running
29Variable Files (/var/, continued)
30Process Files (/proc/)
Generated dynamically when accessed
init always has the process number 1
31Process Files (/proc/, continued)
32Process Files (/proc/, continued)
33System Information Directory (/sys/)
- The directory /sys/ provides information in the
form of a tree structure on various hardware
buses, hardware devices, active devices, and
their drivers
34Mount Point for Temporarily Mounted File Systems
(/mnt/)
- Standard directory for integrating file systems
- It should only be used for temporary purposes
- da10 mount /dev/hda7 /mnt
- da10 umount /mnt
- If you do not include any options with mount, the
program tries out several file system formats - To specify a specific file system, use the option
-t - If the file system format is not supported by the
kernel, the command is aborted, and you receive
an error message - In this case, you must compile a new kernel that
supports the file system format
35Directories for Mounting Other File Systems
- A directory must exist at the point where you
intend to mount the file system - This directory is referred to as the mount point
- In most cases, only the user root can mount and
unmount directories - Use mount and umount
- /etc/mtab shows which file systems are mounted
- If you mount a file system to a non-empty
directory, existing contents of directory will
not be accessible - Mounted file system does not have to be on a
local hard disk
36Directories for Mounting Other File Systems
(continued)
- The directories listed below cannot be imported
from other machines - Some of the directories that can be shared are
37Exercise 5-1 Mount Removable Media
- In past versions of Linux, it was necessary to
mount removable media with some command to access
them and to unmount them afterwards - This has been automated in current kernel
versions - In this exercise, you access removable media
38Identify File Types in the Linux System
- The file types in Linux referred to as normal
files and directories are also familiar to other
operating systems - Normal Files
- Directories
- Additional types of files are UNIX-specific
- Device Files
- Links
- Sockets
- FIFOs
39Normal Files
- Normal files a set of contiguous data addressed
with one name - This includes all the files normally expected
under this term (such as ASCII texts, executable
programs, or graphics files) - You can use any names you want for these
filesthere is no division into filename and file
type - A number of filenames still retain this
structure, but these are requirements of the
corresponding applications, such as a word
processing program or a compiler
40Directories
- Directories contain two entries with which the
structure of the hierarchical file system is
implemented - One of these entries (.) points to the
directory itself - The other entry (..) points to the entry one
level higher in the hierarchy
41Device Files
- Each piece of hardware (with the exception of
network cards) in a Linux system is represented
by a device file - These files represent links between the hardware
components or the device drivers in the kernel
and the applications - Every program that wants to access hardware must
access it through the corresponding device file - The programs write to or read from a device file
- The kernel then ensures that the data finds its
way to the hardware or can be read from the file
42Links
- Links are references to files located at other
points in the file system - Data maintenance is simplified through the use of
such links - Changes only need to be made to the original file
- The changes are then automatically valid for all
links
43Sockets
- A socket refers to a special file with which data
exchange between two locally running processes
can be implemented through the file system
44FIFOs
- FIFO (first in first out) or named pipe is a term
used for files used to exchange data between
processes - The file can exchange data in one direction only
45Change Directories and List Directory Contents
- The prompt of a shell terminal contains the
current directory (such as tux_at_da10) - The tilde () indicates that you are in the
users home directory - Commands
- ls
- cd
- pwd
46ls
47ls (continued)
48cd
49pwd
- pwd print working directory
- pwd -P prints the physical directory without any
symbolic links
50Exercise 5-2 Change Directories and List
Directory Contents
- Smooth administration of a Linux system requires
familiarity with the directory tree and how to
move within it - The purpose of this exercise is to show you how
to orient yourself and move about within that
tree
51Create and View Files
- To create and view files, you need to know how to
do the following - Create a New File with touch
- View a File with cat
- View a File with less
- View a File with head and tail
52Create a New File with touch
- touch changes the time stamp of a file or
creates a new file with a size of 0 bytes
53Create a New File with touch (continued)
54View a File with cat
- You can use cat to view the contents of a file
- Comparable to the command type in DOS
- The command must include the filename of the file
you want to see
55View a File with less
- less displays the contents of a file page by page
- Even compressed files (such as .gz and .bz2) can
be displayed
56View a File with less (continued)
57View a File with head and tail
- Used to view the first or last lines of a file
- By default, they show ten lines
- head -20 displays the first twenty lines
- tail -f displays a continuously updated view of
the last lines of a file
58Exercise 5-3 Create and View Files
- To be able to view configuration and log files is
a necessary part of system administration - Various tools exist for this purpose, and you
choose the appropriate one depending on whether
you want to view the complete file or only part
of it - In this exercise, you practice creating and
viewing files or parts of them
59Manage Files and Directories
- In this objective, you learn how to
- Copy and Move Files and Directories
- Create Directories
- Delete Files and Directories
- Link Files
60Copy and Move Files and Directories
- Move files with mv
- mv .txt /tmp
- mv recipe new_recipe
- Copy files with cp cp source destination
- If you want to copy just the contents of a
directory, the target directory must already
exist - cp -R /tmp/quarterly-1 /tmp/expenses
61Copy and Move Files and Directories (continued)
62Copy and Move Files and Directories (continued)
- To copy the contents of proposals/ and all its
files, including hidden files and subdirectories,
to the existing directory proposals_old/ - To avoid copying the hidden files, do the
following
63Copy and Move Files and Directories (continued)
64Exercise 5-4 Copy and Move a File and a Directory
- Copying, moving, and renaming files are basic and
frequent operations done with files - Most probably, you are already very familiar with
these operations on a graphical desktop
environment - The purpose of this exercise is to get used to
performing these operations on the command line
65Create Directories
- You can use the command mkdir (make directory) to
create new directories - mkdir proposal
- Use the option -p to create a complete path
- mkdir -p proposal/january
66Exercise 5-5 Create Directories
- The purpose of this exercise is to show you how
to create directories
67Delete Files and Directories
- Use rmdir to delete empty directories
- rmdir proposal
- Use rm to delete files and directories
- rm part
- To delete directories (even if not empty)
- rm r testdir
- You can use the two options in Table 5-17 with rm
User must have permission to delete file(s)
68Exercise 5-6 Delete Files and Directories
- The purpose of this exercise is to show you how
to delete files and directories on the command
line - When deleting files and directories, it is
especially important to work only with the
permissions necessary for the task and not as
root, if possible - As root, you can easily destroy your installation
with the rm command, and there is no undelete in
Linux - Think twice before hitting enter on an rm command
issued as root!
69Link Files
- Each file is described by an inode
- To see the inode number you can enter ls I
- Each inode has a size of 128 bytes
- An inode contains all the information about the
file besides the filename - Link a reference to a file
- Create a hard link using ln, which points to the
inode of an already existing file - Hard links can only be used when both the file
and the link are in the same file system - Create a symbolic link using ln s a symbolic
link is assigned its own inode
70Link Files (continued)
71Link Files (continued)
- A symbolic link can point to a non-existing
object if the object and its corresponding name
no longer exist - For example, if you erase the file old in the
preceding example, in SLES 9 new will be shown in
a different color in the output of ls, indicating
that it points to a non-existent file - An advantage of symbolic links is that you can
create links to directories
72Exercise 5-7 Link Files
- Links are very convenient in administration, as
they help to avoid having different versions of
the same file within the file system - The purpose of this exercise is to learn how to
set hard and symbolic links and know the
difference between those two
73Find Files
- In this objective you learn how to find files and
programs using the following commands - KFind
- find
- locate
- whereis
- which
- type command
74KFind
75KFind (continued)
- Table 5-18 shows the results of three different
search strings
76KFind (continued)
Use to narrow search
77find
Usage find path criteria action
78find (continued)
- You can use the option -exec to call up another
command - Examples
- find /usr/share/doc/ -name ".txt" -type f \
/usr/share/doc/xine/faq/faq.txt - find -mtime 0
- find -name letter -type f -exec grep \
appointment \ -print appointment for \ next
meeting 23.08. - The two brackets are placeholders for the
filenames that are found and passed to grep - The semicolon closes the -exec instruction
79locate
- locate is an alternative to find name
- The package findutils-locate must be installed
- find can be quite slow
- locate searches through a database previously
created (/var/lib/locatedb), making it much
faster - The database is automatically created and updated
daily by SLES 9 - Use updatedb to update it manually
- Examples
- locate letter_Miller
- locate umount
80whereis
- whereis returns the binaries (option -b), manual
pages (option -m), and the source code (option
-s) of the specified command - If no option is used, all this information is
returned, if the information is available - whereis is faster than find, but it is less
thorough - Example
81which
- which searches all paths listed in the variable
PATH for the specified command and returns the
full path of the command - It is especially useful if several versions of a
command exist in different directories and you
want to know which version is executed when
entered without specifying a path
82type command
- type command can be used to find out what kind of
command is executed when command is entereda
shell built-in command or an external command - The option -a delivers all instances of a command
bearing this name in the file system
83Exercise 5-8 Find Files
- With about 100,000 files in a usual installation,
it is essential to be able to find files
effectively within the file system - It is possible to search using part of the file
name, but also to look for files that contain a
certain string, as covered in the exercise after
this one - In this exercise you practice finding files and
directories
84Search File Content
- Suppose you have dozens of text files and you
need to find all files that include a particular
word, phrase, or item - To scan these files without opening them in an
editor, you need to know the following - How to Use the Command grep
- How to Use Regular Expressions
85How to Use the Command grep
- grep search_pattern filename
- If you specify several files, the output will
print the matching line and the corresponding
filenames - grep "(bB)lurb" file
- For more complex patterns, use egrep (or grep
-E), which accepts extended regular expressions - Should comply with the standard regex syntax
86How to Use the Command grep (continued)
87How to Use Regular Expressions
88Exercise 5-9 Search File Content
- Sometimes all you know is a string that appears
within a certain file, but you do not know in
which file exactly - The purpose of this exercise is to show you how
to find such a file
89Archive, Back up, Compress, and Decompress Files
- In this objective, you learn how to
- Archive Files with ark
- Archive Files with tar
- Restore Files from tar Archives
- Copy Files to and from an Archive
- Use Data Backup Command-Line Tools
- Compress Files with gzip
- Decompress Files with gunzip
- Compress Files with bzip2
- Decompress Files with bunzip2
- View Compressed Files with zcat
90Archive Files with ark
- The program ark lets you collect multiple files
or even entire directories into an archive for - Backing up data
- Preparing data to be sent via floppy disk or
e-mail - The standard file format for archives in Linux is
.tar - Tar files can be quite large, but you can
compress them. ark archives and compresses in one
step - Formats supported .tar, .tar.gz, .tar.Z,
.tar.bz2, .gz, .bz, .bz2, .lzo, .Z, .zip, .lzh,
.zoo, .rar, .a
91Archive Files with ark (continued)
92Archive Files with tar
- tar is the most commonly used tool for data
backup - It requires an option, the name of the archive
(or the device file) to be written to, and the
name of the directory to back up - tar -cvf /dev/st0 /home
- tar -cvf /backup/etc.tar /etc
- tar -czvf /backup/etc.tgz /etc
- tar -cvf /dev/st0 /home -X exclude.files
- tar -tvf /dev/st0
- find . -name ".txt" -type f -print0 tar \
--null -cvf txt.tar -T - - When an archive is created, absolute paths are by
default made relative
93Archive Files with tar (continued)
94Restore Files from tar Archives
- tar -xvf /dev/st0
- Writes all files in the archive to the current
directory - Due to the relative path specifications in the
archive, the directory structure of the archive
is created here - To extract the files to another directory, use
the option -C, followed by the directory name - If you want to extract just one file, you can
specify the name of the file with the -C option - tar -xvf /test1/backup.tar -C /home/user1/.bashrc
95Exercise 5-10 Archive Files
- Files are frequently put into archives, as they
can then be sent more easily as e-mail
attachments - Backups also often make use of archives
- Various tools exist for this purpose in Linux,
and the purpose of this exercise is to show you
the more common ones
96Copy Files to and from an Archive
- cpio copies files into or out of a cpio or tar
archive - The archive can be another file on the disk, a
magnetic tape, or a pipe - In copy-out mode, cpio copies files into an
archive - find mystuff/ cpio -o gt mystuff.cpio
- In copy-in mode, cpio copies files out of an
archive or lists the archive contents - cat mystuff.cpio cpio -idvm
- In copy-pass mode, cpio copies files from one
directory tree to another, combining the copy-out
and copy-in steps without actually using an
archive
97Copy Files to and from an Archive (continued)
98Use Data Backup Command-Line Tools
- rsync creates copies of complete directories
across a network to a different computer - -a option switches rsync to archive mode
- rsync -a /home /shadow
- /. at the end of the directory to be mirrored
indicates that this directory is not included in
the copy - dd used to convert and copy files byte-wise
- dd if/etc/protocols ofprotocols.org
- mt lets you work with magnetic tapes
- mt -f /dev/st0 status
99Use Data Backup Command-Line Tools (continued)
- Use cron to automate backup (command line)
- System jobs are controlled by the file
/etc/crontab and the files in /etc/cron.d/ - Other jobs are defined by the scripts in
/etc/cron.hourly/, /etc/cron.daily/,
/etc/cron.weekly/, and /etc/cron.monthly/ - You specify which users can create cron jobs in
/var/spool/cron/allow and /var/spool/cron/deny - By default, only root can define jobs
- Users can edit their own crontab files by
entering crontab -e
100Compress Files with gzip
- gzip options file
- gzip .tex
- gzip -c price_list gt price_list_backup.gz
101Compress Files with gzip (continued)
102Decompress Files with gunzip
- gunzip decompresses a file compressed with gzip
and removes the suffix .gz - This function corresponds to the command
- gzip -d file
- The syntax for the command gunzip is
- gunzip file.gz
- Combining tar and gzip has been covered under
Archive Files with tar
103Compress Files with bzip2
- bzip2 options file
- bzip2 generates files twenty to thirty percent
smaller than gzip, but they take longer to
compress - tar -cjvf /backup/etc.tar.bz2 /etc
104Compress Files with bzip2 (continued)
105Decompress Files with bunzip2
- bunzip2 decompresses files compressed with bzip2
and removes the suffix bz2 - Equivalent to bzip2 -d file
- bunzip2 file.bz2
- Examples
- tar -xjvf /backup/etc.tar.bz2
- bzip2 -d /backup/etc.tar.bz2 -c tar -xf -
106View Compressed Files with zcat
- Use zcat to decompress files compressed with gzip
and write them to the standard output - The compressed file is not modified
- The function of zcat corresponds to
- gunzip -c -d
- The syntax for the command zcat is
- zcat file.gz
107Exercise 5-11 Compress and Decompress Files with
gzip and bzip2
- Usually files are not just put into archives, but
they are compressed as well to save on bandwidth
and/or storage capacity - In this exercise you practice compressing and
decompressing files
108Summary
- The Linux filesystem is arranged hierarchically
- There is only one root directory in Linux (/)
- Linux directories and files follow the FHS their
locations can be described using absolute or
relative pathnames - /dev contains character and block-oriented device
files used to identify most HW devices on the
system - /media and /mnt typically contain mount point
subdirectories used to mount and access removable
media
109Summary (continued)
- Most system configuration files are stored under
/etc shared libraries are stored under /lib - /usr stores most system applications, whereas
/opt stores most user applications - Log files, spool directories, and
application-specific files are typically stored
under /var - /boot contains information used to load the Linux
kernel - Home directories are typically located under /home
110Summary (continued)
- /bin and /sbin contain executable binary programs
- /proc and /sys are virtual directories that
contain information that is exposed by the Linux
kernel - The types of files the Linux filesystem are
normal files, directories, linked files, device
files, sockets, and named pipes - You can use head, tail, cat, and less to view the
contents of text files - You can use cp, mv, rm, rmdir, and mkdir to
create, change the location of, or remove files
and directories
111Summary (continued)
- Every file and directory has an inode
- Files may be a link to another file by name or by
inode - You may use KFind, find, locate, whereis, and
which to find files - Regular expression metacharacters can be used to
specify certain patterns of text - You can use ark, tar, and cpio to archive data
- You can schedule programs with cron
- Two common compression utilities are gzip and
bzip2