Unix crash course: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
#Introduction to Unix | #Introduction to Unix | ||
<span id="filesystem"></span> | <span id="filesystem"></span> | ||
=== Filesystem === | === Filesystem === | ||
<ol> | <ol style="list-style-type: decimal;"> | ||
<li>Directories | <li>Directories | ||
<ol style="list-style-type: lower-alpha;"> | |||
<li>first and bottom of the directory of the file structure tree is called root '''/'''</li> | |||
<li>'''/root''' in addition, there is a file directory, within the root directory called /root (“slash root”), which acts as root’s home directory</li> | |||
<li>'''/home''' normal users’ directories</li> | |||
<li>'''/boot''' contains the boot directory, including kernel, initial RAMdisk, and bootloader</li> | |||
<li>'''/dev''' lists all devices of the computer</li> | |||
<li>'''/etc''' configuration files for nearly all programs that need a state</li> | |||
<li>'''/tmp''' temporary files for the system</li> | |||
<li>'''/var''' variable-length files used by programs | |||
<ul> | |||
<li>'''/var/spool''' old-school webmail location</li> | |||
<li>'''/var/www''' location of http server files</li></ul> | |||
</li> | |||
<li>'''/usr''' files available to users, including binaries for daily user tasks in /usr/bin</li> | |||
<li>'''/sbin''' binaries used only by root</li> | |||
<li>'''/bin''' binaries for the system everyone needs to use</li></ol> | |||
</li> | |||
<li>Commands | <li>Commands | ||
<ol style="list-style-type: lower-alpha;"> | |||
<li>'''cd''' change directories</li> | |||
<li>'''~''' alias used to shortcut the current user directory</li> | |||
<li>'''Ctrl+R''' search history of commands in <code>bash</code>; also available as output from <code>history</code></li> | |||
<li>'''!!''' runs the previous command</li> | |||
<li>'''ls''' list in the contents of current directory</li> | |||
<li>'''.''': the current directory</li> | |||
<li>'''..''' : go up one directory#User and Group Configuration</li></ol> | |||
</li></ol> | |||
<span id="the-shell-explained"></span> | |||
=== The Shell explained === | |||
### | # A shell is a command interpreter where the user interacts with the system via command line interface (CLI) | ||
# Different shells currently in-use A. '''Bourne-Again SHell''' - located at <code>/bin/bash</code> is the main shell and is used was a default on most Linux systems B. '''sh''' - The Bourne Shell, old school version does not require any libraries. Rarely seen as a user-interface shell; typically a symbolic link to another shell binary. Created by Stephen Bourne, first included in Unix V7 C. '''tcsh''' Tee Shell. Based on the earlier C-shell from Unix. Has some additional features built into it. D. '''csh''' C-shell, a BSD component not often seen on Linux. Created by Bill Joy at UC Berkeley. E. '''ksh''' Korn shell from Bell Labs in the 1980s. The idea was to incorporate features from the Bourne Shell along with C-programming syntax. F. '''zsh''' 1990s, incorporates additional ideas into ksh. | |||
# Built-in Commands A. <code>cd</code> changes directory - <code>cd ~</code> or <code>cd /home/user</code> are equivalent B. <code>ls -</code> lists files in a directory - default behavior just lists files - <code>-a</code> lists all files including hidden ones - <code>-l</code> long format - <code>-d</code> just directories - <code>-F</code> uses a special character to rep filetypes - <code>-R</code> recursive listing C. Single characters are substituted by <code>?</code>, wildcards by <code>*</code> D. Exiting the system - <code>exit</code> exits the current shell - <code>logout</code> only works for login shells E. Remote access - '''ssh''' or ''secure shell'' provides the basic command to create a shell across an encrypted pipe - <code>ssh username@remote.host</code> logs in as user ''username'' on a computer ''remote.host'' - several programs provide ssh access as a feature, such as PuTTY - '''scp''' or ''secure copy'' provides file transfer over ssh - <code>scp /path/to/localmachine/ username@remote.host:/path/to/destination</code> - <code>scp username@remote.host:/path/from/remotemachine /path/to/localdestination</code> | |||
<span id="man-pages-and-getting-help"></span> | |||
=== Man pages and getting help === | |||
# displays the manual pages for any packages on the system | # displays the manual pages for any packages on the system | ||
Line 199: | Line 60: | ||
# most programs also allow for <code>--help</code> or <code>-?</code> to be passed for a quick rundown on commands and usage | # most programs also allow for <code>--help</code> or <code>-?</code> to be passed for a quick rundown on commands and usage | ||
<span id="unix-filesystem-permissions"></span> | |||
=== Unix filesystem permissions === | |||
<ol> | <ol style="list-style-type: decimal;"> | ||
<li>filesystem permissions saved as a series of 3 bit numbers</li> | <li>filesystem permissions saved as a series of 3 bit numbers</li> | ||
<li>format: <code>d,rwx,rwx,rwx</code> for read-write-execute | <li>format: <code>d,rwx,rwx,rwx</code> for read-write-execute | ||
<ol style="list-style-type: lower-alpha;"> | |||
<li><code>d</code> - denotes a Directory or file</li> | |||
<li>first triad is for the User of the file</li> | |||
<li>second triad is for Group members of the file</li> | |||
<li>third triad is for Everyone else</li></ol> | |||
</li> | |||
<li>Read-write-execute for files | <li>Read-write-execute for files | ||
<ol style="list-style-type: lower-alpha;"> | |||
<li>'''Read''' allows the file to be opened</li> | |||
<li>'''Write''' allows for the file to be edited</li> | |||
<li>'''Execute''' allows for the file to run as a program</li></ol> | |||
</li> | |||
<li>Read-write-execute for directories | <li>Read-write-execute for directories | ||
<ol style="list-style-type: lower-alpha;"> | |||
<li>'''Read''' allows for the directory to view filenames in the directory</li> | |||
<li>'''Write''' allows files to be added or deleted from a dir</li> | |||
<li>'''Execute''' allows the directory to be visited, “permission to traverse through”; must have this to execute programs within a directory</li></ol> | |||
</li> | |||
<li><code>chmod</code> changes file and directory permissions | <li><code>chmod</code> changes file and directory permissions | ||
<ol style="list-style-type: lower-alpha;"> | |||
<li>two modes for chmod | |||
<ol style="list-style-type: decimal;"> | |||
<li>'''symbolic mode''' <code>chmod {u,g,o}[+,-,=]{r,w,x}</code></li> | |||
<li>absolute mode - chmod 777 /path/to/file` * 0 no permissions * 1 execute * 2 write * 4 read F. chown - change file owner and group</li> | |||
<li>chown user /foo - changes foo’s owner to user</li> | |||
<li>chown root:root /foo - changes foo’s owner to root, also changes foo’s group to root G. echo - displays any text you enter after the command</li> | |||
<li>primarially used in shell scripts or ‘programs’ that utilize a combination of of built-in commands, variables, and executables to complete tasks</li> | |||
<li>variables reached from echo by invoking the variable name as $NAME</li></ol> | |||
</li></ol> | |||
</li></ol> | |||
<span id="redirection-symbols"></span> | |||
=== Redirection symbols === | |||
# prog > - redirects std output to the file, truncating and rewriting | # prog > - redirects std output to the file, truncating and rewriting | ||
Line 229: | Line 107: | ||
# $() - is used like a backtick; more resistant to weird syntax | # $() - is used like a backtick; more resistant to weird syntax | ||
<span id="piping-data-between-programs"></span> | |||
=== Piping data between programs === | |||
<ol> | <ol style="list-style-type: decimal;"> | ||
<li>programs on a UNIX system frequently use pipes to redirect each | <li>programs on a UNIX system frequently use pipes to redirect each other’s output.</li> | ||
<li>for instance you might use a text-filtering command on output from a file reading command.</li> | <li>for instance you might use a text-filtering command on output from a file reading command.</li> | ||
<li>allows small programs to operate on a flow of information in a row</li> | <li>allows small programs to operate on a flow of information in a row</li> | ||
<li> | <li><p>- this is an unnamed pipe</p></li> | ||
<li>mkfifo - allows for the creation of named pipes | <li>mkfifo - allows for the creation of named pipes | ||
<ol style="list-style-type: lower-alpha;"> | |||
<li><code>mkfifo fifo1</code></li> | |||
<li><code>ls -l > fifo1</code></li> | |||
<li><code>cat < fifo1</code></li></ol> | |||
</li> | |||
<li>tee - read from standard input and write to standard output and files | <li>tee - read from standard input and write to standard output and files | ||
<ol style="list-style-type: lower-alpha;"> | |||
<li>splits standard input so that is it is both displayed via standard output and also captured to one of more files</li> | |||
<li>allows for logging and redirection while the process is also shown on screen</li> | |||
<li>overwrites to a file as per ‘>’ by default rather than like ‘>>’</li> | |||
<li>example | |||
<ul> | |||
<li>echo $PATH | tee file1 file2</li> | |||
<li>file1, file2, and stdoutput all have same information</li></ul> | |||
</li></ol> | |||
</li> | |||
<li>find - searches for files in directory hierarchy</li> | <li>find - searches for files in directory hierarchy</li> | ||
<li>xargs - builds a command from standard input, using command name and arguments to that command and then executes the command on each line of standard input | <li>xargs - builds a command from standard input, using command name and arguments to that command and then executes the command on each line of standard input | ||
<ol style="list-style-type: lower-alpha;"> | |||
<li>example: find / - user darren | xargs -d %rm</li></ol> | |||
</li> | |||
<li>file - returns the type of file and metadata information regarding files</li> | <li>file - returns the type of file and metadata information regarding files</li> | ||
<li>join - combines two text files by matching the contents of specified fields | <li>join - combines two text files by matching the contents of specified fields | ||
<ol style="list-style-type: lower-alpha;"> | |||
<li>by default, join uses spaces as the limiters (unless -t CHAR is given)</li> | |||
<li>example: join -1 3 -2 2 | |||
<ul> | |||
<li>this joins the third field of the first file and the second field of the second file</li></ul> | |||
</li></ol> | |||
</li> | |||
<li>paste A. Paste is like cat rotated 90 degrees. When operating on multiple files, it will merge the lines of the files.</li> | <li>paste A. Paste is like cat rotated 90 degrees. When operating on multiple files, it will merge the lines of the files.</li> | ||
<li>expand - converts tabs to spaces A. by default assumes you want 8 spaces per tab; -t changes B. unexpand - converts spaces to tabs</li> | <li>expand - converts tabs to spaces A. by default assumes you want 8 spaces per tab; -t changes B. unexpand - converts spaces to tabs</li> | ||
<li>od - dump files into octal and other formats A. default usage is dumping binary file into octal binary format B. -x for 2byte hex output</li> | <li>od - dump files into octal and other formats A. default usage is dumping binary file into octal binary format B. -x for 2byte hex output</li> | ||
<li>sort - sorts lines of text files A. writes sorted concatenation of all files as output B. by default, uses first column C. Important options 1. -f : ignore case 2. -m : sort by three digit month abbreviation; ex, jan 3. -n : numeric sort 4. -v : sort by versio 5. -h : compare human readable numbers 6. -r : reverses the sort 7. -R : sorts randomly but groups like files 8. -d : dictionary-order 9. -k : sort via key, sorts by a field number, can be comma-seperated listq D. the opposite of this is | <li>sort - sorts lines of text files A. writes sorted concatenation of all files as output B. by default, uses first column C. Important options 1. -f : ignore case 2. -m : sort by three digit month abbreviation; ex, jan 3. -n : numeric sort 4. -v : sort by versio 5. -h : compare human readable numbers 6. -r : reverses the sort 7. -R : sorts randomly but groups like files 8. -d : dictionary-order 9. -k : sort via key, sorts by a field number, can be comma-seperated listq D. the opposite of this is ‘shuf’</li> | ||
<li>split - splits a file into 2 or more files A. needs to have an output file prefix B. where do you want to split 1. -b : size in bytes 2. -l : lines C. example: split -l 2 listening1.txt numbers -> output is: | <li>split - splits a file into 2 or more files A. needs to have an output file prefix B. where do you want to split 1. -b : size in bytes 2. -l : lines C. example: split -l 2 listening1.txt numbers -> output is: ‘numbersaa’ and ‘numbersab’ D. by default: splits every 10,000 lines</li> | ||
<li>tr - trnaslate or delete characters A. changes individual characters coming in through standard input and outputs via standard output B. options 1. [:lower:] - all lower case chars 2. [:upper:] - all upper case chars 3. [::]</li> | <li>tr - trnaslate or delete characters A. changes individual characters coming in through standard input and outputs via standard output B. options 1. [:lower:] - all lower case chars 2. [:upper:] - all upper case chars 3. [::]</li> | ||
<li>cut - extracts portions of lines of input and prints out as output A. used to remove items within lines, delimited by spaces or tabs B. options 1. -b : cuts the specified list by byte 2. -c : cuts the specified characters 3. -f : cuts specified list of fields from input 4. -d : specifies the delimiter for fields C. EXAMPLE: cut -f 7 -d : /etc/passwd</li> | <li>cut - extracts portions of lines of input and prints out as output A. used to remove items within lines, delimited by spaces or tabs B. options 1. -b : cuts the specified list by byte 2. -c : cuts the specified characters 3. -f : cuts specified list of fields from input 4. -d : specifies the delimiter for fields C. EXAMPLE: cut -f 7 -d : /etc/passwd</li> | ||
<li>uniq - take an input list and report or omit repeated lines A. only if they are next to each other, so you have to sort first B. EXAMPLE: cut -f 7 -d : /etc/passwd | sort | uniq</li></ol> | <li>uniq - take an input list and report or omit repeated lines A. only if they are next to each other, so you have to sort first B. EXAMPLE: cut -f 7 -d : /etc/passwd | sort | uniq</li></ol> | ||
<span id="tar-and-packaging"></span> | <span id="tar-and-packaging"></span> | ||
=== Tar and Packaging === | === Tar and Packaging === | ||
# tar A. the name | # tar A. the name ‘tar’ comes from its original purpose: Tape ARchive B. you can bundle files as .tar, ‘tarballs’, which contain a series of file entries and terminating bytes between files C. each entry has a file descriptor and the binary contents of the file D. file descriptor, or header, contains the name of a file, a checksum of that file, and permissions data E. does not compress data, but instead keeps it in one file F. tar options: -c: Creates a tar archive -v: Verbose output -x: eXtract from a tar archive -f: specify a Filename -A: Appends two archives together -M: create or extract a multivolume archive -j: specify bzip2 compression -J: specify xz compression -z: specify gzip compression –lzma: specify lzma compression -t: Test an archive | ||
<span id="more-unix-commands"></span> | <span id="more-unix-commands"></span> | ||
Line 352: | Line 166: | ||
# nl - line number A. nl does count whitespace by default B. identical to cat -b (but not cat -n) | # nl - line number A. nl does count whitespace by default B. identical to cat -b (but not cat -n) | ||
# pr - prepare a file for printing A. includes header, footer, and page breaks B. create colummns: pr -3 file.txt C. -l sets the length of lines D. -o choose the header text | # pr - prepare a file for printing A. includes header, footer, and page breaks B. create colummns: pr -3 file.txt C. -l sets the length of lines D. -o choose the header text | ||
# regular expressions - sed and awk A. simliar to wildcard expansion: ex; ls ''.txt B. grep and sed are two examples of regEx programs C. unix system programs have two different forms of regular expressions: basic and extended D. types of expresssions: 1. exact text match: grep | # regular expressions - sed and awk A. simliar to wildcard expansion: ex; ls ''.txt B. grep and sed are two examples of regEx programs C. unix system programs have two different forms of regular expressions: basic and extended D. types of expresssions: 1. exact text match: grep “blah” returns all lines with “blah” 2. bracketed expressions: b[aeiou]g - returns bag, beg, big, bog, bug 3. bracketed with range: a[2-4]z - returns a2z, a3z, a4z 4. matching single char: a.z (allows for anything between a and z) 5. matching begin/end of line: ^ beginning, $ end 6. any of these can be combined with the wildcard: '' ex: A.* Lincoln (with 0 or more occurances of a pattern.) ex: A[ae]* Lincoln (with 0 or more occurances of a-e after A. 7. matching at least one of: + symbol ex. A[ae]+ Lincoln - returns at least Aa or Ae Lincoln 8. multiple possibilities or: | needs to be set up with ’ ex. ‘Darren (F|Fredrick) Seifert’ 9. grep -E invokes regex functionality | ||
<span id="shell-scripting"></span> | <span id="shell-scripting"></span> | ||
Line 364: | Line 178: | ||
# Print out hello world | # Print out hello world | ||
echo 'Hello World!'</pre> | echo 'Hello World!'</pre> | ||
<ol start="3" style="list-style-type: decimal;"> | |||
<li>When you want to run a shell script, it needs to be made executable before it is an executable file: ‘# chmod u+x ./file.sh’</li> | |||
<li>Can be invoked with ./file.sh</li> | |||
<li>Alternative routes: A. bash file.sh works whether or not #!/bin/bash or chmod +x has been given B. exec file.sh is used to run scripts from within other scripts and to stop the tty instance after it is done (used to run scripts within scripts)</li> | |||
<li>Passing Parameters A. much like C++ programs, bash scripts can be passed parameters B. accessing these, the dollar sign is used, much like PHP or Perl, to call the number of the parameter 1. $0 - the name of the running script itself 2. $1 - the first parameter 3. and so on… 4. $? - returns the exit status of the last run command 5. $$ - returns the process ID (PID) of the current script 6. $USER - returns the username of the user running the script 7. $SECONDS - returns the runtime of the current script 8. $RANDOM - generates a random number (int 0 - 32000) 9. $LINENO - current line of script C. creation of new variables 1. variablename=value # no spaces ever 2. if you do want spaces, they must be escaped by quotes 3. double quotes recommended for variable substitution later 4. for instance varname=“value $1” allows for insertion of other variables into variables whereas varname=’value <math display="inline">1' will not work D. saving output into variables 1. output from programs can be saved into a variable 2. example: curdir=</math>(pwd) will run pwd and return the standard output as a variable 3. if you want to capture the standard error for a command, it can be saved as <code>error=$(</tmp/error)</code> 4. alternative use: curdir=<code>pwd</code> note the backtics</li></ol> | |||
<pre># foo.sh | <pre># foo.sh | ||
Line 376: | Line 191: | ||
echo "Details for $2" | echo "Details for $2" | ||
ls -l $2</pre> | ls -l $2</pre> | ||
* handling user input and output can use | * handling user input and output can use ‘echo’ to create interactive scripts: | ||
<pre># plususer.sh | <pre># plususer.sh | ||
Line 408: | Line 223: | ||
var2=bleh | var2=bleh | ||
echo $0 :: var1 : $var1 , var2 : $var2</pre> | echo $0 :: var1 : $var1 , var2 : $var2</pre> | ||
Latest revision as of 18:14, 21 October 2022
- Introduction to Unix
Filesystem
- Directories
- first and bottom of the directory of the file structure tree is called root /
- /root in addition, there is a file directory, within the root directory called /root (“slash root”), which acts as root’s home directory
- /home normal users’ directories
- /boot contains the boot directory, including kernel, initial RAMdisk, and bootloader
- /dev lists all devices of the computer
- /etc configuration files for nearly all programs that need a state
- /tmp temporary files for the system
- /var variable-length files used by programs
- /var/spool old-school webmail location
- /var/www location of http server files
- /usr files available to users, including binaries for daily user tasks in /usr/bin
- /sbin binaries used only by root
- /bin binaries for the system everyone needs to use
- Commands
- cd change directories
- ~ alias used to shortcut the current user directory
- Ctrl+R search history of commands in
bash
; also available as output fromhistory
- !! runs the previous command
- ls list in the contents of current directory
- .: the current directory
- .. : go up one directory#User and Group Configuration
The Shell explained
- A shell is a command interpreter where the user interacts with the system via command line interface (CLI)
- Different shells currently in-use A. Bourne-Again SHell - located at
/bin/bash
is the main shell and is used was a default on most Linux systems B. sh - The Bourne Shell, old school version does not require any libraries. Rarely seen as a user-interface shell; typically a symbolic link to another shell binary. Created by Stephen Bourne, first included in Unix V7 C. tcsh Tee Shell. Based on the earlier C-shell from Unix. Has some additional features built into it. D. csh C-shell, a BSD component not often seen on Linux. Created by Bill Joy at UC Berkeley. E. ksh Korn shell from Bell Labs in the 1980s. The idea was to incorporate features from the Bourne Shell along with C-programming syntax. F. zsh 1990s, incorporates additional ideas into ksh. - Built-in Commands A.
cd
changes directory -cd ~
orcd /home/user
are equivalent B.ls -
lists files in a directory - default behavior just lists files --a
lists all files including hidden ones --l
long format --d
just directories --F
uses a special character to rep filetypes --R
recursive listing C. Single characters are substituted by?
, wildcards by*
D. Exiting the system -exit
exits the current shell -logout
only works for login shells E. Remote access - ssh or secure shell provides the basic command to create a shell across an encrypted pipe -ssh username@remote.host
logs in as user username on a computer remote.host - several programs provide ssh access as a feature, such as PuTTY - scp or secure copy provides file transfer over ssh -scp /path/to/localmachine/ username@remote.host:/path/to/destination
-scp username@remote.host:/path/from/remotemachine /path/to/localdestination
Man pages and getting help
- displays the manual pages for any packages on the system
- typing
[/]
will allow you to search through the page - allows you to move via
[spacebar]
or[pgup/pgdn]
man
is broken up into seperate chapters:man 5
would show chapter 5- executable programs and shell
- system calls
- library calls
- device calls
- file formats
- games
- misc. (macro packages)
- sys admin commands
- kernel routines
man -k *
: to find any pages matching*
- most programs also allow for
--help
or-?
to be passed for a quick rundown on commands and usage
Unix filesystem permissions
- filesystem permissions saved as a series of 3 bit numbers
- format:
d,rwx,rwx,rwx
for read-write-executed
- denotes a Directory or file- first triad is for the User of the file
- second triad is for Group members of the file
- third triad is for Everyone else
- Read-write-execute for files
- Read allows the file to be opened
- Write allows for the file to be edited
- Execute allows for the file to run as a program
- Read-write-execute for directories
- Read allows for the directory to view filenames in the directory
- Write allows files to be added or deleted from a dir
- Execute allows the directory to be visited, “permission to traverse through”; must have this to execute programs within a directory
chmod
changes file and directory permissions- two modes for chmod
- symbolic mode
chmod {u,g,o}[+,-,=]{r,w,x}
- absolute mode - chmod 777 /path/to/file` * 0 no permissions * 1 execute * 2 write * 4 read F. chown - change file owner and group
- chown user /foo - changes foo’s owner to user
- chown root:root /foo - changes foo’s owner to root, also changes foo’s group to root G. echo - displays any text you enter after the command
- primarially used in shell scripts or ‘programs’ that utilize a combination of of built-in commands, variables, and executables to complete tasks
- variables reached from echo by invoking the variable name as $NAME
- symbolic mode
- two modes for chmod
Redirection symbols
- prog > - redirects std output to the file, truncating and rewriting
- prog 2> - redirect std error to the file, truncating and rewriting
- &> - redirects both std output and error to file
>>
- non-destructively appends to end of file rather than rewriting- "backtick" process commands in the middle of another command a. A tool similar to xargs b. text within backticks are treated as a separate command line who's results are substituted on this command line c. things inside of backticks are attempted to be used as a command d. example: rm
find / - user darren `- $() - is used like a backtick; more resistant to weird syntax
Piping data between programs
- programs on a UNIX system frequently use pipes to redirect each other’s output.
- for instance you might use a text-filtering command on output from a file reading command.
- allows small programs to operate on a flow of information in a row
- this is an unnamed pipe
- mkfifo - allows for the creation of named pipes
mkfifo fifo1
ls -l > fifo1
cat < fifo1
- tee - read from standard input and write to standard output and files
- splits standard input so that is it is both displayed via standard output and also captured to one of more files
- allows for logging and redirection while the process is also shown on screen
- overwrites to a file as per ‘>’ by default rather than like ‘>>’
- example
- echo $PATH | tee file1 file2
- file1, file2, and stdoutput all have same information
- find - searches for files in directory hierarchy
- xargs - builds a command from standard input, using command name and arguments to that command and then executes the command on each line of standard input
- example: find / - user darren | xargs -d %rm
- file - returns the type of file and metadata information regarding files
- join - combines two text files by matching the contents of specified fields
- by default, join uses spaces as the limiters (unless -t CHAR is given)
- example: join -1 3 -2 2
- this joins the third field of the first file and the second field of the second file
- paste A. Paste is like cat rotated 90 degrees. When operating on multiple files, it will merge the lines of the files.
- expand - converts tabs to spaces A. by default assumes you want 8 spaces per tab; -t changes B. unexpand - converts spaces to tabs
- od - dump files into octal and other formats A. default usage is dumping binary file into octal binary format B. -x for 2byte hex output
- sort - sorts lines of text files A. writes sorted concatenation of all files as output B. by default, uses first column C. Important options 1. -f : ignore case 2. -m : sort by three digit month abbreviation; ex, jan 3. -n : numeric sort 4. -v : sort by versio 5. -h : compare human readable numbers 6. -r : reverses the sort 7. -R : sorts randomly but groups like files 8. -d : dictionary-order 9. -k : sort via key, sorts by a field number, can be comma-seperated listq D. the opposite of this is ‘shuf’
- split - splits a file into 2 or more files A. needs to have an output file prefix B. where do you want to split 1. -b : size in bytes 2. -l : lines C. example: split -l 2 listening1.txt numbers -> output is: ‘numbersaa’ and ‘numbersab’ D. by default: splits every 10,000 lines
- tr - trnaslate or delete characters A. changes individual characters coming in through standard input and outputs via standard output B. options 1. [:lower:] - all lower case chars 2. [:upper:] - all upper case chars 3. [::]
- cut - extracts portions of lines of input and prints out as output A. used to remove items within lines, delimited by spaces or tabs B. options 1. -b : cuts the specified list by byte 2. -c : cuts the specified characters 3. -f : cuts specified list of fields from input 4. -d : specifies the delimiter for fields C. EXAMPLE: cut -f 7 -d : /etc/passwd
- uniq - take an input list and report or omit repeated lines A. only if they are next to each other, so you have to sort first B. EXAMPLE: cut -f 7 -d : /etc/passwd | sort | uniq
Tar and Packaging
- tar A. the name ‘tar’ comes from its original purpose: Tape ARchive B. you can bundle files as .tar, ‘tarballs’, which contain a series of file entries and terminating bytes between files C. each entry has a file descriptor and the binary contents of the file D. file descriptor, or header, contains the name of a file, a checksum of that file, and permissions data E. does not compress data, but instead keeps it in one file F. tar options: -c: Creates a tar archive -v: Verbose output -x: eXtract from a tar archive -f: specify a Filename -A: Appends two archives together -M: create or extract a multivolume archive -j: specify bzip2 compression -J: specify xz compression -z: specify gzip compression –lzma: specify lzma compression -t: Test an archive
More Unix Commands
- fmt - format A. some files have crazy long lines of text B. reformats to a certain -width C. by default, truncates after 75 characters and cleans up paragraphs D. anything with 2+ blankline removed, and adds indention E. -s : prevents truncation of paragraphs
- nl - line number A. nl does count whitespace by default B. identical to cat -b (but not cat -n)
- pr - prepare a file for printing A. includes header, footer, and page breaks B. create colummns: pr -3 file.txt C. -l sets the length of lines D. -o choose the header text
- regular expressions - sed and awk A. simliar to wildcard expansion: ex; ls .txt B. grep and sed are two examples of regEx programs C. unix system programs have two different forms of regular expressions: basic and extended D. types of expresssions: 1. exact text match: grep “blah” returns all lines with “blah” 2. bracketed expressions: b[aeiou]g - returns bag, beg, big, bog, bug 3. bracketed with range: a[2-4]z - returns a2z, a3z, a4z 4. matching single char: a.z (allows for anything between a and z) 5. matching begin/end of line: ^ beginning, $ end 6. any of these can be combined with the wildcard: ex: A.* Lincoln (with 0 or more occurances of a pattern.) ex: A[ae]* Lincoln (with 0 or more occurances of a-e after A. 7. matching at least one of: + symbol ex. A[ae]+ Lincoln - returns at least Aa or Ae Lincoln 8. multiple possibilities or: | needs to be set up with ’ ex. ‘Darren (F|Fredrick) Seifert’ 9. grep -E invokes regex functionality
Shell Scripting
- The first line of a shell script tells which program to use to interpret the contents: #!/bin/bash or another shell A. the first two characters are (#!) are special code to tell the kernel this is a script - crunchbang, shebang, hashbang, poundbang B. the remaining lines beginning with # are considered comments
# this is a comment
- Classic Hello World
# hello.sh #!/bin/bash # Print out hello world echo 'Hello World!'
- When you want to run a shell script, it needs to be made executable before it is an executable file: ‘# chmod u+x ./file.sh’
- Can be invoked with ./file.sh
- Alternative routes: A. bash file.sh works whether or not #!/bin/bash or chmod +x has been given B. exec file.sh is used to run scripts from within other scripts and to stop the tty instance after it is done (used to run scripts within scripts)
- Passing Parameters A. much like C++ programs, bash scripts can be passed parameters B. accessing these, the dollar sign is used, much like PHP or Perl, to call the number of the parameter 1. $0 - the name of the running script itself 2. $1 - the first parameter 3. and so on… 4. $? - returns the exit status of the last run command 5. $$ - returns the process ID (PID) of the current script 6. $USER - returns the username of the user running the script 7. $SECONDS - returns the runtime of the current script 8. $RANDOM - generates a random number (int 0 - 32000) 9. $LINENO - current line of script C. creation of new variables 1. variablename=value # no spaces ever 2. if you do want spaces, they must be escaped by quotes 3. double quotes recommended for variable substitution later 4. for instance varname=“value $1” allows for insertion of other variables into variables whereas varname=’value (pwd) will run pwd and return the standard output as a variable 3. if you want to capture the standard error for a command, it can be saved as
error=$(</tmp/error)
4. alternative use: curdir=pwd
note the backtics
# foo.sh #!/bin/bash # An example script cp $1 $2 #verify it worked echo "Details for $2" ls -l $2
- handling user input and output can use ‘echo’ to create interactive scripts:
# plususer.sh #!/bin/bash echo -n 'Please enter user name for account:' read username # this saves the stdin input as username read -p 'Username' $username # -n no-newline echo $username read -sp 'Password:' password # -s stealth
- scripts can also handle files as standard input like other commands
# salesreport.sh #!/bin/bash #a sales report generated from standard input echo Data Summary: cat /dev/stdin | cut -d ' ' -F2,3 | sort
- variables can be made global via export
# script.sh #!/bin/bash var1=blah var2=foo echo $0 :: var1 : $var1 , var2 : $var2 export var1 exec ./script2 echo $0 :: var1 : $var1 , var2 : $var2
script2.sh #!/bin/bash echo $0 :: var1 : $var1 , var2 : $var2 var1=flop var2=bleh echo $0 :: var1 : $var1 , var2 : $var2