Unix Commands

¡Supera tus tareas y exámenes ahora con Quizwiz!

Typical command structure

COMMANDNAME [FLAGS] [PARAMETERS] FLAGS: Commands often accept one or more flags after command name. Each flag starts with "-" or with "--", and is separated from other flags by spaces. Individual flags often may be combined with a single "-". ls -l -a ls -la PARAMETERS: Commands often accept one or more parameters. Parameters are often pathnames representing files or directories. Parameters are separated by spaces. cp -a /home/carol/Mail /home/carol/Mail-Backup

Environment variables You can teach your shell to remember things for later using environment variables. For example under the bash shell: export CASROOT=/usr/local/CAS3.0 export LD_LIBRARY_PATH=$CASROOT/Linux/lib By prefixing $ to the variable name, you can evaluate it in any command: cd $CASROOT echo $CASROOT printenv CASROOT

Environment variables export CASROOT=/usr/local/CAS3.0 Defines the variable CASROOT with the value /usr/local/CAS3.0. export LD_LIBRARY_PATH=$CASROOT/Linux/lib Defines the variable LD_LIBRARY_PATH with the value of CASROOT with /Linux/lib appended, or /usr/local/CAS3.0/Linux/lib By prefixing $ to the variable name, you can evaluate it in any command: cd $CASROOT Changes your present working directory to the value of CASROOT echo $CASROOT Prints out the value of CASROOT, or /usr/local/CAS3.0 printenv CASROOT Does the same thing in bash and some other shells.

tail myfile.txt tail myfile.txt -n 100 tail -f myfile.txt tail -f access.log | grep 24.10.160.10

Examples tail myfile.txt The above example would list the last 10 (default) lines of the file myfile.txt. tail myfile.txt -n 100 The above example would list the last 100 lines in the file myfile.txt. tail -f myfile.txt This next example displays the last 10 lines and then update the file as new lines are being added. This is a great command to use to watch log files or logs in real-time. tail -f access.log | grep 24.10.160.10 Finally, if you're trying to view a file such as the Apache access log file that is updated frequently you can pipe its output through the grep command to filter out only the content you want. In this above example, we're watching the access.log for any IP address of 24.10.160.10

Another very useful type of redirection, particularly in shell scripts, is the "______" document. The syntax looks like <<'EOF' data... data... data... data... EOF For example: $ cat <<'EOF' > this is stuff that > we want to echo > until the > EOF this is stuff that we want to echo until the $

Here

In addition to environmental variables which are passed on to child processes, you can create local variables. % export x=12 % y=15 % echo x = $x and y = $y x = 12 and y = 15 % bash % echo x = $x and y = $y x = 12 and y =

In addition to environmental variables which are passed on to child processes, you can create local variables. % export x=12 # create an environmental variable % y=15 # create a local variable % echo x = $x and y = $y x = 12 and y = 15 % bash # now create a new child process % echo x = $x and y = $y x = 12 and y =

What is a buffer?

In computer science, a buffer is a region of a physical memory storage used to temporarily hold data while it is being moved from one place to another. Typically, the data is stored in a buffer as it is retrieved from an input device (such as a mouse) or just before it is sent to an output device (such as speakers). However, a buffer may be used when moving data between processes within a computer. This is comparable to buffers in telecommunication. Buffers can be implemented in a fixed memory location in hardware—or by using a virtual data buffer in software, pointing at a location in the physical memory. In all cases, the data stored in a data buffer are stored on a physical storage medium. A majority of buffers are implemented in software, which typically use the faster RAM to store temporary data, due to the much faster access time compared with hard disk drives. Buffers are typically used when there is a difference between the rate at which data is received and the rate at which it can be processed, or in the case that these rates are variable, for example in a printer spooler or in online video streaming.

One of the great strengths of Unix has been its very clean "___ ______". You can easily send a process into the background with a simple ampersand: % ( ls -R / | wc -l ) 2>/dev/null & % jobs [1]+ Running ( ls -R / | wc -l ) 2> /dev/null &

Job control

less COMMANDS: less

Less is a program similar to more (1), but which allows backward movement in the file as well as forward movement. Also, less does not have to read the entire input file before starting, so with large input files it starts up faster than text editors like vi (1). Less uses termcap (or terminfo on some systems), so it can run on a variety of terminals. There is even limited support for hardcopy terminals. (On a hardcopy terminal, lines which should be printed at the top of the screen are prefixed with a caret.)

Macros Default Keystroke(s) in emacs ^X( ^X) ^Xe Editing Default Keystroke(s) in emacs ^K ^O ^T ^W ESC W ^Y ESC x query-replace something RET otherthing ESC x query-replace-regexp regexp RET otherthing ^Y ESC x undo

Macros Default Keystroke(s) ^X( Start learning a macro ^X) End learning a macro ^Xe Execute a macro Editing Default Keystroke(s) ^K "Kill" from this point up to the end of line (saved in "kill" ring) ^O "Open" a new line immediately below current, "open" a new line immediately above current ^T "Transpose" the current character and the next one ^W Cut the current region (saved in "kill" ring) ESC W Copy the current region (saved in "kill" ring) ^Y Paste from latest item in "kill" ring ESC x query-replace something RET otherthing Replace something with otherthing ESC x query-replace-regexp regexp RET otherthing Replace regexp with otherthing (powerful!) ^Y Paste from latest item in "kill" ring ESC x undo Undo

sort sort -b sort -c sort -d sort -f sort -g sort -i sort -k sort -m sort -M

Sorts the lines in a text file. Syntax sort [options]... [file] -b Ignores spaces at beginning of the line. -c Check whether input is sorted; do not sort -d Uses dictionary sort order and ignores the punctuation. -f Ignores caps -g Ccompare according to general numerical value -i Ignores nonprinting control characters. -k Start a key at POS1, end it at POS2 (origin 1) -m Merges two or more input files into one sorted output. -M Treats the first three letters in the line as a month (such as may.)

The following are permissions. What is the leading "-" or "d" What is "r" What is "w" What is "x" $ ls -l /etc/hosts -rw-r--r-- 1 root root 905 Jun 16 2010 /etc/hosts $ ls -dl /etc drwxr-xr-x 105 root root 12288 Jan 15 14:24 /etc $ ls -l /usr/bin/emacs-21.4-x -rwxr-xr-x 2 root root 6649776 Apr 28 2011 /usr/bin/emacs-21.4-x $ ls -l /usr/bin/emacs lrwxrwxrwx 1 root root 19 Feb 8 2012 /usr/bin/emacs -> /usr/bin/emacs-21.4

