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
touchcommand - Create folders using the
mkdircommand - Rename files using the
mvcommand - Copy files using the
cpcommand - Print the contents of files using the
catcommand - Use the
*wildcard character - Remove files and folders using the
rmcommand
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
pwdto 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
mydocsdirectories. Remember that case matters.
ls
ls mydocs
- You should see
me.txtlisted in the contents of themydocsdirectory.
$ ls mydocs
me.txt
- The
lscommand knows thatmydocsis a directory in the current working directory, so adding it to thelscommand tells thelscommand to list the files in that directory. - Adapt the previous commands to move
you.txtinto themydocsfolder. - Change directories to your
mydocsfolder. - When finished, run the
lscommand 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
cdcommand without any parameters is a shortcut to go to the home directory. This is the same ascd ~. - Change directories into
mydocsagain.
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
cpcommand 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. Usepwdto verify that this is your working directory. If you need to change your working directory, use thecdcommand below.
cd ~/mydocs
- Run the following command to create a
sweetcarolinedirectory.
mkdir sweetcaroline
- Again, no news is good news.
- List the files and directories in the
mydocsdirectory.
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.txtextension. 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
pwdto 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
pwdto 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
rmcommand. (The command below will fail.)
rm imempty
- I won't work. By default,
rmdoes 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
imemptyand any (nonexistant in this case) subdirectories.
rm -r imempty
- Run
lsto ensure that theimemptydirectory has been removed. Note that the text files still exist in thesweetcarolinedirectory. - Run the following command to change working directories to the
mydocsdirectory.
cd ..
- Delete the
sweetcarolinedirectory.
rm -r sweetcaroline
- Verify that the
mydocsdirectory is empty using thelscommand.
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
~/mydocsusing thepwdcommand. - Create a directory called
it workswith 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
itandworks. - Create a file named "imma file.txt" with the following command.
touch "imma file.txt"
- List the contents of
mydocswithls. - 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
mydocswith 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.