Directory Commands
Introduction
You can use directory commands to create and remove directories, list their contents and more.
Create Directories
mkdir <directory>
Example: mkdir comp101
List the Contents of a Directory
ls <directory>
Example: ls comp101
Deleting Directories
rmdir <directory>
Note: The directory must be empty in order for you to successfully delete the directory.
To recursively delete a directory and all the files and subdirectories below that directory, type:
rm -r <directory>
Example: rm -r comp101
Display the Current Working Directory
pwd
You can also view your current working directory by typing:
echo $cwd
Changing Directories
This command will move you down into the specified directory. The specified directory must be a subdirectory of the directory you are currently in.
cd <directory>
Example: cd classes
This command will move you up one directory level from your current directory:
cd ..
This command will move you into the specified users home directory:
cd ~<username>
Example: cd ~ttrojan
This directory will be readable only if you have read permissions for it.
This command will move you into the specified directory starting from the root and following the path:
cd /<...path...>/<directory>
Examples:
cd /tmp
cd /usr/usc/doc>
cd /usr/spool/mail
cd /usr/spac/sparcompilers/default
The command by itself will take you back to your top level (home) directory.
Note: "/" represents the root directory. "~" represents users' home directories. ".." represents the parent directory.
Copying Directories
This command will make a copy of the specified file and put it in the specified directory leaving the original file intact:
cp <filename> <directory>
Example: cp essay1 comp101
This command will do one of two things. If the target directory already exists, a copy of the source directory will be put into the target directory. If the target directory does not already exist, the target directory will be created and it will contain all the files and subdirectories that the source directory contains.
cp -r <directory> <directory>
Example: cp -r comp101 comp102
Move/Rename Directories
This command will move the specified file from your current directory to the specified directory. No copy of the file will be left in its original directory.
mv <filename> <directory>
Example: mv essay1 comp101
This command will do one of two things. If the target directory already exists, the source directory will be moved into the target directory. If the target directory does not already exist, the source directory will be renamed with the target directory name.
mv <directory> <directory>
Example: mv comp101 comp102
Push Directories
This command will push a directory onto the directory stack. With a <directory> option, it will push the current working directory onto the stack and change to <directory>. With an <n> option, it will rotate the <n>th entry to the top of the stack and change to that directory. With no arguments it will exchange the top two elements.
Examples:
pushd <directory>
pushd +<n>
pushd
Example: pushd /usr/spac/sparcompilers/default
Pop Directories
This command will pop the directory stack and cd to the new top directory. The elements of the directory stack are numbered from 0 starting at the top. With an <n> option, it will discard the <n>'th entry in the stack.
popd
popd +<n>
Example: popd
Print the Directory Stack
This command will print the directory stack with the most recent directory shown on the left; the first directory shown is the current directory. With the - l option, it will produce an unabbreviated printout. Use of the ~ notation is suppressed.
dirs
dirs -l
Example: dirs
Last updated:
February 03, 2011