The first component tells us about the item in the filesystem; an unadorned "-" indicates a regular file; "d" indicates a directory; "l" indicates a soft link; "s" indicates a socket; "c" is a character device file; "b" is a block device file; "p" is a named pipe. The next 3 characters indicate whether the owner of a file has (1) read (2) write or (3) execute permission (execute in the case of a directory means being able to cd into the directory). The second set of three characters indicate whether users in the same group as this item have read/write/execute permission; the last group of three indicates if all users (the "world") have read/write/execute permission.

chmod chmod u+x chmod g+x chmod o+x chmod a+o chmod a-x chmod g-r

To change permission chmod u+x # add execute permission for the user chmod g+x # add execute permission for group chmod o+x # add execute permission for other chmod a+x # add execute permission for all users chmod a-x # remove execute permission for all users chmod g-r # remove read permission for group

Copying with cp cp both.txt copy.txt cp -a both.txt cp -r .mozilla .mozilla-backup cp -a .mozilla .mozilla-exact

# copies, but uses new date and umask default permissions for new file # creates an exact copy # creates a new copy of a dir but times are new and permissions are umask-controlled # creates an exact copy of a dir

Removing files rm old.txt rm -rf .mozilla rm -rf / rmdir .somedir/

# removes file "old.txt" # remove a directory without complaining # remove everything in the system (bad idea!) # doesn't work unless .somedir/ is empty

What is the significance of dot files/directories?

"Dot" files generally are used by programs such as shells and windowing environments which have a need to have and retain complex state, such as startup commands and current information.

Metacharacters: 1.* 2. ? 3. [] 4.{} 5. ~

* matches any string of characters ? matches any one character [] matches a range of characters {} matches specific characters ~ expands to either your home directory (as unadorned initial ~), or $HOME for another user if ~user

ls -1 ls --help ls --version

-1 list one file per line --help display this help and exit --version output version information and exit

ls -G, ls --no-group ls -h, ls --human-readable ls -i, ls --inode ls -I, ls --ignore=PATTERN

-G, --no-group inhibit display of group information -h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G) -i, --inode print index number of each file -I, --ignore=PATTERN do not list implied entries matching shell PATTERN

touch -a, touch --time=access, touch --time=use touch -c,touch --no create touch -d, touch --date time touch -m, touch --time=mtime, touch --time=modify

-a, --time=atime, --time=access, --time=use Change the access time only. -c, --no-create Do not create files that do not exist. -d, --date time Use time (which can be in various common formats) instead of the current time. It can contain month names, timezones, `am' and `pm', etc. -m, --time=mtime, --time=modify Change the modification time only.

ls -c ls -C ls --color[=WHEN] ls -d, ls --directory ls -D, ls --dired ls -f ls --format=WORD

-c with -lt: sort by, and show, ctime (time of last modification of file status information) with -l: show ctime and sort by name otherwise: sort by ctime -C list entries by columns --color[=WHEN] control whether color is used to distinguish file types. WHEN may be `never', `always', or `auto' -d, --directory list directory entries instead of contents -D, --dired generate output designed for Emacs' dired mode -f do not sort, enable -aU, disable -lst --format=WORD across -x, commas -m, horizontal -x, long -l, single-column

pr -d pr -F pr -m

-d Produce output that is double-spaced; append an extra NEWLINE character following every NEWLINE character found in the input. -F Fold the lines of the input file. When used in multi-column mode (with the -a or -m options), lines will be folded to fit the current column's width; otherwise, they will be folded to fit the current line width (80 columns). -m Merge files. Standard output will be formatted so pr writes one line from each file specified by file , side by side into text columns of equal fixed widths, in terms of the number of column positions. Implementations support merging of at least nine file s.

pr -f pr -p

-f Use a FORMFEED character for new pages, instead of the default behavior that uses a sequence of NEW- LINE characters. Pause before beginning the first page if the standard output is associated with a terminal. -p Pause before beginning each page if the standard output is directed to a terminal (pr will write an ALERT character to standard error and wait for a carriage-return character to be read on /dev/tty).

pr -i [char][gap] pr -I lines pr -n [char][width]

-i [char][gap] In output, replace SPACE characters with TAB characters wherever one or more adjacent SPACE characters reach column positions gap+1, 2*gap+1, 3*gap+1, and so forth. If gap is 0 or is omitted, default TAB settings at every eighth column position are assumed. If any non-digit character, char, is specified, it will be used as the output TAB character. -l lines Override the 66-line default and reset the page length to lines. If lines is not greater than the sum of both the header and trailer depths (in lines), pr will suppress both the header and trailer, as if the -t option were in effect. -n [char][width] Provide width-digit line numbering (default for width is 5). The number will occupy the first width column positions of each text column of default output or each line of -m output. If char (any non-digit character) is given, it will be appended to the line number to separate it from whatever follows (default for char is a TAB character).

ls -k ls -m ls -N, ls --literal ls -o

