diff --git a/sources/tech/20161025 3 Ways to Delete All Files in a Directory Except One or Few Files with Extensions.md b/sources/tech/20161025 3 Ways to Delete All Files in a Directory Except One or Few Files with Extensions.md deleted file mode 100644 index b017d82bc7..0000000000 --- a/sources/tech/20161025 3 Ways to Delete All Files in a Directory Except One or Few Files with Extensions.md +++ /dev/null @@ -1,180 +0,0 @@ -yangmingming translating -# 3 Ways to Delete All Files in a Directory Except One or Few Files with Extensions - -Sometimes you get into a situation where you need to delete all files in a directory or simply cleanup a directory by removing all files except files of a given type (ending with a particular extension). - -In this article, we will show you how to delete files in a directory except certain file extensions or types using rm, find and globignore commands. - -Before we move any further, let us start by briefly having a look at one important concept in Linux – filename pattern matching, which will enable us to deal with our issue at hand. - -In Linux, a shell pattern is a string that consists of the following special characters, which are referred to as wildcards or metacharacters: - -1. `*` – matches zero or more characters -2. `?` – matches any single character -3. `[seq]` – matches any character in seq -4. `[!seq]` – matches any character not in seq - -There are three possible methods we shall explore here, and these include: - -### Delete Files Using Extended Pattern Matching Operators - -The different extended pattern matching operators are listed below, where pattern-list is a list containing one or more filenames, separated using the `|` character: - -1. `*(pattern-list)` – matches zero or more occurrences of the specified patterns -2. `?(pattern-list)` – matches zero or one occurrence of the specified patterns -3. +(pattern-list) – matches one or more occurrences of the specified patterns -4. `@(pattern-list)` – matches one of the specified patterns -5. `!(pattern-list)` – matches anything except one of the given patterns - -To use them, enable the extglob shell option as follows: - -``` -# shopt -s extglob - -``` - -#### 1. To delete all files in a directory except filename, type the command below: - - ``` -$ rm -v !("filename") - - ``` - [![Delete All Files Except One File in Linux](http://www.tecmint.com/wp-content/uploads/2016/10/DeleteAll-Files-Except-One-File-in-Linux.png)][9] - - Delete All Files Except One File in Linux - -#### 2. To delete all files with the exception of filename1 and filename2: - - ``` -$ rm -v !("filename1"|"filename2") - - ``` - [![Delete All Files Except Few Files in Linux](http://www.tecmint.com/wp-content/uploads/2016/10/Delete-All-Files-Except-Few-Files-in-Linux.png)][8] - - Delete All Files Except Few Files in Linux - -#### 3. The example below shows how to remove all files other than all `.zip` files interactively: - - ``` -$ rm -i !(*.zip) - - ``` - [![Delete All Files Except Zip Files in Linux](http://www.tecmint.com/wp-content/uploads/2016/10/Delete-All-Files-Except-Zip-Files-in-Linux.png)][7] - - Delete All Files Except Zip Files in Linux - -#### 4. Next, you can delete all files in a directory apart from all `.zip` and `.odt` files as follows, while displaying what is being done: - - ``` -$ rm -v !(*.zip|*.odt) - - ``` - [![Delete All Files Except Certain File Extensions](http://www.tecmint.com/wp-content/uploads/2016/10/Delete-All-Files-Except-Certain-File-Extensions.png)][6] - - Delete All Files Except Certain File Extensions - - Once you have all the required commands, turn off the extglob shell option like so: - - ``` -$ shopt -u extglob - - ``` - -### Delete Files Using Linux find Command - -Under this method, we can [use find command exclusively][5] with appropriate options or in conjunction with xargscommand by employing a pipeline as in the forms below: - - ``` -$ find /directory/ -type f -not -name 'PATTERN' -delete -$ find /directory/ -type f -not -name 'PATTERN' -print0 | xargs -0 -I {} rm {} -$ find /directory/ -type f -not -name 'PATTERN' -print0 | xargs -0 -I {} rm [options] {} - - ``` - -#### 5. The following command will delete all files apart from `.gz` files in the current directory: - - ``` -$ find . -type f -not -name '*.gz'-delete - - ``` - [![Command find - Remove All Files Except .gz Files](http://www.tecmint.com/wp-content/uploads/2016/10/Remove-All-Files-Except-gz-Files.png)][4] - - Command find – Remove All Files Except .gz Files - -#### 6. Using a pipeline and xargs, you can modify the case above as follows: - - ``` -$ find . -type f -not -name '*gz' -print0 | xargs -0 -I {} rm -v {} - - ``` - [![Remove Files Using find and xargs Commands](http://www.tecmint.com/wp-content/uploads/2016/10/Remove-Files-Using-Find-and-Xargs-Command.png)][3] - - Remove Files Using find and xargs Commands - -#### 7. Let us look at one additional example, the command below will wipe out all files excluding `.gz`, `.odt`, and `.jpg` files in the current directory: - - ``` -$ find . -type f -not \(-name '*gz' -or -name '*odt' -or -name '*.jpg' \) -delete - - ``` - [![Remove All Files Except File Extensions](http://www.tecmint.com/wp-content/uploads/2016/10/Remove-All-Files-Except-File-Extensions.png)][2] - - Remove All Files Except File Extensions - -### Delete Files Using Bash GLOBIGNORE Variable - -This last approach however, only works with bash. Here, the GLOBIGNORE variable stores a colon-separated pattern-list (filenames) to be ignored by pathname expansion. - -To employ this method, move into the directory that you wish to clean up, then set the GLOBIGNORE variable as follows: - -``` -$ cd test -$ GLOBIGNORE=*.odt:*.iso:*.txt - -``` - -In this instance, all files other than `.odt`, `.iso`, and `.txt` files with be removed from the current directory. - -Now run the command to clean up the directory: - -``` -$ rm -v * - -``` - -Afterwards, turn off GLOBIGNORE variable: - -``` -$ unset GLOBIGNORE - -``` -[![Delete Files Using Bash GLOBIGNORE Variable](http://www.tecmint.com/wp-content/uploads/2016/10/Delete-Files-Using-Bash-GlobIgnore.png)][1] - -Delete Files Using Bash GLOBIGNORE Variable - -Note: To understand the meaning of the flags employed in the commands above, refer to the man pages of each command we have used in the various illustrations. - -Thats all! If you have any other command line techniques in mind for the same purpose, do not forget to share with us via our feedback section below. - --------------------------------------------------------------------------------- - -via: http://www.tecmint.com/delete-all-files-in-directory-except-one-few-file-extensions/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+tecmint+%28Tecmint%3A+Linux+Howto%27s+Guide%29 - -作者:[ Aaron Kili][a] - -译者:[译者ID](https://github.com/译者ID) - -校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 - -[a]: http://www.tecmint.com/author/aaronkili/ -[1]:http://www.tecmint.com/wp-content/uploads/2016/10/Delete-Files-Using-Bash-GlobIgnore.png -[2]:http://www.tecmint.com/wp-content/uploads/2016/10/Remove-All-Files-Except-File-Extensions.png -[3]:http://www.tecmint.com/wp-content/uploads/2016/10/Remove-Files-Using-Find-and-Xargs-Command.png -[4]:http://www.tecmint.com/wp-content/uploads/2016/10/Remove-All-Files-Except-gz-Files.png -[5]:http://www.tecmint.com/35-practical-examples-of-linux-find-command/ -[6]:http://www.tecmint.com/wp-content/uploads/2016/10/Delete-All-Files-Except-Certain-File-Extensions.png -[7]:http://www.tecmint.com/wp-content/uploads/2016/10/Delete-All-Files-Except-Zip-Files-in-Linux.png -[8]:http://www.tecmint.com/wp-content/uploads/2016/10/Delete-All-Files-Except-Few-Files-in-Linux.png -[9]:http://www.tecmint.com/wp-content/uploads/2016/10/DeleteAll-Files-Except-One-File-in-Linux.png diff --git a/translated/tech/20161025 3 Ways to Delete All Files in a Directory Except One or Few Files with Extensions.md b/translated/tech/20161025 3 Ways to Delete All Files in a Directory Except One or Few Files with Extensions.md new file mode 100755 index 0000000000..68cdb7f5b6 --- /dev/null +++ b/translated/tech/20161025 3 Ways to Delete All Files in a Directory Except One or Few Files with Extensions.md @@ -0,0 +1,178 @@ +# 删除在一个目录下除了一个或者一些带扩展名文件的其他所有文件的三种方法 + +有的时候,你可能会遇到这种情况,你需要删除一个目录下的所有文件,或者只是简单的通过删除除了一些指定类型(以指定扩展名结尾)的文件来清空一个目录。 + +在这篇文章,我们将会向你展现如何通过 rm、 find 和 globignore 命令删除一个目录下除了指定文件后缀或者类型的的文件。 + +在我们进一步深入之前,让我们开始简要的了解一下 Linux 中的一个重要的概念 —— 文件名模式匹配,它可以让我们解决眼前的问题。 + +在 Linux 下,一个 shell 模式一个包含以下特殊字符的字符串,称为通配符或者元字符: + +1. `*` – 匹配 0 个或者多个字符 +2. `?` – 匹配任意单个字符 +3. `[seq]` – 匹配序列中的任意一个字符 +4. `[!seq]` – 匹配任意一个不再序列中的字符 + +我们将在这儿探索三种可能的办法,包括: + +### 使用扩展模式匹配操作符删除文件 + +下来列出了不同的扩展模式匹配操作符,这些模式列表是一个用 `|` 分割包含一个或者多个文件名的列表: + +1. `*(pattern-list)` – 匹配 0 个或者多个出现的指定模式 +2. `?(pattern-list)` – 匹配 0 个或者 1 个出现的指定模式 +4. `@(pattern-list)` – 匹配 1 个或者多个出现的指定模式 +5. `!(pattern-list)` – 匹配除了一个指定模式之外的任何内容 + +为了使用它们,像下面一样打开 extglob shell 选项: + +``` +# shopt -s extglob + +``` + +#### 1. 输入以下命令,删除一个目录下除了 filename 之外的所有文件 + + ``` +$ rm -v !("filename") + + ``` + [![删除 Linux 下除了一个文件之外的所有文件](http://www.tecmint.com/wp-content/uploads/2016/10/DeleteAll-Files-Except-One-File-in-Linux.png)][9] + + 删除 Linux 下除了一个文件之外的所有文件 + +#### 2. 删除除了 filename1 和 filename2 之外的所有文件 + + ``` +$ rm -v !("filename1"|"filename2") + + ``` + [![在 Linux 下删除除了一些文件之外的所有文件](http://www.tecmint.com/wp-content/uploads/2016/10/Delete-All-Files-Except-Few-Files-in-Linux.png)][8] + + 在 Linux 下删除除了一些文件之外的所有文件 + +#### 3. 下面的例子显示如何通过交互模式删除除了 `.zip` 之外的所有文件 + + ``` +$ rm -i !(*.zip) + + ``` + [![在 Linux 下删除除了 Zip 文件之外的所有文件](http://www.tecmint.com/wp-content/uploads/2016/10/Delete-All-Files-Except-Zip-Files-in-Linux.png)][7] + + 在 Linux 下删除除了 Zip 文件之外的所有文件 + +#### 4. 接下来,通过如下的方式你可以删除一个目录下除了所有的`.zip` 和 `.odt` 文件的所有文件,并且在删除的时候,显示正在删除的文件: + + ``` +$ rm -v !(*.zip|*.odt) + + ``` + [![删除除了指定文件扩展的所有文件](http://www.tecmint.com/wp-content/uploads/2016/10/Delete-All-Files-Except-Certain-File-Extensions.png)][6] + + 删除除了指定文件扩展的所有文件 + +一旦你已经执行了所有需要的命令,使用如下的方式关闭 extglob shell 选项。 + + ``` +$ shopt -u extglob + + ``` + +### 使用 Linux 下的 find 命令删除文件 + +在这种方法下,我们可以[只使用 find 命令][5]的适当的选项或者采用管道配合 xargs 命令,如下所示: + + ``` +$ find /directory/ -type f -not -name 'PATTERN' -delete +$ find /directory/ -type f -not -name 'PATTERN' -print0 | xargs -0 -I {} rm {} +$ find /directory/ -type f -not -name 'PATTERN' -print0 | xargs -0 -I {} rm [options] {} + + ``` + +#### 5. 下面的命令将会删除当前目录下除了 `.gz` 之外的所有文件 + + ``` +$ find . -type f -not -name '*.gz' -delete + + ``` + [![find 命令 —— 删除 .gz 之外的所有文件](http://www.tecmint.com/wp-content/uploads/2016/10/Remove-All-Files-Except-gz-Files.png)][4] + + find 命令 —— 删除 .gz 之外的所有文件 + +#### 6. 使用管道和 xargs,你可以通过如下的方式修改上面的例子: + + ``` +$ find . -type f -not -name '*gz' -print0 | xargs -0 -I {} rm -v {} + + ``` + [![使用 find 和 xargs 命令删除文件](http://www.tecmint.com/wp-content/uploads/2016/10/Remove-Files-Using-Find-and-Xargs-Command.png)][3] + + 使用 find 和 xargs 命令删除文件 + +#### 7. 让我们看一个额外的例子,下面的命令行将会抹除掉当前目录下除了 `.gz`、 `.odt` 和 `.jpg` 之外的所有文件: + + ``` +$ find . -type f -not \(-name '*gz' -or -name '*odt' -or -name '*.jpg' \) -delete + + ``` + [![删除除了指定扩展文件的所有文件](http://www.tecmint.com/wp-content/uploads/2016/10/Remove-All-Files-Except-File-Extensions.png)][2] + + 删除除了指定扩展文件的所有文件 + +### 通过 bash 中的 GLOBIGNORE 变量删除文件 + +然而,最后的方法,只适用于 bash。 GLOBIGNORE 变量存储了一个通过路径名扩展忽略的分离的模式(或者文件名)列表。 + +为了使用这种方法,移动到要删除文件的目录,像下面这样设置 GLOBIGNORE 变量: + +``` +$ cd test +$ GLOBIGNORE=*.odt:*.iso:*.txt + +``` + +在这种情况下,除了 `.odt`、 `.iso` 和 `.txt` 之外的所有文件,都将从当前目录删除。 + +现在,运行如下的命令清空这个目录: + +``` +$ rm -v * + +``` + +之后,关闭 GLOBIGNORE 变量: + +``` +$ unset GLOBIGNORE + +``` +[![使用 bash 变量 GLOBIGNORE 删除文件](http://www.tecmint.com/wp-content/uploads/2016/10/Delete-Files-Using-Bash-GlobIgnore.png)][1] + +使用 bash 变量 GLOBIGNORE 删除文件 + +注:为了理解上面的命令行采用的标识的意思,请参考我们在每一个插图中使用的命令对应的 man 手册。 + +就这些了!如果你心里有实现相同目录的其他命令行技术,不要忘了通过下面的反馈部分分享给我们。 + +-------------------------------------------------------------------------------- + +via: http://www.tecmint.com/delete-all-files-in-directory-except-one-few-file-extensions/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+tecmint+%28Tecmint%3A+Linux+Howto%27s+Guide%29 + +作者:[ Aaron Kili][a] + +译者:[yangmingming](https://github.com/yangmingming) + +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: http://www.tecmint.com/author/aaronkili/ +[1]:http://www.tecmint.com/wp-content/uploads/2016/10/Delete-Files-Using-Bash-GlobIgnore.png +[2]:http://www.tecmint.com/wp-content/uploads/2016/10/Remove-All-Files-Except-File-Extensions.png +[3]:http://www.tecmint.com/wp-content/uploads/2016/10/Remove-Files-Using-Find-and-Xargs-Command.png +[4]:http://www.tecmint.com/wp-content/uploads/2016/10/Remove-All-Files-Except-gz-Files.png +[5]:http://www.tecmint.com/35-practical-examples-of-linux-find-command/ +[6]:http://www.tecmint.com/wp-content/uploads/2016/10/Delete-All-Files-Except-Certain-File-Extensions.png +[7]:http://www.tecmint.com/wp-content/uploads/2016/10/Delete-All-Files-Except-Zip-Files-in-Linux.png +[8]:http://www.tecmint.com/wp-content/uploads/2016/10/Delete-All-Files-Except-Few-Files-in-Linux.png +[9]:http://www.tecmint.com/wp-content/uploads/2016/10/DeleteAll-Files-Except-One-File-in-Linux.png