linux怎么删掉配置文件_如何在Linux中删除文件?

linux怎么删掉配置文件

linux怎么删掉配置文件

Deleting files in Linux can be sometimes tricky. We have a tool named rm which is the shortcut for the word remove. In this tutorial, we will look at how to remove or delete a file in Linux with different examples and ways.

在Linux中删除文件有时可能很棘手。 我们有一个名为rm的工具,它是remove一词的快捷方式。 在本教程中,我们将通过不同的示例和方法来研究如何在Linux中删除或删除文件。

rm命令语法 (rm Command Syntax)

rm  command syntax is the same as the most the Linux command. We can provide options before specifying the file and directories we cant to operate or delete.

rm命令的语法与大多数Linux命令相同。 在指定我们无法操作或删除的文件和目录之前,我们可以提供选项。

rm OPTIONS FILENAME
  • OPTINS states the behavior of the rm  command as we will see below in detail.

    OPTINS声明rm命令的行为,我们将在下面详细介绍。

  • FILENAME is the file or directory name we want to delete or operate.

    FILENAME是我们要删除或操作的文件或目录名称。

rm命令帮助 (rm Command Help)

rm command help information can be printed to the console with the --help command which is like below. Help information will provide some popular options and usages.

可以使用--help命令将rm命令帮助信息打印到控制台,如下所示。 帮助信息将提供一些流行的选项和用法。

$ rm --help
rm Command Help
rm Command Help
rm命令帮助

使用rm命令删除单个文件(Delete Single File with rm Command)

We will start with simple steps just deleting a single file. We will just specify the file name we want to delete. In order to remove the file successfully, we should have privileges to modify a file. For example, if we try to remove the file owned by root with a regular user we will get an error and would not delete the file. In this example, we will delete the file named foo.txt

我们将从删除单个文件的简单步骤开始。 我们将只指定我们要删除的文件名。 为了成功删除文件,我们应该具有修改文件的权限。 例如,如果我们尝试用普通用户删除root拥有的文件,则会收到错误消息,并且不会删除该文件。 在此示例中,我们将删除名为foo.txt的文件

$ rm foo.txt

使用rm命令删除多个文件 (Delete Multiple Files with rm Command)

We have the ability to delete multiple files in a single rm  command. We will just put file names we want to delete by separating them with space. In this example, we will delete file names foo.txt and bar.txt but we can add more if we need it.

我们可以在一个rm命令中删除多个文件。 我们将要删除的文件名用空格分隔,以放置它们。 在此示例中,我们将删除文件名foo.txtbar.txt但如果需要,可以添加更多文件名。

$ rm foo.txt bar.txt

使用rm命令根据文件的扩展名/类型删除文件 (Delete Files According To Their Extensions/Types with rm Command)

Linux bahs provides the glob or asterisk in order to specify the files with a specific name or extension. We can use glob * in order to specify a specific extension like *.txt , *.pdf , *.tmp etc. We can use this extension or name specification in order to delete specific files. In this example, we will delete all *.deb extensions.

Linux bahs提供glob或星号以指定具有特定名称或扩展名的文件。 我们可以使用glob *来指定特定的扩展名,例如*.txt*.pdf*.tmp等。我们可以使用此扩展名或名称说明来删除特定的文件。 在此示例中,我们将删除所有*.deb扩展名。

$ rm -v *.deb
Delete Files According To Their Extensions/Types
Delete Files According To Their Extensions/Types
根据文件的扩展名/类型删除文件

We can also specify names like deleting all files which name starts with pof like below.

我们还可以指定名称,例如删除所有以pof开头的文件,如下所示。

$ rm pof*

递归删除文件 (Delete Files Recursively)

rm command provides the ability to delete or remove files recursively. Recursive removal will check subdirectories for files to remove with the directories. We will remove the directory name ndiff with all sub-directories and files in this example. We will use -R option for the recursive operation.

rm命令提供了递归删除或删除文件的功能。 递归删除将检查子目录中要与目录一起删除的文件。 在此示例中,我们将删除所有子目录和文件的目录名称ndiff 。 我们将使用-R选项进行递归操作。

$ rm -R -v ndiff
Delete Files Recursively
Delete Files Recursively
递归删除文件

每次删除前先删除文件(Delete File with Prompt Before Every Removal)

While removing files and directories we may need approval for each file to delete. In this case, we can use -i option which will prompt to accept or deny deletion of the given file.

在删除文件和目录时,我们可能需要批准删除每个文件。 在这种情况下,我们可以使用-i选项,提示您接受或拒绝删除给定文件。