-k like --block-size=1K -m fill width with a comma separated list of entries -N, --literal print raw entry names (don't treat e.g. control characters specially)

who -m who -n who -p who -q who -r who -s who -t who -T

-m Output only information about the current terminal. -n x Take a numeric argument, x, which specifies the number of users to display per line. x must be at least 1. The -n option may only be used with -q. -p List any other process which is currently active and has been previously spawned by init . The name field is the name of the program executed by init as found in /sbin/inittab. The state, line , and idle fields have no meaning. The comment field shows the id field of the line from /sbin/inittab that spawned this process. -q (quick who ) display only the names and the number of users currently logged on. When this option is used, all other options are ignored. -r Indicate the current run-level of the init process. -s (default) List only the name, line, and time fields. -t Indicate the last change to the system clock (using the date utility) by root. See su and date. -T Same as the -s option, except that the state field is also written.

sort -n sort -o sort -r sort -s sort -t sort -T sort -u sort -z

-n Sorts by the beginning of the number at the beginning of the line. -o Write result to FILE instead of standard output -r Sorts in reverse order -s Stabilize sort by disabling last-resort comparison -t Use SEP instead of non-blank to blank transition -T Uuse DIR for temporaries, not $TMPDIR or /tmp; multiple options specify multiple directories -u If line is duplicated only display once -z End lines with 0 byte, not newline Examples sort -r file.txt Sort the file, file.txt in reverse order.

pr -o offset pr -s [char] pr -w [width]

-o offset Each line of output will be preceded by offset <space>s. If the -o option is not specified, the default offset is 0. The space taken will be in addition to the output line width (see -w option below). -s [char] Separate text columns by the single character char instead of by the appropriate number of SPACE characters (default for char is the TAB character). -w [width] Set the width of the line to width column positions for multiple text-column output only. If the -w option is not specified and the -s option is not specified, the default width is 72. If the -w option is not specified and the -s option is specified, the default width is 512. For single column output, input lines will not be truncated.

ls -q, ls --hide-control-chars, ls --show-control-chars ls -Q, ls --quote-name, ls -r, ls --reverse ls -R, ls --recursive

-q, --hide-control-chars print ? instead of non graphic characters --show-control-chars show non graphic characters as-is (default unless program is `ls' and output is a terminal) -Q, --quote-name enclose entry names in double quotes -r, --reverse reverse order while sorting -R, --recursive list subdirectories recursively

pr -r pr -t pr -e [char][gap] pr -h header

-r Write no diagnostic reports on failure to open files. -t Write neither the five-line identifying header nor the five-line trailer usually supplied for each page. Quit writing after the last line of each file without spacing to the end of the page -e [char][gap] Expand each input TAB character to the next greater column position specified by the formula n *gap+1, where n is an integer >0. If gap is 0 or is omitted, it defaults to 8. All TAB characters in the input will be expanded into the appropriate number of SPACE characters. If any non-digit character, char, is specified, it will be used as the input tab character. -h header Use the string header to replace the contents of the file operand in the page header.

touch -r, touch --file refeerence-file touch -t touch --help touch --version

-r, --file reference-file Use the times of reference-file instead of the cur- rent time. -t MMDDhhmm[[CC]YY][.ss] Use the argument (months, days, hours, minutes, optional century and years, optional seconds) instead of the current time. --help Print a usage message on standard output and exit successfully. --version Print version information on standard output then exit successfully.

ls -s, ls --size ls -S

-s, --size print size of each file, in blocks -S sort by file size

ls -t ls -T, ls --tabsize=COLS ls -u ls -U ls -v ls -w, ls --width=COLS ls -x ls -X

-t order via modified time rather than by name -T, --tabsize=COLS assume tab stops at each COLS instead of 8 -u with -lt: sort by, and show, access time with -l: show access time and sort by name otherwise: sort by access time -U do not sort; list entries in directory order -v sort by version -w, --width=COLS assume screen width instead of current value -x list entries by lines instead of by columns -X sort alphabetically by entry extension

cp cp -a cp -i cp -r

..cp [-i][-R][-r] source destination Description: Copies the contents of a file or directory to another file or directory. Options: [-a] Archive mode; preserve ownership, permissions, and any special attributes [-i] Ask before you replace [-r] Recursive copy Parameters: source - What you want to copy target - New location Example: cp .plan .plan.backup cp -r ~/public_html/* /tmp cp -a /home/carol /home/carol-backup

What directories would you expect to see on the "root"

./bin ./etc ./lib ./usr ./var ./temp ./boot ./dev ./home

1. What is the pathname for "root"? 2. Sub-directories identified by name, separated by "__" 3. Absolute pathnames always start with the "____" (i.e., the slash character) 4. _________ pathnames are from current directory. 5. "___" refers to directory above 6. "__" refers to the current working directory 7. ________ represents the absolute path to USER's home directory 8. "__" represents the absolute path to your own home directory

1. " / " 2. " / " 3. "root" 4. Relative Ex. "Mail/" "../../bin/emacs" 5. ".." 6. "." 7. ~USER 8. "~/"

What commands could you use to log out of your CS account or any other account

1. "exit" should work for any shell, and is usually your best choice 2. CTRL-D is very likely to work also 3. Most login shells also will accept "logout" 4. "bye" is sometimes accepted, but don't count on it

1. You can use the ____ command to join multiple files together. ways to join: cat 1776.calendar.txt 1752.calendar.txt > both.txt cat 17{76,52}.calendar.txt > both.txt # or with append cat 1776.calendar.txt > both.txt cat 1752.calendar.txt >> both.txt

1. >

1.________ pathnames simply give directions on how to get to a node (from root).

1. Absolute pathnames simply give directions on how to get to a node (from root). Ex. "/bin/emacs" "/home/carol/Mail/"

1. What are the three critical components of a computer? 2. A CPU keeps it internal state in ___________. 3. A ________ is a snapshot of all of the 0s and 1s (bits) in the registers and the RAM at a given instant. The clock sets itself off these states. 4. An "________ ________" provides a mediator of hardware and software resources; programs now make __________ to the operating system for access to hardware rather than just directly accessing the hardware. This mediation allows ___________, and we can have the idea of individual "processes" which can exist simultaneously.

1. CPUs, RAM, and persistent storage (also called "secondary storage", "non-volatile storage", or even the very old term "backing store".) 2. Registers 3. State 4. Operating System, requests, concurrency

1. How do we solve this dilemma? A fundamental construct of most shells is the idea of a _____ variable. The _____ variable simply lists a number of directories that we will search for commands that have no explicit path information.

1. How do we solve this dilemma? A fundamental construct of most shells is the idea of a PATH variable. The PATH variable simply lists a number of directories that we will search for commands that have no explicit path information. $ echo $PATH /usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin This PATH means that each of the paths /usr/kerberos/bin, /usr/local/bin, /bin, and /usr/bin will be searched when an unadorned command name is invoked.

How could you communicate with other users on the system?

1. Mail 2. write 3. wall 4. talk 5. irc

1. In addition to simple file redirection, we have the concept of a "______", which is a creation of the kernel. It's actually just a simple "______". The syntax for creating a pipe between two processes is the vertical bar ("__"). sort < /etc/passwd | cut -d: -f 1 The tee: there's a nice program call tee that will let you intercept "__-____" data. $ sort < /etc/passwd | tee /tmp/sorted-passwd | cut -d: -f 1 $ cat /tmp/sorted-passwd

1. Pipe, Buffer, |, mid-pipe

1.________: The ``Bourne-Again SHell''. Introduced the ``readline'' library. A comprehensive overhaul of the original Bourne shell. 2.________: A decreasingly popular shell; designed to be more of a user interface than a scripting language. 3._________: The ``Practical Extraction and Report Language''. An amazing language in its own right; originally, early versions of Perl programs could have be mistaken for line noise, much as the old TECO editor commands. 4.__________: An increasingly popular programming language for system administration tasks. 5.___________: Another language that is recently getting attention is lua. It's fast and it's expressive; for some people, Perl's syntax is too cryptic, Python's is too inexpressive, but they find Lua more acceptable.

1. bash: The ``Bourne-Again SHell''. Introduced the ``readline'' library. A comprehensive overhaul of the original Bourne shell. 2. csh: A decreasingly popular shell; designed to be more of a user interface than a scripting language. 3. perl5: The ``Practical Extraction and Report Language''. An amazing language in its own right; originally, early versions of Perl programs could have be mistaken for line noise, much as the old TECO editor commands. 4. python: An increasingly popular programming language for system administration tasks. 5. lua: Another language that is recently getting attention is lua. It's fast and it's expressive; for some people, Perl's syntax is too cryptic, Python's is too inexpressive, but they find Lua more acceptable.

1. In the home directory you can expect for these files not to show up with a standard ls command are called _____ files. The ls ignores these files unless it is specified with ls -a. 2. ___________ ignores _____ files unless you explicitly include such a file. 3. So, _________ treats characters like "*", "?", "[]", "{}", and "~" as "___________"

1. dot 2. globbing, dot 3. globbing, metacharacters

1. Once we have the concept of a bytestream flowing through pipes, a natural metaphor for programs that modify the bytestream is to call them a "_______". There are a very large number of standard filters; What are some? 2. What does each filter do? cat base 64 diff grep/egrep/fgrep fmt/pr/nl head/tail less/more/pg cut/paste sort tr uniq spell xargs sed

1. filter ^^ cat -- "Catenate" a file. ^^ base64 -- Encode/decode "ASCII armor" ^^ diff -- Take the difference in two files ^^ grep/egrep/fgrep -- Apply regular expressions to a bytesream ^^ fmt/pr/nl -- Apply formatting to a file. ^^ head/tail -- Show the initial/final lines of a bytestream. ^^ less/more/pg -- Display one page at a time. ^^ cut/paste -- Excerpt and join input. ^^ sort -- Sort ^^ tr -- Transform a bytestream. ^^ uniq -- Find/Remove/Count unique lines in a bytestream. ^^ spell -- Find problematic spellings (ispell is great for interactively fixing spelling problems.) ^^ xargs -- Build and execute command lines from standard input ^^ sed -- The stream editor; arbitrarily modify lines in a bytestream.

1. Which is faster the mv command or the cp command? mv both.txt old.txt mv .mozilla .mozilla-2013-01-22

1. the mv command because it doesnt worry about the contents of the file like cp. Rather than doing anything with the contents mv just makes some minor changes to directory information. # notice that no option is necessary to specify directory

1. \ Shells generally have an "built-in" command called _______ which allows the user to specify the default permissions for newly created files. It actually specifies the complement of the default read-write permissions for newly created files. umask 077 umask 000 umask 022 umask 777

1. umask umask 077 # no one except the user can read or write new files umask 000 # anyone can read or write all new files (dangerous!) umask 022 # anyone can read but not write new files umask 777 # no one (not even the owner) can read or write new files

1. ____ has two main modes: move and insert. A variety of commands in ____ will put you into insert mode, but ESC is the way to get out! 2. You can use _______ to check your spelling. Ispell is very easy to use in emacs, more so than vi

1. vi, vi 2. spell

1. vi: It is almost always found somewhere on a Unix/Linux machine, often in the form of ____. 2. What are the advantages? 3. What are the disadvantages?

1. vim 2. vi advantage: It is simple to learn. It has two modes, ``motion'' and ``insert''. In the ``motion'' mode, the most important keys are just h: Move the cursor left. l: Move the cursor right. j: Move the cursor down. k: Move the cursor up. i: Go into ``insert'' mode. :: Execute an internal vi command of some sort. The most popular are w for "write", q for "quit", and quit! for "quit without saving anything". 3. vi disadvantage: While original vi is quite simple, it also is not very featureful. This isn't as much of a problem with the more recent incarnation vim, an updated version of vi. These extensions are not as logical as those emacs since they were created as an afterthought.

What commands could you use to see who else is on the system

1. who 2. w 3. users

the three redirection operators? What do all three do?

<,>, and >> The < operator lets you redirect standard in; for instance, you can do sort < /etc/passwd and this will have sort takes its standard in from the file /etc/passwd. As we have seen already, the > operator lets you redirect standard out; for instance, you can take the output of cal and save it: cal 1752 > /tmp/unusual-cal.txt Finally, you can use the >> operator to append data to a file: cal 1753 >> /tmp/unusual-cal.txt

Bourne shell, Korn shell, Bash, and others in that family have two primary prompt variables, PS1 and PS2. export PS1="_____" export PS2="_____ "

Bourne shell, Korn shell, Bash, and others in that family have two primary prompt variables, PS1 and PS2 export PS1="# " export PS2="... "

cd cd .. Ex. cd ../home/users/computerhope cd../../

Changes the directory. Syntax cd [directory] directory Name of the directory user wishes to enter. cd .. Used to go back one directory on the majority of all Unix shells. It is important that the space be between the cd and the .. Examples cd ../home/users/computerhope The above example would go back one directory and then go into the home/users/computerhope directory. cd ../../ Next, the above example would go back two directories.

Chmod What does each chmod do: chmod 755 file chown cliff file chown -R cliff dir

Changing file permissions and attributes chmod 755 file Changes the permissions of file to be rwx for the owner, and rx for the group and the world. (7 = rwx = 111 binary. 5 = r-x = 101 binary) chgrp user file Makes file belong to the group user. chown cliff file Makes cliff the owner of file. chown -R cliff dir Makes cliff the owner of dir and everything in its directory tree. You must be the owner of the file/directory or be root before you can do any of these things

cmp cmp -c cmp -i N cmp -l cmp -s cmp -v Ex. cmp file1.txt file2.txt

Compares two files and tells you what line numbers are different. Syntax cmp [-c] [-i N] [-l] [-s] [-v] firstfile secondfile -c Output differing bytes as characters. -i N Ignore differences in the first N bytes of input. -l Write the byte number (decimal) and the differing bytes (octal) for each difference. -s Write nothing for differing files; return exit statuses only. -v Output version info. firstfile First file that you wish to compare. secondfile Second file that you wish to compare to. Examples cmp file1.txt file2.txt Compares file1 to file2 and outputs results. Below is example of how these results may look. file.txt file2.txt differ: char 1011, line 112

rmdir rm --ignore-fail-on-non-empty rmdir - p, rmdir --parents rmdir -v, rmdir --verbose rmdir --version Ex. rmdir mydir rm -rf directory/

Deletes a directory. Syntax rmdir [OPTION]... DIRECTORY... --ignore-fail-on-non-empty ignore each failure that is solely because a directory is non-empty. -p, --parents Remove DIRECTORY and its ancestors. E.g., `rmdir -p a/b/c' is similar to `rmdir a/b/c a/b a'. -v, --verbose output a diagnostic for every directory processed. --version output version information and exit. Examples rmdir mydir Removes the directory mydir rm -rf directory/ Remove a directory, even if files existed within that directory.

tail tail -l tail -b tail -c tail -r tail -f tail -c number

Delivers the last part of the file. Syntax tail [+ number] [-l] [-b] [-c] [-r] [-f] [-c number | -n number] [file] -l Units of lines. -b Units of blocks. -c Units of bytes. -r Reverse. Copies lines from the specified starting point in the file in reverse order. The default for r is to print the entire file in reverse order. -f Follow. If the input-file is not a pipe, the program will not terminate after the line of the input-file has been copied, but will enter an endless loop, wherein it sleeps for a second and then attempts to read and copy further records from the input-file. Thus it may be used to monitor the growth of a file that is being written by some other process. -c number The number option-argument must be a decimal integer whose sign affects the location in the file, measured in bytes, to begin the copying: + Copying starts relative to the beginning of the file. - Copying starts relative to the end of the file. none Copying starts relative to the end of the file. The origin for counting is 1; that is, -c+1 represents the first byte of the file, -c-1 the last. -n number Equivalent to -c number, except the starting location in the file is measured in lines instead of bytes. The origin for counting is 1; that is, -n+1 represents the first line of the file, -n-1 the last. file Name of the file you wish to display

Name each Directory / /usr /usr/STRIM100

Directories: File and directory paths in UNIX use the forward slash "/" to separate directory names in a path. examples: / "root" directory /usr directory usr (sub-directory of / "root" directory) /usr/STRIM100 STRIM100 is a subdirectory of /usr

head head -n head filename head -15 myfile.txt

Displays the first ten lines of a file, unless otherwise stated. Syntax head [-number | -n number] filename -n The number of the you want to display. filename The file that you want to display the x amount of lines of. Examples head -15 myfile.txt Display the first fifteen lines of myfile.txt.

diff diff -b diff -i diff -t diff -w diff -c

Displays two files and prints the lines that are different. Syntax diff [-b] [-i] [-t] [-w] [-c] [-C] [-e] [-f] [-h] [-n] [-D string] [-l] [-r] [-s] [-S name] [fileone filetwo ] [directoryone directorytwo] -b Ignores spacing differences. -i Ignores case. -t Expands TAB characters in output lines. Normal or -c output adds character(s) to the front of each line that may adversely affect the indentation of the original source lines and make the output lines difficult to interpret. This option will preserve the original source's indentation. -w Ignores spaces and tabs. -c Produces a listing of differences with three lines of context. With this option output format is modified slightly: output begins with identification of the files involved and their creation dates, then each change is separated by a line with a dozen *'s. The lines removed from file1 are marked with '-'; those added to file2 are marked '+'. Lines that are changed from one file to the other are marked in both files with '!'.

who who -a who -b who -d who -H who -I

Displays who is on the system. Syntax who [-a] [-b] [-d] [-H] [-l] [-m] [-nx] [-p] [-q] [-r] [-s] [-t] [-T] [-u] [am i] [ file ] -a Process /var/adm/utmp or the named file with -b, -d, -l, -p, -r, -t, -T, and -u options turned on. -b Indicate the time and date of the last reboot. -d Display all processes that have expired and not been respawned by init . The exit field appears for dead processes and contains the termination and exit values (as returned by wait), of the dead process. This can be useful in determining why a process terminated. -H Output column headings above the regular output. -l List only those lines on which the system is waiting for someone to login. The name field is LOGIN in such cases. Other fields are the same as for user entries except that the state field does not exist. state is one of the characters listed under the /usr/bin/who version of this option. If the -u option is used with -T, the idle time is added to the end of the previous format.

Editing keystroke(s) in vi i a o, O r cw cc C R

Editing Keystroke(s) Meaning i Insert text a Append text (very useful at the end of line!) o, O "Open" a new line immediately below current, "open" a new line immediately above current r Change one character cw Change a word from the current point cc Reinitialize the current line C Change from current point to end of line R Overwrite mode

Editing, files, and exiting emacs Default Keystroke(s) ^X^C ^X^S, ^Xs ^X^W somefile ^X^F somefile, ^X^V ESC X revert-buffer Manipulating emacs windows Default Keystroke(s) ^X2, ^X3 ^Xo ^X^ ^Xb

Editing, files, and exiting emacs Default Keystroke(s) ^X^C Save and quit ^X^S, ^Xs Write file, write all files ^X^W somefile Write to a new file somefile ^X^F somefile, ^X^V Start editing a new file called somefile ESC X revert-buffer Start editing over, forgetting all changes since last save Manipulating emacs windows Default Keystroke(s) ^X2, ^X3 Split windows ^Xo Switch windows ^X^ Make a window larger vertically ^Xb Switch the current window's buffer

Editing, files, and exiting vi Keystroke(s) Meaning ZZ Q :w :w! :w somefile :q :q! :e somefile :e! :n

Editing, files, and exiting vi Keystroke(s) Meaning ZZ Save and quit Q Quit vi and go into ex (bad idea!) :w Write file :w! Force write, even if "protected" :w somefile Write to a new file somefile :q Quit :q! Quit, losing all changes :e somefile Start editing a new file called somefile :e! Start editing over, forgetting all changes since last save :n Go to the next file (such as one named on the command line)

pr COMMANDS: pr pr -column pr -a

Formats a file to make it look better when printed. -column Produce multi-column output that is arranged in column columns (default is 1) and is written down each column in the order in which the text is received from the input file. This option should not be used with -m. The -e and -i options will be assumed for multiple text-column output. Whether or not text columns are produced with identical vertical lengths is unspecified, but a text column will never exceed the length of the page (see the -l option). When used with -t, use the minimum number of lines to write the output. -a Modify the effect of the -column option so that the columns are filled across the page in a round-robin order (for example, when column is 2, the first input line heads column 1, the second heads column 2, the third is the second line in column 1, and so forth).

what commands can create files and directories?

touch mkdir cp / mv redirecting output text editors programs

Movement Default Keystroke(s) in emacs ^F, ^B, ^N, ^P ESC F, ESC B ^A, ^E ^V, ESC V ^Ssomething ESC x isearch-forward-regexpregexp ^Rsomething ESC > ESC < ^SPACE ^X^X

Movement Default Keystroke(s) in emacs ^F, ^B, ^N, ^P The big four: left, down, up, right (←, ↓, ↑, →) ESC F, ESC B Forward one word, back one word ^A, ^E Beginning of current line, end of current line ^V, ESC V Forward one screen, back one screen ^Ssomething Incremental search forward for something ESC x isearch-forward-regexpregexp Incrementally search forward for regexp (very powerful!) ^Rsomething Incrementally reverse search for something ESC > Go to last line ESC < Go to first line ^SPACE Set mark (start creating a region) ^X^X Go to mark

Movement Keystroke(s) in vi h, j, k, l w, W, b, B e, E (,) {,} 0, ^ $ +,- H, M, L

Movement Keystroke(s) Meaning h, j, k, l The big four: left, down, up, right (←, ↓, ↑, →) w, W, b, B Forward one word, back one word e, E end of current word (, ) Beginning of previous sentence; end of next sentence (doesn't always work correctly!) {, } Beginning of previous paragraph; end of next pararaph 0, ^ First position, first character $ End of line +, - First character of next or previous line H, M, L Top line of screen, middle line, last

Moving around the file system: pwd cd cd /usr/STRIM100 cd INIT cd .. cd $STRMWORK cd ~bob

Moving around the file system: pwd Show the "present working directory", or current directory. cd Change current directory to your HOME directory. cd /usr/STRIM100 Change current directory to /usr/STRIM100. cd INIT Change current directory to INIT which is a sub-directory of the current directory. cd .. Change current directory to the parent directory of the current directory. cd $STRMWORK Change current directory to the directory defined by the environment variable 'STRMWORK'. cd ~bob Change the current directory to the user bob's home directory (if you have permission).

Moving, renaming, and copying files: cp file1 file2 mv file1 newname mv file1 ~/AAA/ rm file1 [file2 ...] rm -r dir1 [dir2...] mkdir dir1 [dir2...] mkdir -p dirpath rmdir dir1 [dir2...]

Moving, renaming, and copying files: cp file1 file2 copy a file mv file1 newname move or rename a file mv file1 ~/AAA/ move file1 into sub-directory AAA in your home directory. rm file1 [file2 ...] remove or delete a file rm -r dir1 [dir2...] recursivly remove a directory and its contents BE CAREFUL! mkdir dir1 [dir2...] create directories mkdir -p dirpath create the directory dirpath, including all implied directories in the path. rmdir dir1 [dir2...] remove an empty directory

grep grep boo /etc/passwd grep -i "boo" /etc/passwd $ grep -r "192.168.1.5" /etc/

Search a file grep Command Syntax grep 'word' filename grep 'string1 string2' filename cat otherfile | grep 'something' command | grep 'something' command option1 | grep 'data' grep --color 'data' fileName How Do I Use grep To Search File? Search /etc/passwd for boo user: $ grep boo /etc/passwd You can force grep to ignore word case i.e match boo, Boo, BOO and all other combination with -i option: $ grep -i "boo" /etc/passwd You can search recursively i.e. read all files under each directory for a string "192.168.1.5" $ grep -r "192.168.1.5" /etc/

1.________: The Unix world grew up under the idea of a shell, a keyboard-based approach to computing. 2._______: A lot of Unix/Linux tasks revolve around editing various files. The best two choices for editing are the programs vi and emacs, although there are many, many editors available, including the very easy to use nano. 3._______: While scripting was not unique to Unix, its ability join programs together ad hoc using pipes was a very powerful paradigm when combined with the ability to create sequences of commands, test for conditions, and iterate over a sequence. 4._______: Unix and C are inextricably linked, with both created by the same people at the same time. In addition to C, Unix/Linux also supports virtually every other language, from ancient ones like Fortran, Basic, FORTH, and COBOL to more modern languages.

Shells: The Unix world grew up under the idea of a shell, a keyboard-based approach to computing. Editors: A lot of Unix/Linux tasks revolve around editing various files. The best two choices for editing are the programs vi and emacs, although there are many, many editors available, including the very easy to use nano. Scripting: While scripting was not unique to Unix, its ability join programs together ad hoc using pipes was a very powerful paradigm when combined with the ability to create sequences of commands, test for conditions, and iterate over a sequence. Compilation: Unix and C are inextricably linked, with both created by the same people at the same time. In addition to C, Unix/Linux also supports virtually every other language, from ancient ones like Fortran, Basic, FORTH, and COBOL to more modern languages.

mkdir mkdir -m mode mkdir -p mkdir -v mkdir -Z Ex. mkdir mydir mkdir -m a=rwx mydir

Short for make directory this command is used to create a new directory. Syntax mkdir [option] directory -m mode Set permission mode (as in chmod), not rwxrwxrwx - umask. -p No error if existing, make parent directories as needed. -v Print a message for each created directory -Z (SELinux) set security context to CONTEXT directory The name of the directory that you wish to create. Examples mkdir mydir The above command creates a new directory called mydir. mkdir -m a=rwx mydir This next example would use the -m option to not only create the mydir directory but also set the permissions to all users having read, write, and execute permissions.

Goals of Unix

Simplicity Multi-user support Portability Universities could get source code easily Users shared ideas, programs, bug fixes

.PATH .HOME .USER .SHELL

You can get a list of the current shell's environmental variables in various ways: env, printenv; (in Bash, you can also use set, but that also gives additional shell variables that aren't actually in the process's environment.) Important environment variables?(4)

Viewing and editing files: cat filename more filename less filename vi filename emacs filename head filename head -n filename tail filename tail -n filename

Viewing and editing files: cat filename Dump a file to the screen in ascii. more filename Progressively dump a file to the screen: ENTER = one line down SPACEBAR = page down q=quit less filename Like more, but you can use Page-Up too. Not on all systems. vi filename Edit a file using the vi editor. All UNIX systems will have vi in some form. emacs filename Edit a file using the emacs editor. Not all systems will have emacs. head filename Show the first few lines of a file. head -n filename Show the first n lines of a file. tail filename Show the last few lines of a file. tail -n filename Show the last n lines of a file.

What happens when you enter a command?

Whatever you type, when you hit return, the shell first takes a good long look at your input: it evaluates and substitutes for any metacharacters, it expands any variables, and then it tries to match the command against its internal command set. If it doesn't find anything there, it finally looks around the directories listed in the $PATH variable to see if it can find a program of that name. Finally, once the program is complete (or you put it in the "background"), you get a new prompt.

grep -w "boo" /path/to/file egrep -w 'word1|word2' /path/to/file grep -c 'word' /path/to/file grep -n 'word' /path/to/file

When you search for boo, grep will match fooboo, boo123, etc. You can force grep to select only those lines containing matches that form whole words i.e. match only boo word: $ grep -w "boo" /path/to/file use egrep as follows: $ egrep -w 'word1|word2' /path/to/file Count line when words has been matched grep can report the number of times that the pattern has been matched for each file using -c (count) option: $ grep -c 'word' /path/to/file Also note that you can use -n option, which causes grep to precede each line of output with the number of the line in the text file from which it was obtained: $ grep -n 'word' /path/to/file Grep invert match

Merging files

You can also specifically name which file descriptor to use with the form "n>" and "n<". This is particularly useful when you want to split, say, stdout and stderr data out to two different places. (ls -R | wc -l) 2>/dev/null Merging file descriptor data You can also merge file descriptor data with the special forms "x>&y" and "x<&y". The first lets you merge the output of two file descriptors. For example: ls -lR / 1>&2 This sends all of file descriptor 1 (stdout) also to file descriptor 2 (stderr).

grep -v bar /path/to/file # dmesg | egrep '(s|h)dp[a-z]' # cat /proc/cpuninfo | grep -i 'Model'

You can use -v option to print inverts the match; that is, it matches only those lines that do not contain the given word. For example print all line that do not contain the word bar: $ grep -v bar /path/to/file UNIX / Linux pipes and grep command grep command often used with pipes. For example print name of hard disk devices: # dmesg | egrep '(s|h)d[a-z]' Display cpu model name: # cat /proc/cpuinfo | grep -i 'Model' However, above command can be also used as follows without shell pipe: # grep -i 'Model' /proc/cpuinfo

Movement Keystroke(s) in vi ^F, ^B ^D, ^U z RET / RET ? RET ^G G mx 'X

^F, ^B Forward one screen, back one screen ^D, ^U Down half a screen, up half a screen z RET Position current line at top of screen /something Search forward for something ?something Reverse search for something / RET Repeat last search forward ? RET Position current line in middle of screen ^G Reports status line (including the current line's number) ^G Reports status line (including the current line's number) G n RET, :n RET Go to absolute line n G Go to last line mx Mark current as x 'x Go to mark x

cat -e cat -s cat -t cat -u cat -v

cat -e $ is printed at the end of each line. This option must be used with -v. cat -s Suppress messages pertaining to files that do not exist. cat -t Each tab will display as ^I and each form feed will display as ^L. This option must be used with -v. cat -u Output is printed as unbuffered. cat -v Display control characters and nonprinting characters

cat COMMANDS: cat [options] [filenames] [-] [filenames] cat file1 cat file1 > file2 cat file1 | less cat > felines

cat is one of the most frequently used commands on Unix-like operating systems. It has three related functions with regard to text files: displaying them, combining copies of them and creating new ones (bytestreams). displays the contents of a file named file1: cat file1 writes the output of file1 into file2: cat file1>file2 displays the contents of a file one page at a time, used for big files: cat file1 | less empty files will be filled with the text preceding cat function like this: cat > felines This is not about a feline. cat function used again will display: cat This is not about a feline.

chmod options filename

chmod options filename --- lets you change the read, write, and execute permissions on your files. The default is that only you can look at them and change them, but you may sometimes want to change these permissions. For example, chmod o+r filename will make the file readable for everyone, and chmod o-r filename will make it unreadable for others again. Note that for someone to be able to actually look at the file the directories it is in need to be at least executable. See help protection for more details.

cat files file1, file2, and file3 cat file1 file2 file3 > file4 cat file1 file2 file3 | sort > file4 cat > file1 cat >> file1

command will concatenate copies of the contents of the three files file1, file2 and file3: cat file1 file2 file3 The contents of each file will be displayed on the monitor screen Writes files to file4: cat file1 file2 file3 > file4 the output of cat is piped to the sort filter in order to alphabetize the lines of text after concatenation and prior to writing to file4: cat file1 file2 file3 | sort > file4 a new file named file1 can be created by typing(or overwrite): cat > file1 append operator (represented by two successive rightward pointing angular brackets) in order to prevent unintended erasure. That is: cat >> file1

cat file1 > file2 cat - file5 > file6 cat file7 - > file8

creates a new file named file2 that contains a copy of the contents of file1: cat file1 > file2 to create a new file file6 that consists of text typed in from the keyboard followed by the contents of file5, first enter the following: cat - file5 > file6 to create a new file file8 that consists of the contents of file7 followed by text typed in from the keyboard, first enter the following: cat file7 - > file8

emacs: Not always installed by default. In fact, in the last few years, default installation of Emacs in Unix/Linux distributions has become less common. What are the advantages? What are the disadvantages?

emacs advantages: As computer scientists, emacs is quite intuitive: each sequence of keystrokes can be mapped to an arbitrary Emacs Lisp function. For instance, by default in TeX mode the key a is just mapped to a function which inserts an a, but the double quote key is mapped to insert first a pair of `` and then the second time to insert a pair of closing '' in accord with TeX's expectations. emacs disadvantage: It's not found on every machine. emacs disadvantage: It's big. Doing an install via repositories can occasionally entail many more packages than you might expect!

fork(2) exec*(2) wait*(2)

fork(2) (to create a new child process) exec*(2) (to have the child process start executing a new program) wait*(2) (to wait on the child (or at least check on its status if non-blocking)) So what does that (2) indicate in the above items? The "(2)" indicates the section of the "man pages" (manual pages) where information about these (and all) system calls can be found.

ls COMMANDS: ls ls -l ls -a ls -A ls --author ls --block-size=SIZE ls -B, ls --ignore-backups

ls --- lists your files ls -l --- lists your files in 'long format', which contains lots of useful information, e.g. the exact size of the file, who owns the file and who has the right to look at it, and when it was last modified. ls -a --- lists all files, including the ones whose filenames begin in a dot, which you do not always want to see. -A, --almost-all do not list implied . and .. --author print the author of each file --block-size=SIZE use SIZE-byte blocks -B, --ignore-backups do not list implied entries ending with ~

man date

man shows online manuals of unix commands date Tells you the date and time in Unix. Syntax date [-a] [-u] [-s datestr] -a Slowly adjust the time by sss.fff seconds (fff represents fractions of a second). This adjustment can be positive or negative. The system's clock will be sped up or slowed down until it has drifted by the number of seconds specified. Only the super-user may adjust the time. -u Display (or set) the date in Greenwich Mean Time (GMT-universal time), bypassing the normal conversion to (or from) local time. -s datestr Sets the time and date to the value specfied in the datestr. The datestr may contain the month names, timezones, 'am', 'pm', etc. See examples for an example of how the date and time can be set.

more filename emacs filename mv filename1 filename2 cp filename1 filename2

more filename --- shows the first part of a file, just as much as will fit on one screen. Just hit the space bar to see more or q to quit. You can use /pattern to search for a pattern. emacs filename --- is an editor that lets you create and edit a file. See the emacs page. mv filename1 filename2 --- moves a file (i.e. gives it a different name, or moves it into a different directory (see below) cp filename1 filename2 --- copies a file

mv mv -i Ex. mv myfile.txt newdirectory/ mv myfile.txt ../ mv computer\hope.txt computer_hope.txt

mv [-i] source target Description: Renames or moves a file from one directory to another, either with the same name or a different one. Note the original file (and name) will no longer exist. This is not a copy. Options: [-i] Prompt you before replacing a file Parameters: source - what you want to copy target - where to put it Examples: mv x y mv x dir/ mv -i /etc/passwd-old /etc/passwd Examples mv myfile.txt newdirectory/ Moves the file myfile.txt to the directory newdirectory. mv myfile.txt ../ Moves the file myfile.txt back one directory (if available). mv computer\ hope.txt computer_hope.txt Moves (renames) the file "computer hope.txt" to computer_hope.txt. When working with a file or directory with a space you must escape that space with a backslash or surround the filename or directory with quotes.

rm rm -i rim -r rm -f Ex. rm myfile.txt rm -rf directory

rm [-i][-r][-f] NAMES Description: Delete files. Options: [-i] Prompt you before replacing a file [-r] Recursive, deletes an entire directory and all contents and subdirectories. [-f] Forcible removal. Don't give any error messages or ask questions even if files don't exist or permissions are awkward. Parameters: NAMES - the names of what to remove Example: rm -rf / rm -i this.* rm -r /home/carol Examples rm myfile.txt Remove the file myfile.txt without prompting the user. rm -rf directory Remove a directory, even if files exist in that directory

rm filename diff filename1 filename2 wc filename

rm filename --- removes a file. It is wise to use the option rm -i, which will ask you for confirmation before actually deleting anything. You can make this your default by making an alias in your .cshrc file. diff filename1 filename2 --- compares files, and shows where they differ wc filename --- tells you how many lines, words, and characters there are in a file

touch

touch -- Update the timestamp on a file; if the file does not exist, it is created. Changes the date/time stamp of the file filename to the current time. Creates an empty file if the file does not exist. You can change the stamp to any date using touch -t 200201311759.30 (year 2002 January day 31 time 17:59:30). There are three date/time values associated with every file on an ext2 filesystem: - the time of last access to the file (atime) - the time of last modification to the file (mtime) - the time of last change to the file's inode (ctime). Touch will change the first two to the value specified, and the last one always to the current system time.

Unix has had long had the very useful concept of being able to expand output from a process into the command line of a shell process. echo The date and time is `date` This can be particularly valuable when you save the output to a ________: % x=`date --iso-8601` % mkdir new-$x/ old-$x/ cur-$x/ % ls -d *$x cur-2013-01-29 new-2013-01-29 old-2013-01-29 %

variable

wc wc -c, wc --bytes wc -m, wc --chars wc -l, wc --lines wc -L, wc --max-line-length wc -w, wc --words wc --help wc --version

wc - print the number of bytes, words, and lines in files SYNOPSIS wc [OPTION]... [FILE]... DESCRIPTION Print byte, word, and newline counts for each FILE, and a total line if more than one FILE is specified. With no FILE, or when FILE is -, read standard input. -c, --bytes print the byte counts -m, --chars print the character counts -l, --lines print the newline counts -L, --max-line-length print the length of the longest line -w, --words print the word counts --help display this help and exit --version output version information and exit

Editing keystroke(s) in vi x dw dd D p P "np yw yy "ayy "aP :%s/something/otherthing/g u, U

x Delete the current character dw Delete word dd Delete current line D Delete from current character to end of line p Put back at current P Put back at previous (much more useful!) "np Go back in delete history n items for P yw Copy word (use p,P to retrieve) yy Copy line (use p,P to retrieve) "ayy Copy line into buffer a "aP Paste line from buffer a :%s/something/otherthing/g Replace something with otherthing throughout the current file u, U Undo; restore line (very useful!)


Conjuntos de estudio relacionados

Liability of Principle to Third Parties in CONTRACT

View Set

Chapter 27 - Bacteria and Archaea

View Set

EDUC 1300 Chapter 8: Learning Curve

View Set

Java Programming Final Test review

View Set

Chapter 12 Quiz Hardware and Network Troubleshooting

View Set

The Relationship of the Sun,Moon and Earth

View Set

Biology Exam 4 practice questions

View Set

Perception, Attribution & Decision Making

View Set

2019-20 WRS Sample Questions (1-5)

View Set

Lección 4 | Lesson Test, Lesson 4 - Lesson Test, Leccion 4 | Lesson Test (Spanish)

View Set

Ch. 11 Nucleic Acid Structure, DNA Replication, and Chromosome Structure Study Questions and Answers

View Set