More Detail on Unix File 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. When you first login your current directory is your home directory. Many commands work relative to the the current directory.

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


Notes
Specifying paths:
There are two ways to specify where a file or directory is:
1. absolute path - The path begins with "/" and specifies the sequence of directories to the desired file or directory.
2. relative path - The path doesn't begin with "/". For "." and ".." the path starts relative to the current directory.

In Unix "/" is the top of the file system, like the c: drive in MS Windows. Unlike MS Windows there are no other drives, all is integreted under "/".

Note that "/" alone, the first "/" in a path, is a directory, called the root directory. The "/" is also used as a separator to separate directories and files in a path.
"." may be used in a path specification, it denotes the current directory.
".." may be used in a path specification, it denotes the parent of the current directory, the one before the current directory in the path to the current directory.
"~" may be begin a path specification, it denotes a user's home directory.
~username may be begin a path specification, it denotes username's home directory.


In the table below:

Bold, monospace indicates a unix command, possibly with options.

Options may follow a command name and begin with a "-". Often (in UnIx) options denoted by a single character may be grouped together. An option modifies the behavior of the command.
Italics indicates arguments to the command. These are user supplied files, directories, etc. as appropriate.
[optional items], square brackets, [ ], indicate optional items, they may be omitted or included.

Look at the manual entry for a command (man command, ex: man ls) for more details.

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 be the above options or others.


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 contents are lost. A DELETED FILE IS UNRECOVERABLE. Be careful with these commands.

cp file1 file2

An identical copy of file file1 is made and is named file2 .

mv file1 file2

File file1 is renamed (moved) to file2 .

rm file

File file is deleted (removed).

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

mkdir dir

Create directory dir in the current directory.

I usually 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.

Examples
The # indicates a comment and is not part of the command. The $> denotes the prompt.

$> 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 ~ # list files in your home directory
$> ls ~/Dir # list files in Dir which is in your home directory

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