$ rm -R -i test
Remove or Delete File with Prompt Before Every Removal
Remove or Delete File with Prompt Before Every Removal
每次删除之前,请先删除或删除文件

打印详细输出有关删除操作(Print Verbose Output About Delete Operation)

While deleting files and directories we may want to see details of the removal operation. rm  command provides a verbose option which will list information about each deletion of file or directory. We will use -voption for this.

在删除文件和目录时,我们可能希望查看删除操作的详细信息。 rm命令提供了一个详细选项,它将列出有关文件或目录每次删除的信息。 我们将使用-v选项。

$ rm -R -v test
Print Verbose Output About Delete Operation
Print Verbose Output About Delete Operation
打印详细输出有关删除操作

使用rmdir命令删除空目录或文件夹(Delete empty Directories or Folders with rmdir Command)

In some cases, we need to delete empty folders. rm without options will not work in this case as we can see this in the following screenshot. We case use rmdir command to remove an empty directory or folder.

在某些情况下,我们需要删除空文件夹。 不带选项的rm在这种情况下不起作用,因为我们可以在以下屏幕截图中看到这一点。 我们用rmdir命令删除空目录或文件夹。

$ rmdir delete_me/
Delete empty Directories or Folders
Delete empty Directories or Folders
删除空目录或文件夹

从文本文件中读取文件名以进行删除或删除(Read File Names From Text File For Delete or Removal)

Another interesting use case for rm command is providing file or directory names from a list like a text file. We will use xargs  command to-read the list and redirect to the rm command.

rm命令的另一个有趣用例是从列表(如文本文件)中提供文件或目录名称。 我们将使用xargs命令读取列表并重定向到rm命令。

$ xargs rm < delete_file.txt
Read File Names From Text File For Delete or Removal
Read File Names From Text File For Delete or Removal
从文本文件中读取文件名以进行删除或删除

删除文件名以短划线开头-(Delete File Names Starts with Dash -)

Another interesting case is dash or - problem where file or directory names starting with dash . As we know Linux commands options are specified with dash. So how can rm recognize file name from option? We will use -- or double dash were to specify the file or directory name start. For example we have a file named -file.txt and we want to remove. We will use the following command. As we can see the file name is specified after double dash. Options are specified before the double dash.

另一个有趣的情况是dash-问题,其中文件或目录名称以dash开头。 众所周知,Linux命令选项由dash指定。 那么, rm如何从选项中识别文件名? 我们将使用--或双破折号分别指定文件或目录名称的开始。 例如,我们有一个名为-file.txt的文件,我们要删除。 我们将使用以下命令。 如我们所见,文件名在双破折号后指定。 选项在双破折号之前指定。

$ rm -i -- -file.txt

通过从文件/列表中读取文件名来删除文件 (Delete Files By Reading Their Names From A File/List)

In some cases, we may need to read a list that contains the file names we want to delete. This is generally a simple text file where each file name with their path is specified line by line. We can use xargs command to redirect the list contents to the rm command which will delete them one by one. In this example, we will read the list file names file-list.txt.

在某些情况下,我们可能需要阅读包含要删除的文件名的列表。 这通常是一个简单的文本文件,其中逐行指定每个文件名及其路径。 我们可以使用xargs命令将列表内容重定向到rm命令,它将逐一删除它们。 在此示例中,我们将读取列表文件名file-list.txt

$ xargs rm -v < file-list.txt
Delete Files By Reading Their Names From A File/List
Delete Files By Reading Their Names From A File/List
通过从文件/列表中读取文件名来删除文件

通过使用find命令查找文件来删除文件(Delete Files By Finding them with find Command)

find is a very useful command which is used to find files and folders. find command also provides some options like running commands on the search results. We can also remove or delete files found by the find command. In this example, we will delete files that are older than 3 days.

find是一个非常有用的命令,用于查找文件和文件夹。 find命令还提供一些选项,例如在搜索结果上运行命令。 我们还可以删除或删除find命令find文件。 在此示例中,我们将删除早于3天的文件。

$ find . -type f -mtime +3 -exec rm '{}' ';' -print
Delete Files By Finding them with find Command
Delete Files By Finding them with find Command
通过使用find命令查找文件来删除文件
LEARN MORE  How To Remove or Delete A Directory In Linux?
了解更多如何在Linux中删除或删除目录?

翻译自: https://www.poftut.com/delete-file-linux/

linux怎么删掉配置文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值