This section of the course describes Unix commands for working with files. One of the innovative concepts in Unix is that everything is treated as a file: text documents and programs that you create are files, executable commands are files, directories are files, even tape drives and terminals attached to the system are files. Obviously directories and tape drives are special kinds of files, but when you are exploring the system, you'll find these special files listed in the same manner that your text files are listed.
When executing a command, Unix examines all the directories in your path trying to find the command. Most commands you will issue will be in one of a few standard directories, all included in your default path. If you enter a command that you think should work and receive the response "Command not found", it could mean that either you mistyped the command, the command doesn't exist on the system, or it is just not in your path. We'll touch a little later on modifying your path.
Command | Description | Syntax & example |
---|---|---|
ls | Lists the names of the files & directories. It has several options for getting more information. | ls [ filename ] ls -l (long listing) ls -a (include hidden file) ls -F (note directories with "/") ls -alF (combining options) |
cat |
Displays the contents of a file to your screen. | cat file-name |
less | Displays the contents of a file to your screen, one screen at a time. | less file-name cat file-name | less |
head | Displays the first ten lines of a file. | head file-name You can give the head command a number option if you want to see more or less than the first ten lines of your file. Usage: head file-name |
tail | Displays the last ten lines of a file. | tail file-name tail -15 file-name |
rm | Deletes files | rm file-name rm -i file-name |
mv | Move or change name of a file | mv old-file-name new-file-name mv file-name new-directory mv file-name new-directory/new-file-name |
cp | Makes a copy of a file | cp original-file-name new-file-name cp original-file-name target-directory |
This section explains some of the commands used to create and manipulate directories.
Command | Description | Syntax & example |
---|---|---|
cd | Change to a different directory | cd directory-pathname |
mkdir | Create a new directory | mkdir new-directory-name |
rmdir | Delete (remove) empty directory | rmdir directory |
cp -r | Copy directory structures (recursive) | cp -r original-directory target-directory |
pwd | Print working directory; path name of the current directory. | pwd |
Here is a list of special characters or words (strings of characters) which have meaning in most environments:
Special Char | Pronunciation | Explanation |
---|---|---|
! | Exclamation point (bang) | Start of a new command |
& | Ampersand | Background the process |
&& | Double ampersand | Logical AND |
| | Vertical bar (pipe) | Redirects output |
|| | Double vertical bar | Logical OR |
; | Semicolon | Equivalent to Return/Enter |
< | Less than | Redirect input from |
<< | Double less than | Reads from shell input (until [word]) |
> | Greater than | Redirect output to (overwrites) |
>> | Double greater than | Redirect output to (appends) |
>& | Greater than ampersand | Redirect output and error output to |
( | Left parenthesis | Used for grouping |
) | Right parenthesis | Used for grouping |
There are three commands that can give you help with and more information about the Unix environment:
Usage: man command-name (Example: man ls)
The -k option of the man command allows you to search for a manual entry without knowing the exact name of the command. It will list one line summaries of all manual entries that contain the keyword you supply.
Usage: man -k keyword (Example: man -k list)
Usage: apropos keyword (Example, apropos list)
Usage: whatis command-name (Example: whatis ls)
When finished in the Unix environment, make sure you log off. The commands to log off the system are:
logout, or