In this tutorial we will learn how to create a new directory using
mkdir, how to rename or move a directory or file using
mv, how to remove a file or directory using
rmdir, and finally how to copy a file or directory using
Making directories using the
The mkdir command has the following format
mkdir [options] directoryname.
This is a straight forward command that requires no options. In its simplest form you would enter
mkdir directoryname. The most useful option for this command is if you need to create a directory structure. For instance if you wanted ~/documents/invoices/2015 and ~documents/invoices/2016 you could type
mkdir ~/documents then
mkdir ~/documents/invoices then
mkdir ~/documents/invoices/2015 and
You could replace those four commands with the following two:
mkdir -p ~/documents/invoices/2015 and
mkdir -p ~/documents/invoices/2016.
-p option tells
mkdir to suppress any errors and create all the required parent directories.
Moving files and folders using the
The mv command has the following format
mv [options] source destination.
The mv command is short for move. It can operate on files or directories. There are a number of options here and you can view a full list of them by reading the man page using the command
We will cover a few of the most used ones here,
-f. The -u option means update and is useful where you are moving a set of files over to a location where some or all of the files may already exist. The
-u option tells
mv to only move files that are newer than the existing files if the file already exists.
-f option tells
mv to force the move even if the files already exist.
The mv -f option is highly dangerous if you are not careful. Please use this with extreme care especially when dealing with the core files of your OS or when using wildcards.
Copying files using the
The cp command has the following format
cp [options] source destination.
The cp command copies files and folders from one location to another leaving the original in place. There are a number of options you can use, to find out more use
man cp. The two options we will look at here are
The -f or force option will disable the interactive prompting when overwriting an existing file. If you are trying to copy new files over old ones say for example to update a set of php scripts then the -f option will save you having to hit the y after every file to confirm you want to overwrite it.
The -R option means perform the copy recursively. If you were in a directory that contained subdirectories you would need the -R option to copy the contents of the subdirectories. If you executed
cp * ../new-folder cp would only copy the files in the current directory. However if you used
cp * ../new-folder -R then all the subdirectories and their contents would be copied as well.
Removing files and folders using
The rm command has the following format
rm [options] filename.
The rm command is mainly used to remove files using the format
rm filename. However it can also remove directories using the recursive option.
rm -R directoryname. Using our previous example of the ~/documents/invoices/2015 and ~/documents/invoices/2015, if we wanted to remove this we could use rm -R ~/documents and it would look like this.
rm -R ~/documents/ rm: descend into directory `documents'? y rm: descend into directory `documents/invoices'? y rm: remove directory `documents/invoices/2016'? y rm: remove directory `documents/invoices'? y rm: remove directory `documents'? y
If we wanted to force this without being prompted at each level we could use the -f option.
rm -fR ~/documents/
rm -f option is highly dangerous if you are not careful. Please use this with extreme care especially when dealing with the core files of your OS or when using wildcards. It is recommended that you go to the directory you are working on rather than entering the full path from
/ before performing an
rm -f operation.
An important option is
-i option will cause the command to prompt you before each removal. This is an important safety feature when you are first starting out or when you want to be certain that each file should be deleted. The
-I option will prompt you once if you are removing more than 3 files or when removing files recursively. The
-I option is less intrusive and less work but still provides a little protection from errors.
Removing directories using
the rmdir command has the following format
rmdir [options] dirname.
rmdir is used to remove directories. In general it cannot be used to remove directories that contain files. The
-p option can be used to remove nested directories however, such as in the example earlier.