mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-23 21:20:42 +08:00
translated
This commit is contained in:
parent
48cbc92168
commit
c6b3377b1d
@ -1,223 +0,0 @@
|
||||
[#]: 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: "geekpi"
|
||||
[#]: 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
|
@ -0,0 +1,224 @@
|
||||
[#]: 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: "geekpi"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
终端基础 #6:在 Linux 中删除文件和文件夹
|
||||
======
|
||||
|
||||
在终端基础系列的前几章中,你学习了[创建新文件][1]和目录(文件夹)。
|
||||
|
||||
现在让我们看看如何在 Linux 终端中删除文件和文件夹。
|
||||
|
||||
### 删除文件
|
||||
|
||||
要删除文件,你可以按以下方式使用 rm 命令:
|
||||
|
||||
```
|
||||
rm filename_or_path
|
||||
```
|
||||
|
||||
如果文件已成功删除,你将看不到任何输出。
|
||||
|
||||
这是一个示例,其中我删除了一个名为 `new_file` 的文件。当我列出目录内容时,你可以看到 `new_file` 不再存在。
|
||||
|
||||
![Removing files in Linux terminal][2]
|
||||
|
||||
你还可以在同一命令中删除多个文件:
|
||||
|
||||
```
|
||||
rm file1 file2 file3
|
||||
```
|
||||
|
||||
让我展示一个在单条命令中删除两个文件的示例。
|
||||
|
||||
![Deleting multiple files in single rm command][3]
|
||||
|
||||
#### 🏋️练习文件删除
|
||||
|
||||
让我们练习一下刚刚学到的东西。创建一个名为 practice_delete 的目录并切换到该目录:
|
||||
|
||||
```
|
||||
mkdir practice_delete && cd practice_delete
|
||||
```
|
||||
|
||||
现在创建一些空文件:
|
||||
|
||||
```
|
||||
touch file1 file2 file3
|
||||
```
|
||||
|
||||
删除 file3:
|
||||
|
||||
```
|
||||
rm file3
|
||||
```
|
||||
|
||||
现在,让我们做一些额外的事情。运行此命令并更改 file2 的权限:
|
||||
|
||||
```
|
||||
chmod u-w file1 file2
|
||||
```
|
||||
|
||||
现在尝试删除 file2:
|
||||
|
||||
```
|
||||
rm file2
|
||||
```
|
||||
|
||||
你是否看到消息 “**remove write protected file**”? 那是因为你从这个文件中删除了写权限(用于修改)。
|
||||
|
||||
你可以**按 Y 或回车键确认删除或按 N 拒绝删除。**
|
||||
|
||||
如果你不想看到这条消息并仍然删除它,你可以使用强制删除选项 `-f`。通过删除 `file1` 试试:
|
||||
|
||||
```
|
||||
rm -f file1
|
||||
```
|
||||
|
||||
以下是上述所有示例的重放:
|
||||
|
||||
![Deleting files in Linux terminal][4]
|
||||
|
||||
> 🚧 Linux 命令行中没有垃圾桶。一旦文件被删除,你就无法像在图形文件管理器中那样撤消将其从垃圾箱中取回的操作。因此,删除文件时要格外小心。
|
||||
|
||||
#### 小心删除
|
||||
|
||||
缺少垃圾桶使删除成为一种永久性的工作。这就是为什么你应该注意要删除的文件的原因。
|
||||
|
||||
有一个带 `-i` 选项的交互模式。有了这个,你会被要求确认删除。
|
||||
|
||||
```
|
||||
rm -i filename
|
||||
```
|
||||
|
||||
当你根据特定模式删除多个文件时,这很有用。
|
||||
|
||||
这是一个示例,其中我以交互方式删除名称中匹配 file_ 模式的所有文件。我删除了一些并在交互模式下保留了一些。
|
||||
|
||||
![Deleting files in interactive mode][5]
|
||||
|
||||
> 💡 我建议切换到文件所在的目录,然后删除它们。这有助于减少由文件路径中的拼写错误引起的任何可能性。
|
||||
|
||||
### 删除目录
|
||||
|
||||
在 Linux 中有专门的 rmdir 命令来删除目录。
|
||||
|
||||
```
|
||||
rmdir dir_name
|
||||
```
|
||||
|
||||
但是,它只能删除空目录。如果目录中有任何文件或子目录,rmdir 命令将抛出错误。
|
||||
|
||||
```
|
||||
[email protected]:~/practice_delete$ rmdir dir2
|
||||
rmdir: failed to remove 'dir2': Directory not empty
|
||||
```
|
||||
|
||||
这使得它在大多数情况下用处不大。
|
||||
|
||||
那么,如何删除非空文件夹呢? 好吧,使用与之前删除文件相同的 rm 命令。
|
||||
|
||||
是的,相同的 rm 命令,但带有递归选项 `-r`:
|
||||
|
||||
|
||||
```
|
||||
rm -r dir_name
|
||||
```
|
||||
|
||||
#### 🏋️练习文件夹删除
|
||||
|
||||
让我们练习你学到的东西。
|
||||
|
||||
如果你还没有,请切换到 practice_delete 文件夹。现在,创建两个目录 dir1 和 dir2。
|
||||
|
||||
```
|
||||
mkdir dir1 dir2
|
||||
```
|
||||
|
||||
在 dir2 中创建一个文件:
|
||||
|
||||
```
|
||||
touch dir2/file
|
||||
```
|
||||
|
||||
现在尝试使用 rmdir 命令删除目录:
|
||||
|
||||
```
|
||||
rmdir dir1
|
||||
```
|
||||
|
||||
```
|
||||
rmdir dir2
|
||||
```
|
||||
|
||||
由于 dir2 不为空,rmdir 命令将失败。相反,使用带有递归选项的 rm 命令:
|
||||
|
||||
```
|
||||
rm -r dir2
|
||||
```
|
||||
|
||||
以下是上述所有命令示例的重放:
|
||||
|
||||
![Deleting folders in Linux][6]
|
||||
|
||||
> 💡 交互式删除模式在使用 rm 命令的递归选项删除目录时更有帮助:
|
||||
|
||||
```
|
||||
rm-ri dir_name
|
||||
```
|
||||
|
||||
因此,你学会了使用 Linux 命令删除文件和文件夹。是时候多练习了。
|
||||
|
||||
### 测试你的知识
|
||||
|
||||
准备一个如下所示的目录树:
|
||||
|
||||
```
|
||||
.
|
||||
├── dir1
|
||||
│ ├── file1
|
||||
│ ├── file2
|
||||
│ └── file3
|
||||
├── dir2
|
||||
├── dir3
|
||||
└── file
|
||||
```
|
||||
|
||||
基本上,你在当前目录 (practice_delete) 中创建一个名为 file 的文件和三个目录 dir1、dir2 和 dir3。然后在 dir1 中创建文件 file1、file2 和 file3。
|
||||
|
||||
现在执行以下操作:
|
||||
|
||||
- 删除 `file2`。
|
||||
- 切换到 `dir3` 并强制删除上层目录中名为 `file` 的文件。
|
||||
- 删除 dir1 的所有内容,但不删除目录本身。
|
||||
- 列出 `dir` 的内容。
|
||||
|
||||
我鼓励你在 [It's FOSS 社区论坛][7]中讨论练习题。
|
||||
|
||||
一切进展顺利。你已经学习了一些基本知识,例如切换目录、检查目录内容、创建和删除文件和目录。在下一章中,你将学习如何在终端中复制文件和文件夹。敬请关注!
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://itsfoss.com/delete-files-folders-linux/
|
||||
|
||||
作者:[Abhishek Prakash][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者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