UNIX Commands

Introduction

Managing files (copying, renaming, moving, removing, etc.) are common tasks. Although there are GUI ways to do these things, doing them from the command line is often quicker.

The shell (command interpreter) maintains a current directory. The current directory is the directory you are currently "in". This can be changed with the cd command.

Many commands work relative to the current directory.

When you first login, the directory that you are in is your home directory.

Folders in UNIX/Linux (just UNIX from here on) are called directories.

# indicates a comment and is not a part of the command.

Paths

Specifying paths:

Notes

For the commands below:

List Commands

ls

List the files in the current directory.

ls -l

List the files in the current directory with additional info.

ls -al

List ALL the files in the current directory with additional info.
Files whose names begin with a "." are not listed by default.

ls [options] path

List the files in the directory specified by path.
path may be absolute or relative,
options may eb the above options or others.

Moving Around Commands

cd

Change the current directory to your home directory.

cd path_to_directory

Change the current directory to directory path_to_directory.
path_to_directory may be absolute or relative

pwd

"print working directory", "present working directory", shows your current working directory.

File Commands

cp file1 file2

An identical copy of file1 is made and named file2.

mv file1 file2

File file1 is renamed (moved) to file2

rm file

File file is deleted (removed)

The commands cp, mv, and rm all may delete a file.

The destination file, file2, in the case of cp and mv is overwritten and the old content is lost.

A DELETED FILE IS UNRECOVERABLE. Be careful with these commands.

If a file is under version control, the svn commands must be used.

Directory Commands

mkdir dir

Create directory dir in the current directory.
You may want to give directories a name which begins with an uppercase letter, so they stand out.

rmdir dir

Remove directory dir in the current directory. If the directory is not empty it will not be removed.

Note that if a directory is under version control, the svn> commands must be used

File Commands

wc

Word count, show count of characters, words, and lines.

wc file
wc -l *.h *.cpp # Line count for only all .h and .cpp files

Examples

cd /                 # Change directory to the root directory
cd /usr/include      # Change directory to /usr/include
cd ..                # Change directory to parent directory
cd Dir1              # Change directory to Dir1 in current directory
cd ../Dir2           # Change directory to Dir2 in parent directory
ls ..                # List files in parent directory
ls ../../lecture     # List files in directory lecture in parent's parent
ls ~                 # List files in your home directory
ls ~/Dir             # List files in Dir which is in your home directory
mv -i file1 file2    # Interactive, ask for confirmation
rm -i file           # Interactive, ask for confirmation

Changing directories to nonexistent directories or listing nonexistent items will produce an error message.


URL: https://data-structures.cs.kent.edu/labs/Info/unix_files.html
Last update: EST