Manage files and directories

Creating, renaming, and removing files and directories.

Creating empty files

The touch <file> command can be used to create new empty files.

Multiple files can be created in a single command.

For instance:

touch a.txt b.csv

Creating files using the &rsquo;touch&rsquo; command.

If a file already exists, the touch command does not edit the contents of the file, but update the timestamp of the latest edit to the current date-time.

Creating directories

The mkdir command can be used to create new directories.

Multiple directories can be created in a single command.

For instance:

mkdir dir1 dir2

Creating directories using the &lsquo;mkdir&rsquo; command.

Copying files

The cp command can be used to make a copy of a file.

The command requires two arguments:

  • first, the filepath to the original file
  • second, the path to the file where the copy must be made

For instance:

cp a.txt a_copy.txt

Copying directories

The cp command can be used with the option -r to recursively copy a directory and its contents to a new directory.

Similarly to the copy of files the command requires two arguments:

  • first, the location of the original directory
  • second, the path to the new directory to create as a copy.

Moving and renaming files

The mv command can be used to both move and rename files.

In particular, renaming a file can be seen as moving the file from one name to another, within the same directory. Meanwhile, a file can be moved to another directory, either keeping the name of the file or renaming the file at the same time.

For instance, a file file1.txt can be renamed to file1.csv as follows:

mv file1.txt file1.csv

Renaming a file using the &lsquo;mv&rsquo; command.

On the other hand, moving a file file1.txt to another directory – without renaming the file itself – can be done as follows:

mv file1.txt ../another_demo_dir/

Moving a file using the &lsquo;mv&rsquo; command.

Remember that absolute paths may be used, both for the file to move and the directory to move it into:

mv /path/to/file1.txt /path/to/new/directory

Finally, a file can be moved to another directory and renamed in a single command as follows:

mv file1.txt ../another_demo_dir/file1.csv

Moving and renaming directories

Similarly to files, directories can be moved and renamed using the mv command.

For instance, a directory dir1 can be renamed to dir2 as follows:

mv dir1 dir2

Renaming a directory using the &lsquo;mv&rsquo; command.

However, if the the new directory already exist, the Bash session will then interpret the command as a request to move the first directory into the second one.

mv dir1 dir2

Moving a directory using the &lsquo;mv&rsquo; command.

Removing files and directories

The rm command can be used to remove files and directories.

Files

For files, the rm command takes the name(s) of one or more file(s) to remove.

rm file1.txt

Removing a file using the &lsquo;rm&rsquo; command.

Multiple files can be removed in a single command, stating individual filenames and/or using the wildcard symbol *.

For instance, the next command demonstrates how to remove the files named file1.txt and file1.csv, along with all files whose names start with file2 and all files that end with .csv:

rm file1.txt file1.csv file2* *.csv

Directories

For directories, the rm command must be used with the option -r.

The option -r indicates that the directory must be remove recursively, meaning that any content in the directory will be removed as well. Refer to the rmdir command below for a safer alternative.

rm -r dir1

Removing a directory using the &lsquo;rm -r&rsquo; command.

It is possible to verbosely display the list of files and directories that are removed by the rm command using the option -v.

Removing a directory verbosely using the &lsquo;rm -rv&rsquo; command.

As a safer alternative, the rmdir command can be used to remove only empty directories. The command will raise an error when attempting to remove a directories that are not empty.

In the example below, the rmdir command is asked to remove two directories. However, one of those directories is not empty, and only the empty directory is actually removed.

rmdir dir1 dir2

Removing an empty directory using the &lsquo;rmdir&rsquo; command.

Cheatsheet

Common options for the rm command are listed below, in alphabetical order of the option flag.

OptionLong optionDescrition
-i--interactiveInteractively prompt the user for confirmation before deleting files. Without this option, files are silently deleted.
-r--recursiveRecursively delete directories. This option is required to delete directories.
-f--forceDo not prompt and ignore errors due to nonexistent files. This overrides the -i option. Use with extreme care.
-v--verboseDisplay informative messages as deletions are performed.