In this shot, you will learn how to remove a file (or files) from your system using Shell (bash, zsh, etc.) In case you’ve forgotten, Shell (or terminal) is the (black) interface that allows you to “instruct” a computer through a text command.
To perform a deletion task, we use the
Let’s see it in more detail below.
Let’s start with a very simple scenario: removing one file from the current directory. For the demo, let’s create our files to work, as shown below:
touch cv_salimas.pdf cv_yves.doc songs_val.txt readme.txt unkis.doc projet_wmdrc.pdf
Type or copy the above command in the terminal (below) to create six empty files.
Now, we can securely perform our deletion task on these files. The syntax is:
rm is short for remove. To delete the file cv_yves.doc, we’ll type the command
rmcommand will permanently remove the file from your system. So, use it with caution. It’s even better to use it with the
-i(interactive) flag, which will prompt you to confirm the deletion. To confirm the deletion, hit the Y button or type yes.
You can practice in Shell below:
In the first part of this section, we discussed how to remove a file from the current directory. Now, lets see how to remove a file located in another directory. The syntax is:
First, type the following command to create files in a new directory:
mkdir test-rm && touch test-rm/file1.txt test-rm/file2.txt test-rm/file3.txt
To delete the file1.txt file, write:
- If the file is protected, you can force it to delete with the
rm -f <filename>
- You can also delete a file with the
Do you want to delete more files at once? Then, let’s move to the next section.
To delete multiple files, just pass their names, with spaces between them, after the
rm <file1> <file2> <file3> <etc>
Let’s try to delete these files (cv_salimas.pdf, songs_val.txt, unkis.doc) in in the terminal window below:
To make sure you’ve successfully deleted all the files above, type
ls. Now, you won’t see them in the list of files.
We can do even better using the wildcard character (*). This character means “any”. Let’s do some exercises so that you can better understand how to use this character.
(i) delete any file starting with CV_ character.
(ii) delete all pdf files
(iii) delete all files
- If the files are located in another directory, don’t forget to pass their path(s) after
- You can use all the flags (
-f) we learned about in the first section for the deletion of multiple files
unlinkcan’t be used to delete multiple files
rm command is used to delete one or more files located in the current directory – this operation is permanent. For that reason, you may need to use
rm with the
-i flag so that you can be prompted for confirmation.
That’s all for today – thank you for learning with me! Before you go, here are some exercises for you.
In the Terminal below:
View all Courses