Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

Chapter 3: The file

System

CY 371: Linux OS lab Done by: Eng. Walaa Ayyad


3.1 The
2
File
🞑 Isa container for storing information
🞑 Can be treated as a sequence of characters
 No specific structure is required. It is up to you.
🞑 Does not contain the EOF (end of file) marker
 However, the kernel knows when to stop
reading the file
🞑 File attributes
 are not stored in the file
 but in a separate area of the hard disk
 known to kernel only not to user
CY 371: Linux OS lab Done by: Eng. Walaa Ayyad
File
3
Types
 Ordinary or regular file
 Contains a stream of data. This file can be a
 text file (contains only printable characters.)
 binary file (contains both printable and nonprintable characters
that cover the entire ASCII range (0 to 255).
 Directory
 A folder containing the names of other files and
subdirectories as well as a number associated with
each name.
 Device file
 This represents a device or peripheral.
 To read or write a device, you have to perform these
operations on its associated file.
CY 371: Linux OS lab Done by: Eng. Walaa Ayyad
3.2 What’s in a
4
(File)name?
 Filenames are limited to 255 characters. (case
sensitive).
🞑 Can’t contain / or NULL (ASCII 0) characters
🞑 May or may not have extensions
 However, avoid using unprintable and
special characters (?,`,$,*,&)
🞑 use alphabetic characters and numerals,
period (.), hyphen (-), and underscore
(_)
 Filenames can compromise multiple embedded
dots. It also can begin and end with a dot
 Never use a (-) at the beginning of a filename.
CY 371: Linux OS lab Done by: Eng. Walaa Ayyad
3.3 File System
5
Hierarchy
 LINUX files are organized in an inverted tree
(hierarchical structure)
🞑 top is called root – reference point for all files
 root directory(/) has number of subdirectories,
which might have more subdirectories under them
🞑 The parent of any file type is a directory /
root directory

bin dev home lib tmp sbin etc usr var

fd0 lp0 bhoffm30 passwd bin sbin local lib


floppy disk printer 0 password file

bin Mail bin lib

CY 371: Linux OS lab Done by: Eng. Walaa Ayyad


Pathnames: Two
6
Types
Files are accessed with pathnames (like /etc/passwd).
🞑 Separated by /’s. The first / is the root, and the others act as
delimiters of the pathname components
 Absolute pathname
🞑 Specifies full location of the file starting from the root directory
🞑 It lists all directories in the hierarchy that lead to the file. (like
/etc/passwd)
🞑 No two files can have identical absolute pathnames
 Relative pathname
🞑 Specifies location with reference to the user’s current location (like
../include)

CY 371: Linux OS lab Done by: Eng. Walaa Ayyad


Relative filename

 A relative path name describes the location of a directory or a file as it


relates to the current directory.

 A relative path name never begins with a slash (/) character. However, it
does use slashes (/) within the path name as delimiters between object
names (for example, directory name or file name).

 If you are in a directory and you want to move down to access another
directory in the hierarchy, you do not have to enter an absolute path
name. Simply enter the path starting with the name of the next directory
down in the tree structure.
3.4 The UNIX File
7
System
/ root directory that contains all
 /bin and /usr/bin binary files for common commands.
 /sbin and /usr/sbin binaries reserved for sys. Admin.
 /usr files and applications for users
 /etc system configuration files
 /dev all of the device files
 /home stores users’ home directories
 /tmp temporary files
 /var variable files, logs, and print
 /lib and /usr/lib queues
 /usr/include library files used by programs
 /usr/share/man standard header files used by C programs
this is where man pages are stored

CY 371: Linux OS lab Done by: Eng. Walaa Ayyad


3.5 Using Absolute Pathnames
8
with Commands
 There are two possible situations when a
command
must be used with an absolute pathname:
1. If a command is not in your $PATH, you can
use it by giving the absolute path.
2. A command sometimes occurs in two
directories, both of which could be in PATH.
For example: /b in/ r m

CY 371: Linux OS lab Done by: Eng. Walaa Ayyad


3.6 The Home
9
Directory
 Directory where user is placed on login.
 The system administrator sets the home directory for a user in
/etc/passwd at the time of opening a user account.
 The shell variable HOME maintains the absolute
pathname of the home directory.
$ echo $HOME
/home/romeo
 Most shells (except Bourne) also use the ~ symbol to refer
to the home directory.
 (~/) refers to yours home directory, but when followed by a string (username),
it refers to that username home directory

CY 371: Linux OS lab Done by: Eng. Walaa Ayyad


3.7 Navigating the File
10
System
 At any instant of time, a user is located in a
directory called current directory
🞑 The command pwd displays the absolute pathname of
this directory
 Navigation is performed by the command cd
cd myfiles : will switch to subdirectory named myfiles
cd /home/myfiles/mydocs : will switch to mydocs
directory
cd / : will move to the roor
directory cd : will move to the
home directory cd ~ : also
CY 371:move
Linux OSto
labthe
Donehome
by: Eng. directory
Walaa Ayyad
3.8 Relative Pathnames ( .
11
and .. )
 . Refers to the current directory
 .. Refers to the parent of the current directory

cd .. : will move the parent directory of the


current directory
$pwd
/home/rana/Desktop
$cd ../..
$pwd
/home

CY 371: Linux OS lab Done by: Eng. Walaa Ayyad


3.9 mkdir: making
12
Directories
 mkdir dirname(s): will make new subdirectory(s) named
dirname(s)
🞑 mkdir mystuff
🞑 mkdir /tmp/mystuff
 a single invocation of mkdir can create a directory tree.
 Ex: mkdir progs progs/include progs/lib
 I created both a directory progs and two subdirectories,
include and lib.
 mkdir can create one or more directories based on # of
arguments
🞑 mkdir one /home/two ../test

CY 371: Linux OS lab Done by: Eng. Walaa Ayyad


3.10 rmdir: Removing
13
Directories
 rmdir dir_name – remove empty directories
provided as arguments
 Two conditions for rmdir
1. The directory must be empty
2. The current directory is above the
directory to be deleted
 mkdir and rmdir works only in directories owned by
the users.
🞑 You can’t delete other’s directories or files
🞑 Permissions play role here (covered later)

CY 371: Linux OS lab Done by: Eng. Walaa Ayyad


3.11 ls: Listing
14
Files
 ls [options] - lists the contents of the current directory
 ls [options] dir_name - list contents of that directory
 ls [options] filename - check whether file name exists
 filename or dir_name can be absolute or relative
names.
 EX:$lscalendar/bin/perl
calendar
/bin/perl:Nosuchfileordirectory
 The default output is ordered in ASCII collating
sequence (numbers first, uppercase, and then lowercase)
CY 371: Linux OS lab Done by: Eng. Walaa Ayyad
Please try them all.
Required in exam

15
CY 371: Linux OS lab Done by: Eng. Walaa Ayyad
3.12 cp: Copying
16
Files
 cp [options] source(s) destination
🞑 This
command copies a file, preserving the original (source) and creating an
identical copy (destination).
 If you already have a file with the new name, cp will
overwrite and destroy the duplicate.
 The destination can be a directory.
🞑 In this case, the source is copied inside the destination directory
 Ex: cp fork.c progs/fork.c.bak fork.c copied to fork.c.bak under
progs
 cp fork.c progs fork.c retains its name under progs
 When it is used to copy multiple source files, the
destination MUST be a directory and MUST already exist
CY 371: Linux OS lab Done by: Eng. Walaa Ayyad
cp
17
options
🞑 -i:
prompt user when the file already exists in
the destination
🞑 -R: copy directories recursively (to work
correctly, destination must NOT exist
before running the command)
🞑 -v: print files that are being copied

 Note: You must have permission to read a


file in order to copy it.

CY 371: Linux OS lab Done by: Eng. Walaa Ayyad


3.13 m v Moving
18
(Renaming) Files
 mv [options] filename newfile_name
 mv [options] directory newdirectory_name
 mv [options] filenames directory
 This command will move (rename) file or
directory
🞑 Unlike the cp command, mv will NOT
preserve the original file
 mv options:
🞑 -f: move the file even if it already
exists
🞑 -i: prompts if the file already exists
CY 371: Linux OS lab Done by: Eng. Walaa Ayyad
3.14 rm:
19
Deleting Files
 rm [options] filename(s)
 This command will delete a file or
directory permanently
 rm options
🞑 -i: prompt user before deleting
🞑 -r or –R: delete directories
recursively
🞑 -f: force deletion even if file is write
protected
 rm -rf * - this command will delete
everything under the current directory and below
CY 371: regardless of by:permissions.
Linux OS lab Done Eng. Walaa Ayyad
3.15 cat: Displaying and
20
Concatenating Files
 cat [options] [file_list]- The command
outputs the contents of one or more files
(file_list) on the terminal
🞑 It just print each byte in the file
🞑 Not used to display executable files since it
will produce junk.
 cat options:
🞑 -e: display $ at the end of each line
🞑 -n: put line numbers with the displayed
lines.
CY 371: Linux OS lab Done by: Eng. Walaa Ayyad
Using cat without
21
argument
 When cat command is executed without arguments, it
takes input from the keyboard.
$cat
This is a
test. This
is a test.
 Some terms
usually used with
commands:
🞑 standard input:
default is the
keyboard
CY 371: Linux OS lab Done by: Eng. Walaa Ayyad
Creating Files
22
Using cat
 cat > myfile
🞑 Create a file on the current working directory
named as myfile allowing you to input any
text you wont.
 cat file1 > myfile
🞑 Save the content of file1 in myfile
 cat file1 >> file2
🞑 Appends file1 onto the end of file2
CY 371: Linux OS lab Done by: Eng. Walaa Ayyad

CY 371: Linux OS lab Done by: Eng. Walaa Ayyad


3.16 more: The UNIX
23
Pager
 more filename(s): display filename(s) one page at
a time
🞑 Used for large files to facilitate reading
🞑 At the bottom of the page you see the filename
and the percentage that has been viewed
 Internal commands (not displayed on screen)
🞑 q: quit the displayed file
🞑 h: invoke help
🞑 f or space bar: one page forward
🞑 b: one page backward
🞑 Enter: one line forward
🞑 . (dot): repeat last command
🞑 /pat: search forward for the string pat

CY 371: Linux OS lab Done by: Eng. Walaa Ayyad


3.18 wc: Counting Lines, Words,
24
and Characters
 wc [options] [filename(s)] : count lines,
words, characters in filename(s)
🞑 Output is displayed in four columns (lines,
words, characters, and filename)
 wc options
🞑 -l: count lines only
🞑 -w: count words only

🞑 -c: count characters only

CY 371: Linux OS lab Done by: Eng. Walaa Ayyad


3.22 tar: The Archival
25
Program
 tar [options] archive_name.tar filenames:
combine filenames into the archive name
provided.
🞑 The archive name ends with a tar extension
 tar options
🞑 -c: creates an archive
🞑 -x: extract files from archive

🞑 -t: display files in archive

🞑 -v: display the progress of the operation

🞑 -f: specify the name of the archive


CY 371: Linux OS lab Done by: Eng. Walaa Ayyad
3.23 gzip: The
26
Compressing Files
 gzip [option] filename - compress and add the
.gz extension and remove the original
file
 gzip options:
🞑 -l : displays the amount of compression achieved
🞑 -d: uncompress the file
 You can uncompress also by using the
command gunzip
 A tar file can be compressed to get a
compressed archive, called a tar-gzipped
file (filename.tar.gz or sometimes .tgz)
🞑To extract, simply reverse the procedure: use gunzip
andOSthen
CY 371: Linux tarby: Eng. Walaa Ayyad
lab Done
3.24 zip: The Compression
27
and Archival Program
 zip archive_name.zip filenames: compress and
archive filenames into the name
archive_name.zip
🞑 it doesn’t overwrite the archive_name.zip if exists. In
that case it simply updates it
🞑 The option –r: recursive compression in the directory
tree
 You can decompress zip file with the command
unzip
🞑 If the file exist while unzipping, it asks you if you
want to replace it.
The OS
CY 371:🞑Linux option -v allows
lab Done by: Eng.you toAyyad
Walaa view the compressed

You might also like