mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-29 21:41:00 +08:00
Merge pull request #28945 from lkxed/20230323-1-Terminal-Basics-6-Delete-Files-and-Folders-in-Linux
[手动选题][tech]: 20230323.1 ⭐️ Terminal Basics 6 Delete Files and Folders in Linux.md
This commit is contained in:
commit
e0ac4e42ca
@ -0,0 +1,223 @@
|
|||||||
|
[#]: subject: "Terminal Basics #6: Delete Files and Folders in Linux"
|
||||||
|
[#]: via: "https://itsfoss.com/delete-files-folders-linux/"
|
||||||
|
[#]: author: "Abhishek Prakash https://itsfoss.com/author/abhishek/"
|
||||||
|
[#]: collector: "lkxed"
|
||||||
|
[#]: translator: " "
|
||||||
|
[#]: reviewer: " "
|
||||||
|
[#]: publisher: " "
|
||||||
|
[#]: url: " "
|
||||||
|
|
||||||
|
Terminal Basics #6: Delete Files and Folders in Linux
|
||||||
|
======
|
||||||
|
|
||||||
|
In the earlier chapters of the Terminal Basics series, you learned to [create new files][1] and directories (folders).
|
||||||
|
|
||||||
|
Let's now see how you can delete files and folders in the Linux terminal.
|
||||||
|
|
||||||
|
### Deleting files
|
||||||
|
|
||||||
|
To remove files, you can use the rm command in the following fashion:
|
||||||
|
|
||||||
|
```
|
||||||
|
rm filename_or_path
|
||||||
|
```
|
||||||
|
|
||||||
|
You won't see any output if the file is successfully deleted.
|
||||||
|
|
||||||
|
Here's an example where I removed one of the files named `new_file`. When I list the directory contents, you can see that `new_file` no longer exists.
|
||||||
|
|
||||||
|
![Removing files in Linux terminal][2]
|
||||||
|
|
||||||
|
You can also remove multiple files in the same command:
|
||||||
|
|
||||||
|
```
|
||||||
|
rm file1 file2 file3
|
||||||
|
```
|
||||||
|
|
||||||
|
Let me show an example of deleting two files in a single command.
|
||||||
|
|
||||||
|
![Deleting multiple files in single rm command][3]
|
||||||
|
|
||||||
|
#### 🏋️Exercise file deletion
|
||||||
|
|
||||||
|
Let's practice what you just learned. Create a directory named practice_delete and switch to it:
|
||||||
|
|
||||||
|
```
|
||||||
|
mkdir practice_delete && cd practice_delete
|
||||||
|
```
|
||||||
|
|
||||||
|
Now create a few empty files:
|
||||||
|
|
||||||
|
```
|
||||||
|
touch file1 file2 file3
|
||||||
|
```
|
||||||
|
|
||||||
|
Delete the file3:
|
||||||
|
|
||||||
|
```
|
||||||
|
rm file3
|
||||||
|
```
|
||||||
|
|
||||||
|
Now, let's do something extra. Run this command and change the permission on file2:
|
||||||
|
|
||||||
|
```
|
||||||
|
chmod u-w file1 file2
|
||||||
|
```
|
||||||
|
|
||||||
|
Try deleting file2 now:
|
||||||
|
|
||||||
|
```
|
||||||
|
rm file2
|
||||||
|
```
|
||||||
|
|
||||||
|
Do you see a message '**remove write protected file**'? That's because you removed the write permission (for modification) from this file.
|
||||||
|
|
||||||
|
You can **press Y or enter key to confirm the deletion or N to deny the removal.**
|
||||||
|
|
||||||
|
If you don't want to see this message and still delete it, you can use the force delete option `-f`. Try it by deleting `file1`:
|
||||||
|
|
||||||
|
```
|
||||||
|
rm -f file1
|
||||||
|
```
|
||||||
|
|
||||||
|
Here's a replay of all the above examples to help you:
|
||||||
|
|
||||||
|
![Deleting files in Linux terminal][4]
|
||||||
|
|
||||||
|
> 🚧 There is no trash bin in the Linux command line. Once the file is deleted, you cannot undo the action to bring it back from the trash bin as you do in the graphical file manager. For this reason, be extra careful while deleting the files.
|
||||||
|
|
||||||
|
#### Remove but with caution
|
||||||
|
|
||||||
|
The lack of trash bin makes the deletion a permanent jobs of sort. This is why you should be careful about what files are you deleting.
|
||||||
|
|
||||||
|
There is an interactive mode with option `-i`. With this, you'll be asked to confirm the deletion.
|
||||||
|
|
||||||
|
```
|
||||||
|
rm -i filename
|
||||||
|
```
|
||||||
|
|
||||||
|
This is helpful when you are deleting several files based on a certain pattern.
|
||||||
|
|
||||||
|
Here's an example where I am interactively deleting all the files that match file_ pattern in their name. I delete some and keep some in the interactive mode.
|
||||||
|
|
||||||
|
![Deleting files in interactive mode][5]
|
||||||
|
|
||||||
|
> 💡 I advise switching to the directory where the files are located and then removing them. This helps in reducing any potential caused by a typo in file path.
|
||||||
|
|
||||||
|
### Deleting directories
|
||||||
|
|
||||||
|
There is a dedicated rmdir command to remove directories in Linux.
|
||||||
|
|
||||||
|
```
|
||||||
|
rmdir dir_name
|
||||||
|
```
|
||||||
|
|
||||||
|
However, it can only delete empty directories. If the directory has any files or subdirectories in it, the rmdir command will throw error.
|
||||||
|
|
||||||
|
```
|
||||||
|
[email protected]:~/practice_delete$ rmdir dir2
|
||||||
|
rmdir: failed to remove 'dir2': Directory not empty
|
||||||
|
```
|
||||||
|
|
||||||
|
And that makes it less useful in most cases.
|
||||||
|
|
||||||
|
So, how do you delete a non-empty folder then? Well, you use the same rm command that you used earlier for removing files.
|
||||||
|
|
||||||
|
Yes, the same rm command but with the recursive option `-r`:
|
||||||
|
|
||||||
|
```
|
||||||
|
rm -r dir_name
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 🏋️Exercise folder deletion
|
||||||
|
|
||||||
|
Let's practice what you learned.
|
||||||
|
|
||||||
|
Switch to practice_delete folder if you are not already there. Now, create two directories dir1 and dir2.
|
||||||
|
|
||||||
|
```
|
||||||
|
mkdir dir1 dir2
|
||||||
|
```
|
||||||
|
|
||||||
|
Create a file in dir2:
|
||||||
|
|
||||||
|
```
|
||||||
|
touch dir2/file
|
||||||
|
```
|
||||||
|
|
||||||
|
Now try deleting the directories using the rmdir command:
|
||||||
|
|
||||||
|
```
|
||||||
|
rmdir dir1
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
rmdir dir2
|
||||||
|
```
|
||||||
|
|
||||||
|
Since the dir2 is not empty, rmdir command will fail. Instead, use the rm command with recursive option:
|
||||||
|
|
||||||
|
```
|
||||||
|
rm -r dir2
|
||||||
|
```
|
||||||
|
|
||||||
|
Here's a replay of all the above command examples to help you out:
|
||||||
|
|
||||||
|
![Deleting folders in Linux][6]
|
||||||
|
|
||||||
|
> 💡 The interactive deletion mode is even more helpful while deleting a directory with the recursive option of the rm command:
|
||||||
|
|
||||||
|
```
|
||||||
|
rm-ri dir_name
|
||||||
|
```
|
||||||
|
|
||||||
|
So, you learned to delete files and folders both using Linux commands. It's time to practice some more.
|
||||||
|
|
||||||
|
### Test your knowledge
|
||||||
|
|
||||||
|
Prepare a directory tree that looks like this:
|
||||||
|
|
||||||
|
```
|
||||||
|
.
|
||||||
|
├── dir1
|
||||||
|
│ ├── file1
|
||||||
|
│ ├── file2
|
||||||
|
│ └── file3
|
||||||
|
├── dir2
|
||||||
|
├── dir3
|
||||||
|
└── file
|
||||||
|
```
|
||||||
|
|
||||||
|
Basically, you create a file named file and three directories dir1, dir2 and dir3 in the current directory (practice_delete). And then you create files file1, file2 and file3 in dir1.
|
||||||
|
|
||||||
|
Now do the following:
|
||||||
|
|
||||||
|
- Delete `file2`.
|
||||||
|
- Switch to the `dir3` and force delete the file named `file` in the upper directory.
|
||||||
|
- Delete all the contents of dir1 but not the directory itself.
|
||||||
|
- List the contents of the `dir`.
|
||||||
|
|
||||||
|
I encourage you to discuss the practice questions in the [It's FOSS community forum][7].
|
||||||
|
|
||||||
|
This is going good. You have learned several basic things like switching directories, checking contents of directory, creating and deleting files and directories. In the next chapter, you'll learn about copying files and folders in the terminal. Stay tuned!
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
via: https://itsfoss.com/delete-files-folders-linux/
|
||||||
|
|
||||||
|
作者:[Abhishek Prakash][a]
|
||||||
|
选题:[lkxed][b]
|
||||||
|
译者:[译者ID](https://github.com/译者ID)
|
||||||
|
校对:[校对者ID](https://github.com/校对者ID)
|
||||||
|
|
||||||
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||||
|
|
||||||
|
[a]: https://itsfoss.com/author/abhishek/
|
||||||
|
[b]: https://github.com/lkxed/
|
||||||
|
[1]: https://itsfoss.com/create-files/
|
||||||
|
[2]: https://itsfoss.com/content/images/2023/03/delete-files-linux-terminal.png
|
||||||
|
[3]: https://itsfoss.com/content/images/2023/03/remove-multiple-files-linux-terminal.png
|
||||||
|
[4]: https://itsfoss.com/content/images/2023/03/file-delete-example.svg
|
||||||
|
[5]: https://itsfoss.com/content/images/2023/03/interactive-delete-example.svg
|
||||||
|
[6]: https://itsfoss.com/content/images/2023/03/folder-delete-example.svg
|
||||||
|
[7]: https://itsfoss.community/?ref=itsfoss.com
|
Loading…
Reference in New Issue
Block a user