File System

Creating a File

Command Effect
cat > myfile Allows you to enter text with the keyboard to be stored in the file named myfile. After entering the desired text, press Crtl-D.
vi myfile Launches the vi text editor, given a file name of myfile.

Creating a Directory

Command Effect
mkdir dir2 Creates a directory (within the current directory) named dir2.

Displaying File Contents

Command Effect
cat myfile Displays the entire contents of the file name myfile.
more myfile Displays the contents of the file myfile one page (screen-full) at a time.

Comparing Files

Command Effect
diff file1 file2 Performs a line-by-line comparison of the files file1 and file2. The differences are displayed in the shell window.
cmp file1 file2 Performs a byte-by-byte comparison of the files file1 and file2. The differences are displayed in the shell window.

Changing Access Modes

Command Effect
chmod mode file1 file2 Changes the modes of both files file1 and file2 to mode.
chmod -R mode dir Changes the modes of all files and directories within the dir directory to mode.

Here are the mode settings:

  • Users affected
    • u: user (owner)
    • g: group
    • o: other (world)
  • Actions
    • +: add permission
    • -: remove permission
  • Access types
    • r: read
    • w: write
    • x: execute

For example:

UNIX> chmod go-rwx foo.c

removes read, write and execute permissions for "group" and "other" users for the file foo.c.

Listing Files and Directories

Command Effect
ls Lists the contents of the current (working) directory.
ls -a Lists the contents of the current directory, including dotfiles.
ls -l Lists the contents of the current directory in long format (shows access modes).

Moving (or Renaming) Files and Directories

Command Effect
mv src-file dest-file Renames the file src-file​ to dest-file
mv src-file dest-dir Moves the file src-file into the dest-dir directory.
mv src-diri dest-dir Renames the src-dir directory to dest-dir​; alternatively, if dest-dir already exists, moves the src-dir directory into the dest-dir directory.
mv -i src dest Moves src to dest, but prompts before overwriting existing files/directories.

Copying Files

Command Effect
cp src-file dest-file Makes a copy of the file src-file and names the copy dest-file.
cp src-file dest-dir Copies the file src-file into the dest-dir directory.
cp -R src-dir dest-dir Copies the src-dir directory and all of its contents into the dest-dir directory.
cp -i src dest Copies src into dest, but prompts before overwriting existing files.

Deleting Files and Directories

Command Effect
rm myfile Deletes (removes) the file myfile.
rmdir mydir Deletes the empty directory named mydir.
rm -r mydir Deletes the mydir directory and all of its contents.
rm -i myfile Deletes the file myfile​, but prompts before doing so.

Compressing Files

Command Effect
gzip myfile Compresses the file myfile​, replacing it with the file named myfile.gz.
gunzip myfile.gz Uncompresses the file myfile.gz, replacing it with the file named myfile.

Changing the Working Directory

Command Effect
cd Changes the current (working) directory to your login (home) directory.
cd mydir Changes to the mydir directory.

Displaying the Name of the Current Directory

Command Effect
pwd Displays the absolute path of the current (working) directory.

Searching Within Files

Command Effect
grep hello myfile Displays the lines within the file myfile that contain the string hello.
grep hello file1 file2 ... Displays the lines within files file1file2​, etc. that contain the string hello.
grep hello * Displays the lines within any file in the current directory that contain the string hello.
grep -v hello * Displays the lines within any file in the current directory that do not contain the string hello.
grep -i hello * Displays the lines within any file in the current directory that contains the string hello in any letter-case. For example, lines containing "Hello", "HELLO" and "HeLlO" would be displayed.