Commands useful for viewing contents of files.
Generally, these should be applied only text files.
cat [file[s]] # concatenate.
displays contents of a list of files.
displays them in the order specified.
When used with redirection, >, creates a single concatenated file.
cat fn.c1 fn.c2 fn.c3 fn.c4 > fn.all
When used with piping, |, creates a single data stream input to next cmd.
cat fn.c1 fn.c2 fn.c3 fn.c4 | sort > fn.sorted
When used without input file, redirects keyboard input to specified file.
# used [ctrl]D to specify end of input.
Some Options:
-b # number non-blank lines.
-n # number all lines.
-v # show non-printable characters, except line feed or tab.
-t # show tabs with ^I
-e # show end of lines with $
head file[s] # show the 1st 10 (default) lines of a file.
Useful for looking at comment area of source code or shell script.
Uses line-feed delimiter to recognize lines for most options.
Some Options :
-c [[-]n] # displays 1st n characters of file. -n displays all but
last n characters of file.
-b [[-]n] # displays 1st n lines of file. -n displays all but
last n lines of file.
tail file[s] # show the last 10 (default) lines of a file.
Useful for looking at log files, which have latest log info appended
to end of file.
Uses line-feed delimiter to recognize lines for most options.
Some Options :
-c [n] # displays last n characters of file. Line feed is a valid
character, so remember to count it.
-b [n] # displays 1st n lines of file.
od [file] # octal dump. Displays the byte value of each byte in a file.
Default display is in 2 byte octal format.
Can be safely used to view binary files.
Some Options:
-t # format type modifier, newer option. Gives better control of display.
-b, -t o1 # octal bytes.
-c, -t c # show printable characters as such, uses backslash form
-a, -t a # show printable characters as such, uses abbrev. names
-x, -t x2 # show values in hexadecimal.
--endian {big|little} # changes endian order of byte sequence, new.
more file[s] # view the contents of text file one screen at a time.
Use [return] to advance on line.
Use / and regular expressions to search forward.
Can only move forward in file.
Refuses to view binary file.
Superseded by less
less file[s] # shows the contents of a file one screen at a time.
Allows movement or search in either direction.
Allows use of {up|down} arrow keys or some vi (vim) keys to move.
Uses regular expression (regex) patterns to do search matches.
Can search forwards or backwards in text.
Highlights found instances of phrase.
May be used to safely view a binary. Probably won't make sense.
Some Options :
-N # number each line.
-n # do not number each line (default)
-i # ignore case, ignore case of text unless search pattern includes
upper-case.
-I # ignore case, ignore case of text even if search pattern includes
upper-case.
-- # causes any subsequent arguments on command line be treated as
file-names. Useful if filename actually starts with - (hyphen).
less can be used to view/search multiple files.
Some useful cmds:
/ {regex} # search forward for string match to regex pattern.
? {regex} # search backwards for string matching pattern.
/ and ? # without pattern, repeats search for previously specified pattern
in specified direction.
q, Q, :q, :Q # quits viewing.
# When viewing more than one file
:n # displays next file.
:p # displays previous file.
:d # remove current file from list files being viewed.
:e [filename] # examine, Open a new file to view.
pr # displays file in printer friendly format.
Doesn't actually print.
Use redirection to capture output.
Prints header information and numbers pages.
Some Options :
-n # (COLUMN) : n is a numeric value specifying number of
columns to display. e.g pr -3 fn1
Treats each line as 1 item.
Works best on files containing one item or specific groupings per line.
-a[n] # Used with COLUMN option but arranges data across instead
of vertical.
-h # center header.
-l # sets pages length, default 56 lines
-n # number lines, includes formatting specifiers.
-N # specify line number for initial line.
-t # omit header
-T # omit paging, useful when using other options to arrange text.
diff # compares two files and reports any different lines.
Indicates how each file should be modified to match other.
Files need to be fairly similar for this to be useful.
Some Options :
-q # only report if different, no list of differences,
sets non-zero error return code.
-i # ignore case.
-b # ignore differences in white-space. Still sees it. (tabs = spaces)
-w # ignore all white-space, as if not there at all.
-B # ignore blank lines.
comm # compares two files and generates 3 columns.
More useful if files contain single item or short grouping lists.
Files need to be fairly similar for this to be useful.
- column 1 has lines unique to 1st file.
- column 2 has lines unique to 2nd file.
- column 3 has lines common to both.
grep # global regular expression (regex) parser.
Search utility with an extensive search criteria 'language'.
Case sensitive but this can be over-ridden.
Displays lines containing strings matching criteria.
Multiple search patterns can be specified.
Search patterns can be stored in file for repeated use.
Can search through list of files.
Some search criteria :
- Literal string match
- Word match
- Location in line
- Match to alternative list of characters.
- Match to number of characters.
Some Options :
-e # used to specify different (multiple) search patterns.
-f regexfile # used to ID file containing search patterns to use.
-i # ignore case.
-v # invert, displays lines containing no strings matching pattern.
-w # match only to strings that are complete words.
-l # show only file name containing match.
fgrep - version of grep that doesn't use regular expressions - faster.
egrep - version of grep that uses different regular expressions -
adds some features but loses others.
Both are now emulated in grep with appropriate options.
Commands useful for modifying contents of files.
aspell # interactive spell checker.
Uses a standard dictionary database.
But also allows user to keep a local database of additional words.
Saves original file as a .bak
When a questionable word is found, user is provided several options.
Numbered list of possible alternatives. Choose number to replace.
i) ignore spelling in this instance.
I) ignore all occurrences of this spelling in this file.
a) add current 'word' to local spelling database. Becomes an accepted
word for all future spelling checks.
Personal dictionary : ~/.aspell.en.pws
r) replace - allows user to manually enter an alternative spelling, may
require an a) add to complete entry.
R) replace all further occurrences of this spelling.
b) abort - cancel spell check and any changes already made.
sort # sorts a file or files and sends results to standard output.
Sorts text based files, expects end of line markers.
Original file not modified.
Most often used with redirection to generate new sorted version of file.
DO NOT redirect to same file as source, file will be destroyed
before being opened.
Use -o option to put output back in source file.
sort data.txt -o data.txt
Multiple input files may be specified, will generate single sorted output.
sort data1 data2 data3 > data.s
Can specify 'columns' or fields to use as primary sort key criteria.
Can specify alternative field separators as column separators.
Can sort in reverse order.
Can check if sorted without doing any sorting.
Can recognize, understand, and sort on date.
Some Options :
-o filename # output to specified filename, may be source file.
-n # evaluate numbers representation for their value. So 2 comes before 03,
whereas, normally, it would put 03 before 2 because it sees 03 as a 2 character
string starting with 0.
-r # sort in reverse order.
-R # randomize, unsorts data.
-c # check without sorting, sets return code, 0 if sorted, >0 if not. Also
prints first out of place line if unsorted.
-C # check whiteout sorting, sets return code, 0 if sorted, >0 if not. No
message.
-m # merges multiple files, assumes already sorted. faster?
-M # recognizes month names, such as January or jan, and honors ordering.
-k n[.C][OPTS] # where n is a numeric values specifying
field to sort on first.
Can be repeated to specify additional sort keys.
Can specify specific start character in target field to use as primary sort
point.
Several of the general options available may be applied to the key value
even if they differ from main option specification.
-t char # field separator (transition), normally space and tab
separate fields on a line. -t allows specifying different separator.
sort -t : -k 2 fn1 # says to recognize colon as separator and then to
use 2nd field of line to do primary sort.