Unix File Structure
File Hierarchy
Unix file system is a hierarchy of files
- Tree structure
- / is the root
Absolute pathname
- Files represented by their position in the hierarchy
- File names separated by '/'
Directory files vs. ordinary files
Directories, data, executable code and I/O devices are all files
File names
- typically contain letters, numbers, underscore, period, comma
- names are case sensitive
- extensions indicate file contents, format, or purpose
Hidden files have names that start with '.'
- .profile
- .sh_history
- .mailrc
Special characters have special meanings to the shell
- & ; | * ? ' " ` [ ] ( ) $ < > { } ^ # / \
- <return> <space> <tab>
- control characters (view with stty)
Special characters may be quoted with '\'
Directories
Working directory is the directory you are RinS
- check the current working directory with pwd
- change the current working directory with cd
Home directory
- set as value of HOME
- generally referred to as ~username
- return to your home directory with cd
Relative path names
- file references that don't start with '/' are relative to the working
directory
- . refers to the current working directory
- .. refers to the parent directory
File Operations
Creating and removing directories
- mkdir creates a new empty directory (relative or absolute)
- rmdir removes a directory
File manipulation
- cp copies files
- rm deletes files
- mv renames files and/or moves them to different directories
Listing Files Listing files in a directory with ls
- ls -a lists all files (including hidden ones)
- ls -l provides detailed information about files
-rwxr--r-- 1 john staff 335 Jan 22 13:23 myfile- access permissions
- number of links (for ordinary files) or number of subdirectories (for
directories)
- owner of the file
- group to which the file belongs
- size of the file in bytes
- date and time file was last modified
- filename
- ls -t sorts files by time last modified
- ls -F uses symbols to indicate file types
Access Permissions
Checking access permissions (ls -l)
- 1 = type of file:ordinary (-), symbolic link (l), directory (d), or device (b,c)
- 2-4 = read, write, execute permissions of user (owner)
- 5-7 = read, write, execute permissions group
- 8-10 = read, write, execute permissions of others (everyone else)
Changing access permissions
- chmod permissions filename(s)
- absolute permissions represented by a 3-digit octal number
- add permissions by specifying who+permission
who is one or more of user (u), group (g), other (o), or all (a)
- permission is one or more of read (r), write (w), or execute
(x)
remove permissions by specifying who-permission
Links
Hard links
- represented by absolute file pointers
- files must be local
- only root can set hard links to directories
Soft links
- represented by full pathname
- may refer to files on other mounted systems
- anyone can point to directories
Creating/removing links
- ln [-s] pathname linkname
Using the Shell
Command line basics
Syntax: command [arg1] [arg2] ... [argn]
Arguments
- Input to the program
- Options
Options
- modify the original meaning of the command
- usually preceded by '-'
- collections of options generally only need one '-'
Executing the command
Operating system searches for command
- if relative path is given, searches directories listed in PATH
- if absolute path is given, goes directly to that location
- files prefixed with './' are treated as absolute pathnames
Commands generally open 3 files represented by their file descriptors
- 0: standard input
- 1: standard output
- 2: standard error output
The terminal is a file
- represented as a file in /dev/
- by default, the terminal is standard input, output and error output
Redirection
Indicates other file(s) are to be used as standard input/output/error
Standard input
- < filename reads all standard input from the file
Standard output
- > filename writes all standard output to a new file (creates or
overwrites it)
- >> filename appends all standard output to the end of the file
Standard error
- 2> filename writes all standard error output to the file
- 2>&1 writes all standard error output to the same file used
for standard output
Pipes
Pipes pass standard output of one process to the standard input of another
- pipes represented by '|'
- no limits on number of pipes
Filters
- modify the standard output of one command before passing it to the
next
- interactive programs cannot be used as filters
Sending output to terminal AND a file with tee
Background processes
Running a process in the background
- put '&' at end of the command
- operating system returns process ID (PID) and job number
- shell prompt returns immediately
Process status
- ps lists process id, terminal, time running, and command name
- jobs lists job number, status, and command name, and indicates most
recently (and next-most recently) jobs put in the background
- fg puts most recent job in the foreground (unless job number is specified)
Killing a job
- terminate a specified process id or job number
- can specify signal
Filename generation
Metacharacters or wild cards represent variations on file names
- ? represents a single character that could be anything
- * represents 0 or more characters that could be anything
- [] surrounds a list of possible single characters that may be substituted
Shell Scripts
Simple Shell Scripts
A shell script is a file that contains commands to be executed by the
shell.
- Commands are executed sequentially
- Blank lines are ignored
- # precedes comments
- #! /bin/ksh indicates which shell to use
You need to make the file executable with chmod