Trusted answers to developer questions
Free System Design Interview Course
Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2024 with this popular free course.
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 rm
command.
Let’s see it in more detail below.
Remove a single file
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.
Terminal 1
Terminal
Loading...
Now, we can securely perform our deletion task on these files. The syntax is:
rm <filename>
rm
is short for remove. To delete the file cv_yves.doc, we’ll type the command rm cv_yves.doc
.
Beware: This
rm
command 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:
rm path/to/file
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
Terminal 1
Terminal
Loading...
To delete the file1.txt file, write:
rm test-rm/file1.txt
Note:
- If the file is protected, you can force it to delete with the
-f
(force) flag:rm -f <filename>
- You can also delete a file with the
unlink
command:unlink cv_salimas.pdf
Do you want to delete more files at once? Then, let’s move to the next section.
Remove multiple files
To delete multiple files, just pass their names, with spaces between them, after the rm
command:
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:
Terminal 1
Terminal
Loading...
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.
rm CV_*
(ii) delete all pdf files
rm *.pdf
(iii) delete all files
rm *
Note:
- If the files are located in another directory, don’t forget to pass their path(s) after
rm
.- You can use all the flags (
-i
,-f
) we learned about in the first section for the deletion of multiple filesunlink
can’t be used to delete multiple files
Summary
The 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.
Exercises
In the Terminal below:
- delete all .txt files located in the exercise-rm directory
- delete the protected.md file from the current directory
Terminal 1
Terminal
Loading...
RELATED TAGS
general
CONTRIBUTOR
Abel Lifaefi Mbula
Keep Exploring
Related Courses
TRENDING TOPICS
Copyright ©2024 Educative, Inc. All rights reserved.
Did you find this helpful?