UH Navigation bar CS VNET NSM UH SEARCH

Computer Science Department Unix Primer

File Commands

pwd
print working directory. This gives you the full path of your current directory
ls
list: ls gives you a list of files and subdirectories in your current directory. ls gives more informative output with certain options, my favorites are
$ ls -F Adds interesting information to the end of the filenames, e.g. a "/" if the file is a directory, or a "*" if the file is executable.
$ ls -a Shows all files, including the (normally hidden) files that start with a period.
$ ls -l Long format: gives the permissions, ownership, size, and date modified for each file.
These commands may be combined to produce informative listings.
$ ls -Fla Long, informative listing of all files
$ ls -lart Reverse time-sorted listing of all files (most recent on bottom) Now what was the file I was working on last night before I fell asleep?
$ ls -larS Reverse size-sorted listing of all files. For answering the question: Why am I getting messages about my disk quota?
cd
change directory: cd will change your current directory to whatever you specify on the command line, as long as it is a valid directory. The directory can be given in two ways, relative directory, or absolute path. A relative directory starts with the current directory, and moves up or down the filesystem tree from there, while the absolute path starts with the root directory. Examples:
Say we have a directory tree of the form:
    
              (mail)
            /
      (etc) - (init.d)
    /
(/) - (bin)   (bin)
    \       /
      (usr) - (include) - (sys)
            \ 
              (lib)

If we are in /usr (typing 'pwd' at the prompt returns '/usr'), then an example of a relative path would be:

$ cd include

or

$ cd include/sys

or to return to the parent directory,

$ cd ..

Absolute paths always start with '/' and have to begin from the root. When using an absolute path, it does not matter which directory is your current working directory.

$ cd /
$ cd /usr/include/sys
$ cd /bin

Experiment with ls and cd, these are probably the most often-typed commands in Unix.

NOTE: Don't worry about getting lost: typing 'cd' with no arguments will always bring you back to your home directory.

cp
copy: cp allows you to copy files and directories. (use the -r option to copy directories.) Note for DOS users: cp always requires at least two arguments. If you want to copy to your current directory, then use a "." as the second argument.
cp filename1 filename2 Make a copy of filename1 called filename2
cp filename1 directory Make a copy of filename1 in directory
cp filename1 filename2 ... directory copy filename1, filename2, ... to directory
mv
move: mv can be used to move files and directories around (if the second argument is a directory), or to rename them.
rm
remove: deletes files or directories. To delete directories, use the -r option. (-r stands for recursive) be careful
mkdir
make directory: creates a directory
rmdir
remove directory: deletes an empty directory (you will get an error if the directory is not empty. This is a good way to make sure you know what you are doing.)
cat
concatenate: cat is a very useful command that is often used just to display a file, but is designed to join multiple files. The file is written to standard output which is usually set to the terminal.
Wildcards

Although not a command, it is important to bring up wildcards in this section on file commands. Wildcards can be used to save typing by substituting a whole list of files for one typed instruction.

The asterisk (*) can be used to wildcard large parts of a filename, while the question mark (?) is used to wildcard single characters in a name.

Let's use an example directory full of files:
file1, file2, ... file20, and file_a, file_b, ... file_z

if we did an ls, we'd get:
$ ls
file1   file14  file19  file5  file_a  file_f  file_k  file_p  file_u  file_z
file10  file15  file2   file6  file_b  file_g  file_l  file_q  file_v
file11  file16  file20  file7  file_c  file_h  file_m  file_r  file_w
file12  file17  file3   file8  file_d  file_i  file_n  file_s  file_x
file13  file18  file4   file9  file_e  file_j  file_o  file_t  file_y

Now, some examples of ls using wildcards:

What files have only one character following the word 'file'?

$ ls file?
file1  file2  file3  file4  file5  file6  file7  file8  file9

What files contain the letter 'a'?

$ ls *a*
file_a

We can even use some regular expression syntax on our files. For example: to get all files that have numbers after them:

$ls file[0-9]*
file1   file11  file13  file15  file17  file19  file20  file4  file6  file8
file10  file12  file14  file16  file18  file2   file3   file5  file7  file9

NOTE: Wildcards are interpreted by the shell, and not the individual programs you may use them with. To read more on wildcards, please see the manpage for your shell (e.g. bash, csh, tcsh)

University of Houston State of Texas Privacy and Policies Homeland Security Compact with Texans Reporting Copyright Infringement Contact U H Feedback Site Map Statewide Search U H System