Đặng Thanh Bình
Basic System Administration
Contents
• File and Directory management
– ls, cd, pwd, mkdir, mv cp, rm, rmdir, locate, find,
grep
– touch, cat
– Recursive and interactive modes
– PATH variable, which command
• Linking Files
• File and directory permissions
• sudoers
2
FILE AND DIRECTORY MANAGEMENT
3
The Linux Directory Structure
• Directory: Used to organize other files into a
logical tree structure
– Stored in a filesystem of a specific partition in
the hard disk
• Root: The top level directory
– Referred to using the / character
• Forms root of a hierarchical tree
4
The Linux Directory Structure
• The Windows file system structure
5
The Linux Directory
Structure
• The Linux file system structure
6
File Types
• 4 basic file types
– Normal files (program, text, library, …)
– Directory
– Special files (device, socket, pipe, …)
– Symbolic links (symlinks)
7
File Name Regulations
• Maximum 255 characters
• May contain any characters
special characters)
(including
• Hidden file/directory starts with a period (.)
8
Pathname
• Absolute pathname: starts with “/”
• Relative pathname: DOES not start with a
“/”
• Special pathnames:
• .. – parent directory
• . – current directory
9
Changing Directories
• Home directory: unique to each user
– ~ metacharacter used to refer to home directory
• pwd (print working directory) command:
displays current directory in the directory tree
• cd (change directory) command: change the
current directory in the directory tree
– Argument specifies the destination directory
– cd: go to user’s home directory
– cd PATHNAME
10
Listing Files
• ls command: List the files in a directory
• May pass an argument indicating the
directory to be listed
– –F option: Argument to indicate file types
– –l option: Argument to list long file listings
Long listing for each file
includes
eight
components
• File type character
• List of permissions (mode
of the file)
• Hard link count
•
•
•
•
Owner
Group owner
File size
Most recent modification
time
11
• Filename
Listing Files
12
Listing Files
13
Creating Files
• touch command: creat an empty file
– touch FILENAME
– touch FILE1 FILE2
• cat command: display and/or edit file
content
– -n option: displays line number and contents
– cat FILENAME
– cat > FILENAME (use Ctrl-D to finish)
14
Viewing Text Files
• tac command: displays contents of a text file
in reverse order
• head command: view first ten lines of a file
• tail command: view last ten lines of a file
• For head and tail commands
– Line count includes blank lines
– Can provide numeric option to specify the
number of lines to be displayed (e.g., head -2
filename)
15
Viewing Text Files
• more command: displays text files page-bypage
– Pressing Spacebar displays the next page
– Pressing Enter displays the next line
• less command: same as more command,
but can also use cursor to scroll
• Interaction with more and less:
– pressing h key gets Help screen
– pressing q key quits more and less commands
16
Viewing Text Files
• more and less can be used with output of
other commands
• If output is too large to fit on terminal
screen, use “|” metacharacter and more or
less command
– e.g., ls -l | more
17
Displaying the Contents of Binary Files
• strings command: searches for and displays
text characters in a binary file
– Might indicate purpose of binary file
• od command: displays contents of file in
octal format (numeric base 8 format)
– -x option displays contents of the file in
hexadecimal format (numeric base 16 format)
18
Managing Files and Directories
• mkdir command: creates new directories
– Arguments specify
relative pathname
directory’s
absolute
or
• mv command: moves files
– Minimum of two arguments:
• Source file/directory (may specify multiple sources)
• Target file/directory
– Pathnames can be absolute or relative
– For multiple files, can use wildcards in pathname
– Also used to rename files or directories
19
Managing Files and Directories
• cp command: copies files
– Same arguments as the mv command
– Also used to make copies of files
20
Managing Files and Directories
• Recursive: referring to itself and its own
contents
– Recursive copy command copies the directory and all
subdirectories and contents
– Recursive search includes all subdirectories in a
directory and their contents
– Use –r option
• Interactive mode:
overwriting files
Prompts
user
before
– –i option
– –f option (force): Overrides interactive mode
21
Managing Files and Directories
• rm command: Removes files
– Arguments are a list of files
– Can use wildcards
– Interactive mode by default
– Use -f option to override
22
Managing Files and Directories
• rmdir command: removes directories
– Arguments are a list of files
– Can use wildcards
– Interactive mode by default
– Use -f option to override
– Cannot be used to remove directory full of files
• To delete directory and all its contents
(subdirectories and files), use rm –r
command
23
Finding Files
• locate command: Search for files on system
– Receives full or partial filename as argument
– Uses premade indexed database of all files on
system
• To update the database use updatedb command
– Information returned may not fit on screen
• Use with more or less commands
24
Finding Files
• find command: recursively search for files
starting from a specified directory
– Slower than locate command, but more versatile
– Format: find <start directory> -criteria
find>
• e.g., find /root –name project
– If using wildcard metacharacters, ensure that they
are interpreted by the find command
• Place wildcards in quotation marks
– To reduce search time, specify subdirectory to be
searched
25