CSC 357 UNIX Basics

CSC 357 UNIX Basics


This handout has some bare-essential UNIX commands for dealing with files and directories. Chapter 1 of the Stevens book also has some introductory discussion of user-level UNIX commands. And there are links to additional UNIX resources in the 357 documentation directory.

Command Description
cp  source_file target_file
cp  source_file ... target_directory
cp _rp source_directory target_directory   
Copy files or directories. The first form copies the single source_file to the target_file. If the target exists, it is overwritten. If the target does not exist, it is created.

The second form of cp copies one or more source_files into the target_directory. The target must exist.

The third form recursively copies an entire source_directory hierarchy to the target_directory. If the target exists, the source is copied into it. If the target does not exist, it is created.


mv source target

Move the source file or directory to the target file or directory. If source is a file, target cannot be an existing directory.


ln -s  source_file target

Create a symbolic link, i.e., an alias or short cut, from the source_file to the target. If the target is a file, it cannot exist. If the target is a directory, a file of same name as source_file cannot exist.


mkdir  name

Make a directory of the given name. A file or directory of the given name cannot exist.


rm  file ...
rm -r  directory
rmdir  directory

Remove files or directories. The first form removes one or more file(s). The second form recursively removes an entire directory hierarchy. The third form removes an empty directory.


cd  directory

Change the current working directory to the given directory.


pwd

Print the current working directory.


chmod  permissions file ...
chmod -R  permissions directory


Change the permissions mode of the given file(s) to the given permissions. The first form changes one or more files. The second form recursively changes all files in the given directory. The permissions argument is a octal number with values defined as follows:
0400 Allow read by owner.
0200 Allow write by owner.
0100 Allow execute (search in directory) by owner.
0700 Allow read, write, and execute (search) by owner.

0040 Allow read by group.
0020 Allow write by group.
0010 Allow execute (search in directory) by group.
0070 Allow read, write, and execute (search) by group.

0004 Allow read by others.
0002 Allow write by others.
0001 Allow execute (search in directory) by others.
0007 Allow read, write, and execute (search) by others.

ls
ls -sal

List the contents of the current directory. The first form lists just the file names, not including files staring with ".". The second form gives a long-form listing of all files, including their sizes, modification dates, and permissions.


man  command_name

Print the detailed manual page for the given command_name.


gcc  file.c ...

Compile the given .c file(s), putting the executable result in the file named "a.out." See the gcc man page for the many options.


gdb  executable_file

Run the executable_file under debugger control. E.g.,
gdb a.out
runs the debugger on an a.out generated by gcc. See the gdb man page and 357 debugging handout for gdb command details.


emacs  file ...

Launch the emacs editor on zero or more files. See the emacs man page for further details, including how to start the tutorial. (NOTE: On some terminals, you may have trouble with the backspace key, which Emacs uses as its help key. See the writeup for 357 Lab 1 to deal with this problem.)


command  >  output_file

Redirect the output of command to output_file. The command can be any valid UNIX call, including a compiled C program. E.g.,
a.out > outfile
sends the standard output generated by the program a.out to the file named "outfile".


command  <  input_file

Redirect the contents of input_file to the input of command. The command can be any valid UNIX call, including a compiled C program. E.g.,
a.out < infile > outfile
sends infile to the standard input of a.out, then sends the output to the file named "outfile".


command_1  |  command_2

Pipe the output of command_1 to the input of command_2. E.g.,
a.out | fmt
sends the output of a.out through the simple UNIX text formating command "fmt ".


command  | tee  output_file

"Tee" the output of command to output_file, as well as to the normal standard output stream. E.g.,
a.out | tee outfile
sends the standard output of a.out to both stdout and the file named "outfile".


>&    and   |&

These are forms of output redirection and piping that apply to stderr as well as stdout. E.g.,
a.out >& outfile
redirects both the standard output and standard error of a.out to outfile. The command line
a.out |& tee outfile
sends both standard output and standard error of a.out to their normal streams, as well as to outfile.

In the preceding commands, file and directory arguments are specified as a path name, in one of these forms:

A path that starts with a leading slash is an absolute path, referring to a file from the root of the entire file system. A path that starts with ~user_id is relative to the home directory of user whose UNIX ID is user_id. A tilde character by itself refers the current user's home directory.

Here are some examples of typical commands useful in CSC 357.

Example Command Description

cd
    - or -
cd ~

Change to your home directory. Your home directory is the default when you initially log in, so this command is only necessary when you've used cd to change to some other directory.


mkdir cpe357

Make a directory named "cpe357".


chmod 700 cpe357

Change the permissions of cpe357 so that no one but you can access the files.


cd cpe357

Change into the cpe357 directory.


cp ~gfisher/classes/357/handouts/unix-commands.html .
    

Copy the file ~gfisher/classes/357/handouts/unix-commands.html into the current directory, i.e., cpe357.


rm unix-commands.html

Remove the unix-commands.html file -- you've decided to make a link instead.


ln -s ~gfisher/classes/357/handouts/unix-commands.html .

Make a symbolic link to ~gfisher/classes/357/handouts/unix- commands.html into the current directory. A symbolic link is the same as a short cut in Windows.


man ls

Print the manual page information for the ls command.

gcc -g lab1.c -o lab1

Compile lab1.c with a couple compiler options. The "-g" flag includes debugging information, so the executable program can be run better with gdb. The "-o" flag puts the executable program in the file named "lab1", instead of "a.out".