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.
Specifying paths:
There are two ways to specify where a file or directory is:
absolute path - The path begins with "/" and specifies the sequence of directories to the desired file or directory.
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 Windows. Unlike Windows, there are no other drives, all is integrated 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 begin a path specification, it denotes a user's home directory.
"~username
" may begin a path specification, it denotes username
's home directory.
For the commands 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.
|
List the files in the current directory. |
|
|
List the files in the current directory with additional info. |
|
|
List ALL the files in the current directory with additional info. |
|
|
List the files in the directory specified by path. |
|
Change the current directory to your home directory. |
|
|
Change the current directory to directory path_to_directory. |
|
|
"print working directory", "present working directory", shows your current working directory. |
|
An identical copy of file1 is made and named file2. |
|
|
File file1 is renamed (moved) to file2 |
|
|
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.
|
Create directory dir in the current directory. |
|
|
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
|
Word count, show count of characters, words, and lines. |
wc file
wc -l *.h *.cpp # Line count for only all .h and .cpp files
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.