Creating, moving and deleting files and directories

To create an empty file, use the touch command.

$ touch file

The touch command creates empty file.

mv command moves or renames files (or directories). Synopsis:

mv source destination

The source is the file (or directory) you wish to move. The destination is where you want the file (or directory)to be located. When a file is moved, the file is removed from the original location and placed in a new location. In the following example, the file is moved from the current home directory to the Documents directory.

$ mv file Documents

The file name will change only if a different destination file name is also specified. The mv command is also used to rename a file. The following example will rename the file in the current directory.

$ mv file file2

If the destination file already exists, the mv command will overwrite the existing file. With the -i option, the mv command will prompt before overwriting a file. The -i option requires you to answer y for "yes" or n for "no", for every copy that could end up overwriting an existing file. mv command will by default move directories.

cp command copies files (or, optionally, directories). Synopsis:

cp source destination

The following command will copy the file from the current home directory to the Documents directory.

$ cp file Documents

New file will have the same name as the original file. If you want the new file to have a different name, you must provide the new name as part of the destination.

$ cp file Documents/file2

To copy a directory and all files in a directory, use the -r option. Like the mv command, the cp command provides -i option.

rm command removes each given file. By default, it does not remove directories. Synopsis:

rm file

Files are permanently deleted without question. As a precaution, users should use the -i option.

$ rm -i file

If you want to delete a directory, use the -r option.

To create a directory, use the mkdir command.

$ mkdir directory

You can also delete a directory with the rmdir command, but only if the directory is empty.

$ rmdir directory