Deleting Files and Directories (rm) - Images: Japanese
Hey there, everyone!
Next up — let's look at how to delete files and directories.
To delete files and directories, use the rm command. Let's walk through how it works.
Starting with file deletion: create a test.txt file using touch:
[root@localhost ~]# touch test.txt [root@localhost ~]# ls test.txt
Now run rm test.txt:
[root@localhost ~]# rm test.txt
After hitting Enter, you'll see a prompt like this:
[root@localhost ~]# rm test.txt rm: remove regular empty file `test.txt'?
It's asking for confirmation. Type "y" or "Y" and press Enter:
[root@localhost ~]# rm test.txt rm: remove regular empty file `test.txt'? y
The prompt returns and the deletion is complete. Verify with ls:
[root@localhost ~]# rm test.txt rm: remove regular empty file `test.txt'? y [root@localhost ~]# ls
test.txt is gone. That's the basic usage.
By the way, on CentOS the confirmation logic works like this: "if the first character of the response is 'y' or 'Y', treat it as yes." So something like this is technically valid:
[root@localhost ~]# touch test.txt [root@localhost ~]# rm test.txt rm: remove regular empty file `test.txt'? y Miku Hatsune! [root@localhost ~]# ls
As long as the first character is "y" or "Y", it counts. Conversely, to cancel, just make sure the first character is anything else — or simply press Enter without typing anything.
Now let's look at deleting directories. Create a test directory with mkdir:
[root@localhost ~]# mkdir test
Try removing it with a plain rm:
[root@localhost ~]# rm test rm: cannot remove `test': is a directory
An error — "test is a directory." The rm command without any options cannot remove directories. To remove a directory, add the -r option:
[root@localhost ~]# rm -r test
You'll get the same confirmation prompt as before — type "y" to confirm:
[root@localhost ~]# rm -r test rm: remove directory `test'? y
Done. The usage isn't too complicated.
Now let's look at a couple of useful options. The confirmation prompt is helpful for safety, but it can get tedious when there are many files to delete. For example, create a directory with three files inside:
[root@localhost ~]# mkdir test [root@localhost ~]# touch test/test1.txt [root@localhost ~]# touch test/test2.txt [root@localhost ~]# touch test/test3.txt
Try deleting:
[root@localhost ~]# mkdir test [root@localhost ~]# touch test/test1.txt [root@localhost ~]# touch test/test2.txt [root@localhost ~]# touch test/test3.txt [root@localhost ~]# rm -r test/ rm: descend into directory `test'? y rm: remove regular empty file `test/test3.txt'? y rm: remove regular empty file `test/test1.txt'? y rm: remove regular empty file `test/test2.txt'? y rm: remove directory `test'? y
That's a lot of confirmations. With 100,000 files you'd be there all day. Add the '-f' option to skip all confirmations and delete immediately:
[root@localhost ~]# mkdir test [root@localhost ~]# touch test/test1.txt [root@localhost ~]# touch test/test2.txt [root@localhost ~]# touch test/test3.txt [root@localhost ~]# rm -rf test/ [root@localhost ~]# ls
Gone in one shot — no prompts.
If you want to keep a record of what was deleted, add the '-v' option. It prints each deleted item to standard output:
[root@localhost ~]# mkdir test [root@localhost ~]# touch test/test1.txt [root@localhost ~]# touch test/test2.txt [root@localhost ~]# touch test/test3.txt [root@localhost ~]# rm -rfv test removed `test/test3.txt' removed `test/test1.txt' removed `test/test2.txt' removed directory: `test'
And since the output goes to standard output, you can combine it with > from the previous article to save it to a file:
[root@localhost ~]# mkdir test [root@localhost ~]# touch test/test1.txt [root@localhost ~]# touch test/test2.txt [root@localhost ~]# touch test/test3.txt [root@localhost ~]# rm -rfv test > remove_list.txt [root@localhost ~]# cat remove_list.txt removed `test/test3.txt' removed `test/test1.txt' removed `test/test2.txt' removed directory: `test'
Useful for keeping deletion logs.
One critical warning: files deleted with rm are gone permanently — there is no recovery.
On Windows or macOS, deleting a file just moves it to the Trash, where you can restore it. With rm, there's no Trash. Once deleted, the data is gone. If the file is important, make a backup with the cp command before deleting. We'll cover cp in the next article.
As mentioned in an earlier article, there's also a command called rmdir for removing directories.
It works, but only on empty directories. Anything with content inside will produce an error. For practical purposes, rmdir is not something you need to learn — just use rm -r instead.
That wraps up file and directory deletion. In the next article, we'll cover copying files and directories. See you there!
This article was written by Sakurama.
Author's beloved small mammal |
桜舞 春人 Sakurama HarutoA Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him. |
If you find any errors or copyright issues, please contact us.