Brief Intro to Unix Commands
-
Cat is the basic command for displaying files. Once a file is displayed the output of the file can be told to do 3 different things:
- Pass through to standard output (the screen). This is the default.
- Be redirected so that the output is dumped into a file.
- Or have the output be piped to another command; the output appearing there as the input.
A detailed example of cat, and an explanation of pipes and redirection can be
found in Text Processing
-
The cp command "CoPies" files. The syntax is:
cp source_filename dest_filename
CP does have other parameters that can be found by looking at the man page
-
To move a file from one directory to another the "MoVe" command servers two purposes:
- It can rename a file or
- Move a file to a different directory
The syntax for the two uses of MV are the same with the exception that moving a file to a different
directory has a directory specifier in front of the destination filename:
# Example of moving a file
$ ls
example1 dir1
$ mv example1 dir1
$ ls dir1
example1
# Example of Renaming a file
$ cd dir1
$ ls
example1
$ mv example1 sample
$ ls
sample
-
The command "ReMove" removes files from a directory:
$ ls
sample1 sample2
$ rm sample1
$ ls
sample2
-
To make a directory, use "MaKe DIR":
$ ls
sample1
$ mkdir dir1
$ ls
dir1 sample1
$ cd dir1
-
A directory can only be removed if there are no files in the directory using the "ReMove a DIR" command:
$ ls
sample1
$ mkdir dir1
$ ls
dir1 sample1
$ rmdir dir1
$ ls
sample1
The RM command will also allow removal of directories - take a look at the MAN page for RM for more
information. (It's not covered in this basic introduction as use of those flags can cause accidental
mass deletions if you're not careful).
-
Output from many Unix commands goes to something called "standard output" and comes in through
"standard input". Typically, these are the screen and keyboard. Redirection allows us to change these
to read input from a file or dump the output of a command to a file.
-
Redirection of output is valuable in Unix as we can save results for the future and simplify having to type
in a lot of items.
- > - The > operator redirects output to a file overwriting the file if it exists.
- >> - The >> operator appends any output to a file, creating it if it doesn't exist.
- < - The < operator takes input from a file.
Examples:
$ cat >tmp.file
This is a test
Only a test
^D
$ cat tmp_file
This is a test
Only a test
$ cat tmp_file >>tmp_file
$ cat tmp_file
This is a test
Only a test
This is a test
Only a test
Note that in the example above, ^D was used to indicate that the CTRL key and D were pressed at the same time.
-
A pipe performs the same operation as redirecting output from one command to a temporary file and then using
that temporary file as input to another command:
$ cat example1
Zebra
Aardvark
$ cat example1 >tmp
$ sort <tmp >out.txt
$ cat out.txt
Aardvark
Zebra
$ # is the same as:
$ cat example1 | sort >out.txt
$ cat out.txt
Aardvark
Zebra
-
To place a command in the background that may take a while - append an & to the end of the
line. This indicates to the shell that the command should be run in the background ("behind the scenes").
To sort a huge list of words we might wish to use something like:
$ sort <huge.list >output.list &
$ # The $ prompt immediately reappears so we can enter other commands while the sort takes place.
-
The command "cat" stands for "conCATenate" and will display or write out 1 ore more documents.
The syntax of CAT is:
cat filename
If the filename is ommitted then the output will be taken from standard input unless re-directed.
CAT can therefore be used to enter in text from keyboard by redirecting the output to a filename:
cat >file2create
Hitting a Ctrl-D after typing your text will place the characters typed into the file "file2create". CAT is
often used in shell scripts to pass the contents of a file to another command such as sort.
-
The command sort will sort the contents of a file. Sort takes input from the standard input (unless
a filename is specified). As an example, suppose the file "to_sort" contains the following text:
Zebra
Aardvark
Gorilla
Tooth Fairy
Performing a
sort to_sort
would produce the following:
Aardvark
Gorilla
Tooth Fairy
Zebra
on the screen.
Another way to do this using "CAT" is to type
cat to_sort | sort
For more information on this see the section on pipes.
-
UNIQ removes any duplicate lines from a file. It works similair to CAT and SORT in that it
expects either a filename on the command line or input from standard input and generates it's
output to standard output.
$ cat vanity.txt
I am great!
I am great!
I am really great!
I am great!
I am really great!
$ cat vanity.txt | uniq
I am great!
I am really great!
I am great!
I am really great!
Notice that uniq will only remove lines that are identical to each other that follow each other. To
remove all lines that have the same text we need to sort the text and then run it through uniq.
$ cat vanity.txt | sort | uniq
I am great!
I am really great!
Notice that the following two commands would have also worked:
$ sort vanity.txt | uniq
$ sort