Learn UNIX in 10 minutes
Directories
Moving around the file system
Changing file permissions and attributes
Moving, renaming, and copying files
Viewing and editing files
Shells
Environment variables
Interactive History
Filename Completion
Bash is the way cool shell
Redirection
Pipes
Command Substitution
Searching for strings in files (
The grep command )
Searching for files (
The find command )
File compression ( compress, gzip, and bzip2 )
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 |
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 ~bib |
Change the current directory to the user bib's home directory (if you have permission). |
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.
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. |
| 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 |
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. |
Shells
The behavior of the command line interface will differ slightly depending
on the shell program that is being used.
Depending on the shell used, some extra behaviors can be quite nifty.
You can find out what shell you are using by the command:
echo $SHELL
Of course you can create a file with a list of shell commands and execute it like
a program to perform a task. This is called a shell script. This is in fact the
primary purpose of most shells, not the interactive command line behavior.
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 |
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. |
Interactive History
A feature of bash and tcsh (and sometimes others) you can use
the up-arrow keys to access your previous commands, edit
them, and re-execute them.
Filename Completion
A feature of bash and tcsh (and possibly others) you can use the
TAB key to complete a partially typed filename. For example if you
have a file called constantine-monks-and-willy-wonka.txt in your
directory and want to edit it you can type 'vi const', hit the TAB key,
and the shell will fill in the rest of the name for you (provided the
completion is unique).
Bash is the way cool shell
Bash will even complete the name of commands and environment variables.
And if there are multiple completions, if you hit TAB twice bash will show
you all the completions. Bash is the default user shell for most Linux systems.
Redirection
| grep string filename > newfile |
Redirects the output of the above grep
command to a file 'newfile'. |
| grep string filename >> existfile |
Appends the output of the grep command
to the end of 'existfile'. |
The redirection directives, > and >> can be used on the output of most commands
to direct their output to a file.
Pipes
The pipe symbol "|" is used to direct the output of one command to the input
of another.
For example:
ls -l | more
This commands takes the output of the long format directory list command
"ls -l" and pipes it through the more command (also known as a filter).
In this case a very long list of files can be viewed a page at a time.
du -sc * | sort -n | tail
The command "du -sc" lists the sizes of all files and directories in the
current working directory. That is piped through "sort -n" which orders the
output from smallest to largest size. Finally, that output is piped through "tail"
which displays only the last few (which just happen to be the largest) results.
Command Substitution
You can use the output of one command as an input to another command in another way
called command substitution. Command substitution is invoked when by enclosing the
substituted command in backwards single quotes.
For example:
cat `find . -name aaa.txt`
which will cat ( dump to the screen ) all the files named aaa.txt that exist in the current
directory or in any subdirectory tree.
Searching for strings in files: The grep command
grep string filename prints all the lines in a file that contain the string
Searching for files : The find command
find search_path -name filename
find . -name aaa.txt Finds all the files named aaa.txt in the current directory or
any subdirectory tree.
find / -name vimrc Find all the files named 'vimrc' anywhere on the system.
find /usr/local/games -name "*xpilot*"
Find all files whose names contain the string 'xpilot' which
exist within the '/usr/local/games' directory tree.
File compression: compress, gzip, and bzip2
The standard UNIX compression commands are compress and uncompress. Compressed files have
a suffix .Z added to their name.
For example:
compress part.igs Creates a compressed file part.igs.Z
uncompress part.igs Uncompresseis part.igs from the compressed file part.igs.Z.
Note the .Z is not required.
Another common compression utility is gzip (and gunzip). These are the GNU compress and
uncompress utilities. gzip usually gives better compression than standard compress,
but may not be installed on all systems. The suffix for gzipped files is .gz
gzip part.igs Creates a compressed file part.igs.gz
gunzip part.igs Extracts the original file from part.igs.gz
The bzip2 utility has (in general) even better compression than gzip, but at the cost of longer
times to compress and uncompress the files. It is not as common a utility as gzip, but is
becoming more generally available.
bzip2 part.igs Create a compressed Iges file part.igs.bz2
bunzip2 part.igs.bz2 Uncompress the compressed iges file.
|