Create, Move, Copy and Delete Files and Directories

It is important to be able to work with files and directories on Linux. There are several ways to create files in Linux. This section will focus on using the touch command to create empty files. You will use the mkdir (make directory) command to create directories. Files will be renamed with the mv (move) command. The cp command is used to copy files. You will then delete those files with rm (remove).

Learning Objectives

You should be able to:

  • Create files using the touch command
  • Create folders using the mkdir command
  • Rename files using the mv command
  • Copy files using the cp command
  • Print the contents of files using the cat command
  • Use the * wildcard character
  • Remove files and folders using the rm command

Video Walkthrough

Use this video to follow along with the steps in this lab.

Exercise Touching Files

The touch command can do several things. First, it can be used to create new, empty files. The touch command can also be used to update the last modified dates and other date properties of files, but we won't get into that.

  • Log into your Linux computer.
  • Run pwd to ensure that your working directory is your home directory.
  • Create a new file with the following command:
touch you.txt
  • Notice that Linux does not give you any indication that the command succeeded. This is the Linux way. No news is (hopefully) good news.
  • Create a new file called me.txt with the touch command.
touch me.txt
  • Verify that the files exist using the list command.
ls

You should see me.txt and you.txt listed.

me.txt you.txt

Move files with mv

It's generally not a great idea to litter your home directory with random files. Follow the steps below to move me.txt and you.txt into a new directory named mydocs.

Run the following mv (move) command to move me.txt to the a new mydocs directory.

mkdir mydocs
mv me.txt mydocs
  • Again, no news is (hopefully) good news.
  • Run the following list commands to check the contents of your home directory and the mydocs directories. Remember that case matters.
ls
ls mydocs
  • You should see me.txt listed in the contents of the mydocs directory.
$ ls mydocs
me.txt
  • The ls command knows that mydocs is a directory in the current working directory, so adding it to the ls command tells the ls command to list the files in that directory.
  • Adapt the previous commands to move you.txt into the mydocs folder.
  • Change directories to your mydocs folder.
  • When finished, run the ls command to view the contents. You should see something similar to the following.
mydocs$ ls
me.txt you.txt

Copying Files

The cp command copies files. The basic syntax is: cp existingfilename copiedfilename.

In the Linux terminal, ensure you are in the home directory.

cd
  • The cd command without any parameters is a shortcut to go to the home directory. This is the same as cd ~.
  • Change directories into mydocs again.
cd mydocs
  • Create a file called config.txt using the following command.
echo "very important config" > config.txt
  • The text "very important config" will be created in the file named config.txt. This is another way to create files.
  • Show the contents of the file using cat.
cat config.txt
  • Before making changes to important system files, it is often a good practice to create a backup copy. Use the cp command to make a copy.
cp config.txt config_backup.txt
  • List the files to verify that both files are present and are the same size.
ls -al

Create Directories

The files are organized better already, but typically we want to organize files in multiple directories. You might have a different directory for each project you are working on, for example. You previously used mkdir to make the mydocs directory. You will practice using mkdir in this section.

  • Your working directory should be ~/mydocs. Use pwd to verify that this is your working directory. If you need to change your working directory, use the cd command below.
cd ~/mydocs
  • Run the following command to create a sweetcaroline directory.
mkdir sweetcaroline
  • Again, no news is good news.
  • List the files and directories in the mydocs directory.
ls
config.txt config_backup.txt me.txt sweetcaroline you.txt
  • That's about how complicated it is to create a directory using the command-line interface in Linux.
  • Move both text files into sweetcaroline using the following command.
mv *.txt sweetcaroline
  • Instead of moving files one by one, it is possible to move multiple files at once. Here, the * character is used to match any file that has the .txt extension. The * character is a common wildcard character. Wildcard characters are used for pattern matching.
  • Change directories to sweetcaroline and list the files.
cd sweetcaroline
ls
config.txt config_backup.txt me.txt you.txt

Deleting Files

Files are deleted with the rm remove command. Pay attention when using the rm command. Linux will let you accidentally delete everything on your system.

  • Use pwd to ensure that your working directory is ~/mydocs/sweetcaroline.
  • Run the following command to delete me.txt.
rm me.txt
  • Notice that there is no prompt about making sure you really want to do it. The file just gets deleted.
  • Verify that me.txt was deleted by listing the directory's contents.
ls
config.txt config_backup.txt you.txt

Deleting Directories

Deleting directories with the rm command requires additional options.

  • Use pwd to ensure that your working directory is ~/mydocs/sweetcaroline.
  • Create a new folder called imempty. (Refer to the syntax) for creating folders if you need help with this command.
  • List the contents of sweetcaroline.
ls sweetcaroline
config.txt config_backup.txt imempty you.txt
  • Try to delete the imempty directory with the rm command. (The command below will fail.)
rm imempty
  • I won't work. By default, rm does not delete directories. This is a basic safeguard to make sure that you don't delete many files accidentally.
  • Use the following command to recursively delete imempty and any (nonexistant in this case) subdirectories.
rm -r imempty
  • Run ls to ensure that the imempty directory has been removed. Note that the text files still exist in the sweetcaroline directory.
  • Run the following command to change working directories to the mydocs directory.
cd ..
  • Delete the sweetcaroline directory.
rm -r sweetcaroline
  • Verify that the mydocs directory is empty using the ls command.

Files and Directories with Spaces

In the Linux world, the use of spaces in files and directory names is discouraged. Spaces just complicate things from the command line. The solution for using spaces is to put quotes around file and directory names.

  • Ensure that your working directory is ~/mydocs using the pwd command.
  • Create a directory called it works with the following command.
mkdir "it works"
  • List the files with ls.
ls

This will output the following.

'it works'
  • Notice that the terminal put single quotes around the filename. The single quotes are not actually part of the filename, that is merely the terminal's way of telling you that it's a directory, not two separate directories named it and works.
  • Create a file named "imma file.txt" with the following command.
touch "imma file.txt"
  • List the contents of mydocs with ls.
  • Delete the new file and directory with the following commands.
rm "imma file.txt"
rm "it works" -r

Practice with Files and Directories

  • Create a new directory in mydocs with the name of a fruit.
  • Create three files in the new directory with the names of people you know (e.g., alice, bob).
  • Rename the files to the names of different colors (e.g., red, blue, yellow).
  • Delete the files one by one.
  • Delete the directory you created with the fruit name.

Tips:

  • Pay attention to your working directory using pwd.
  • You may need to move around using cd.

Reflection

  • Why is organizing files in directories a best practice?
  • How might the command line interface be more efficient than a graphical user interface for working with files and directories?

Key Terms

  • Linux File: A basic unit of storage in a Linux operating system that can contain data, be it text, binary, or executable code. Linux files are organized within a hierarchical directory structure and can represent various types of data, including regular files, directories, symbolic links, device files, and more. Files in Linux are managed by the file system, which provides mechanisms for creating, reading, writing, and deleting files.