William E. Shotts, Jr.: The Linux Command Line, No Starch Press
Learning the Shell
df- free disk spacefree- free memorydate- current datecal- show a calendar
User prompt ends with $, super user prompt ends with #
Common ls Options
| Option | Long Option | Description |
|---|---|---|
| -a | –all | all files, including hidden |
| -d | –directory | with -l show details about the directory rather than its contents |
| -F | –classify | append an indicator character to end of each item (e.g. forward slash if name is a directory). |
| -h | –human-readable | with -l display file sizes in human-readable format rather than in bytes |
| -l | long format | |
| -r | –reverse | display results in reverse order |
| -S | sort results by file size | |
| -t | sort by modification time |
Fields in long listing
-rw-r–r-- 1 root root 453764 2012-04-03 11:05 oo-welcome.odt
1 = File’s number of hard links (often 1) root = user who owns the file root = group who owns the file 453764 = size in bytes 2012-04-03 11:05 = last modified date
Directories Found on Linux Systems
| Directory | Comments |
|---|---|
| / | root directory |
| /bin | binaries for the system to boot and run |
| /boot | Linux kernel (/boot/vmlinuz), initial RAM disk image (drivers needed at boot time), boot loader |
| /dev | list of all devices |
| /etc | system wide config files, shell scripts for starting system files |
| /lib | shared library files for core system programs |
| /lost+found | used in partial recovery situations, usually empty |
| /media | mount point for CD drives etc |
| /mnt | older systems manual mount point for removable drives |
| /opt | optional software |
| /proc | virtual file system maintained by the kernel - not a real files system - but all files are readable |
| /root | home directory for the root account |
| /sbin | system binaries |
| /tmp | temporary files |
| /usr | all programs and support files used by regular users |
| /usr/bin | binaries installed by the linux distro - usually 1000s of programs |
| /usr/lib | shared libraries for /usr/bin |
| /usr/local | system wide, but usually not via the linux distro, but via a sys admin user e.g. /usr/local/bin is where programs compiled from source usually go |
| /usr/sbin | more system binaries (TODO what is the difference between here and /sbin?) |
| /usr/share | shared data for programs in /usr/bin e.g. default config files, icons |
| /usr/share/doc | documentatino |
| /var | storage for data which is likely to change e.g. logs, databases, email |
| /var/log | log files - /var/log/messages is important |
Wildcards
*any character?any single character[characters]any character in set ‘characters’[!characters]any character NOT in set ‘characters’[[:class]]any character of the specified class e.g.[:alnum],[:alpha],[:digit],[:lower],[:upper]
Manipulating files
cp
cp -a item1 item2additionally copy attributes including ownership and permissions (usually copies take on the default attributes of the user performing the copy)cp -u item1 item2copy only files which don’t exist or are newercp -i item1 item2interactivecp -r item1 item2recursivecp -v item1 item2verbosecp file1 file2 dir1copy file1 and file2 to dir1 (dir1 must exist)cp dir1/* dir2all files in dir1 are copied to dir2 (dir2 must exist)cp -r dir1 dir2recursively copy files in dir1 to dir2- IMPORTANT if dir2 exists, dir1 will be copied within it, if dir2 doesn’t exist it will be created and will hold the same contents as dir1
mv
mv dir1 dir2move dir1 and its contents to dir2- IMPORTANT if dir2 doesn’t exists, dir1 will effectively be renamed dir2
rf
rf -fignore nonexistent files and do not prompt - TODO what does this mean?
Commands
A command is one of:
-
an executable binary or script
-
a command built into the shell itself e.g.
cdis a bash shell builtin -
a shell function - minature shell scripts incorporated into the environment
-
an alias
-
typeshows which of the 4 a particular command is e.g.type cdgivescd is a shell builtin -
whichshows the location of an executable (doesn’t necessarily work for shell builtins) -
helphelp command for shell builtins e.g.help cd -
--helpoften supported by executable programs to show similar usage information e.g.mkdir --help -
mandisplay a commands man page - if no section is specified the first section available is shown - most likely section 1 - to show a specific section use e.g.man 5 passwd- man pages are stored in/usr/share/manunder the appropriate section diretorySection Contents 1 User commands 2 Programming interfaces for kernel system calls 3 Programming interfaces to the C library 4 Special files such as device nodes and drivers 5 File formats e.g. man 5 passwd6 Games and amusements such as screensavers 7 Miscellaneous 8 System administrator commands e.g. man mount -
apropossearch man titles and descriptions for a keyword or similar term (man -kdoes the same thing) -
whatismatches man titles and descirption for a keyword exactly -
infoGNU alternative tomane.g.info ls- tree-structured - most commands are in the coreutils.info -
alias name='string'set up an alias - remove withunalias-aliason its own lists all defined aliases
I/O Redirection
stdout, stderr and stdin can be thought of as files where the output for stdout and stderr is by default connected to the screen and the input for stdin is, by default, connected to the keyboard. These can be redirected using I/O redirection.
ls -l /usr/bin > ls-output.txtredirects stdout to a file (if any errors these still go to stderr, which by default is still printed to the screen - in this case the file will be created or truncated before the error happens since > replaces content rather than appending content)>>appends content e.g.ls -l /usr/bin >> ls-output.txt- File descriptors can alternatively be used to redirect stdin, stdout and stderr (and is the only way to redirect stderr). These are 0, 1 and 2 respectively.
2>redirects stderr e.g.ls -l /usr/idontexist 2> ls-error.txt 2>&1redirects stderr to file descriptor 1 i.e. stdout sols -l /bin/usr > ls-output.txt 2>&1will redirect stdout to a file and then stderr to the same destination i.e. also a file&>shorthand for2>&1(recent versions of bash only)2> /dev/nullto discard anything sent to stderrcatconcatenates files and can be used with redirection to rejoin files which have been split e.g.cat movie.00* > movie.mp4(crucially for this wildcards expand in sorted order)catwithout any arguments uses stdin - ctrl-d (EOF) to terminate<redirects stdin e.g.cat < lazy-dog.txtredirects the file lazy-dog.txt to stdin with the result thatcatprints the contents of the file (catuses stdin when no arguments are passed)|pipes the standard output of one command to the standard input of another command- e.g.
ls -l /usr/bin | less
- e.g.
uniqremoves duplicates from asorted listuniq -donly displays duplicates from asorted listwcprints line, word and byte counts (all 3, tab seperated)- without arguments uses stdin (so often used at the end of a pipeline to count things)
grep pattern [files...]where pattern is a regex- option
-iignores case - option
-vprints lines which DON’T match
- option
tail -f [file]to tail a file real timeteereads from stdin and redirects to stdout AND files (hence a T on the pipeline)- e.g.
ls /usr/bin | tee ls.txt | grep zipwill capture the output ofls /usr/binto a file as well as passing it intogrep zip
- e.g.
Configuration and the Environment
A Gentle Introduction to VI
- Most commands can be prefixed with a number e.g.
5jto move down 5 lines uundoaappend after end of wordAappend to end of current lineoopen a line below the current lineOopen a line above the current lineGgo to the last line in the documentddcut the current linedwcut to start of next wordd$cut to end of lined0cut to beginning of lineppaste after current linePpaste before current lineyy‘yank’ the current line (i.e. copy) - then similar combinations to cut