From 413747da51cf1bffd820f1b07cdd036e584618b5 Mon Sep 17 00:00:00 2001 From: Linchenguang <1573453077@qq.com> Date: Sat, 14 Jun 2014 17:01:30 +0800 Subject: [PATCH] =?UTF-8?q?[=E7=BF=BB=E8=AF=91=E5=AE=8C=E6=88=90]Linux--Ba?= =?UTF-8?q?sh=20Delete=20All=20Files=20In=20Directory=20Except=20Few?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...elete All Files In Directory Except Few.md | 112 ------------------ ...elete All Files In Directory Except Few.md | 111 +++++++++++++++++ 2 files changed, 111 insertions(+), 112 deletions(-) delete mode 100644 sources/tech/20140607 Linux--Bash Delete All Files In Directory Except Few.md create mode 100644 translated/tech/20140607 Linux--Bash Delete All Files In Directory Except Few.md diff --git a/sources/tech/20140607 Linux--Bash Delete All Files In Directory Except Few.md b/sources/tech/20140607 Linux--Bash Delete All Files In Directory Except Few.md deleted file mode 100644 index 54a66f6d9f..0000000000 --- a/sources/tech/20140607 Linux--Bash Delete All Files In Directory Except Few.md +++ /dev/null @@ -1,112 +0,0 @@ ->> chenguang is translating - -Linux: Bash Delete All Files In Directory Except Few -================================================================================ -![](http://s0.cyberciti.org/images/category/old/terminal.png) - -I'm a new Linux system user. I need to cleanup in a download directory i.e. delete all files from ~/Downloads/ folders except the following types: - -*.iso - All iso images files. -*.zip - All zip files. - -How do I delete all file except some in bash shell on a Linux, OS X or Unix-like systems? - -Bash shell supports rich file pattern matching such as follows: - -- * - Match any files. -- ? - Matches any single character in filenames. -- [...] - Matches any one of the enclosed characters. - -### Method #1: Say hello to extended pattern matching operators ### - -You need to use the extglob shell option using the shopt builtin command to use extended pattern matching operators such as: - -1. ?(pattern-list) - Matches zero or one occurrence of the given patterns. -1. *(pattern-list) - Matches zero or more occurrences of the given patterns. -1. +(pattern-list) - Matches one or more occurrences of the given patterns. -1. @(pattern-list) - Matches one of the given patterns. -1. !(pattern-list) - Matches anything except one of the given patterns. - -A pattern-list is nothing but a list of one or more patterns (filename) separated by a |. First, turn on extglob option: - - shopt -s extglob - -#### Bash remove all files except *.zip and *.iso files #### - -The rm command syntax is: - - ## Delete all file except file1 ## - rm !(file1) - - ## Delete all file except file1 and file2 ## - rm !(file1|file2) - - ## Delete all file except all zip files ## - rm !(*.zip) - - ## Delete all file except all zip and iso files ## - rm !(*.zip|*.iso) - - ## You set full path too ## - rm /Users/vivek/!(*.zip|*.iso|*.mp3) - - ## Pass options ## - rm [options] !(*.zip|*.iso) - rm -v !(*.zip|*.iso) - rm -f !(*.zip|*.iso) - rm -v -i !(*.php) - -Finally, turn off extglob option: - - shopt -u extglob - -### Method #2: Using bash GLOBIGNORE variable to remove all files except specific ones ### - -From the [bash(1)][1] page: - -> A colon-separated list of patterns defining the set of filenames to be ignored by pathname expansion. If a filename matched by a pathname expansion pattern also matches one of the patterns in GLOBIGNORE, it is removed from the list of matches. - -To delete all files except zip and iso files, set GLOBIGNORE as follows: - - ## only works with BASH ## - cd ~/Downloads/ - GLOBIGNORE=*.zip:*.iso - rm -v * - unset GLOBIGNORE - -### Method #3: Find command to rm all files except zip and iso files ### - -If you are using tcsh/csh/sh/ksh or any other shell, try the following find command syntax on a Unix-like system to delete files: - - find /dir/ -type f -not -name 'PATTERN' -delete - -OR - - ## deals with weird file names using xargs ## - find /dir/ -type f -not -name 'PATTERN' -print0 | xargs -0 -I {} rm {} - find /dir/ -type f -not -name 'PATTERN' -print0 | xargs -0 -I {} rm [options] {} - -To delete all files except php files in ~/sources/ directory, type: - - find ~/sources/ -type f -not -name '*.php' -delete - -OR - - find ~/sources/ -type f -not -name '*.php' -print0 | xargs -0 -I {} rm -v {} - -The syntax to delete all files except *.zip and *.iso is as follows: - - find . -type f -not \( -name '*zip' -or -name '*iso' \) -delete - -For more information see [bash command man page][1] and [find command man page][2]. - --------------------------------------------------------------------------------- - -via: http://www.cyberciti.biz/faq/linux-bash-delete-all-files-in-directory-except-few/ - -译者:[译者ID](https://github.com/译者ID) 校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出 - -[1]:http://www.manpager.com/linux/man1/bash.1.html -[2]:http://www.manpager.com/linux/man1/find.1.html diff --git a/translated/tech/20140607 Linux--Bash Delete All Files In Directory Except Few.md b/translated/tech/20140607 Linux--Bash Delete All Files In Directory Except Few.md new file mode 100644 index 0000000000..da9ce214c9 --- /dev/null +++ b/translated/tech/20140607 Linux--Bash Delete All Files In Directory Except Few.md @@ -0,0 +1,111 @@ +Linux:使用bash删除目录中的特定文件 +================================================================================ +![](http://s0.cyberciti.org/images/category/old/terminal.png) + +我是一个Linux新手用户。现在我需要清理一个下载目录中的文件,其实我就是想删除~/Download/文件夹下面除了以下格式的文件外所以其它文件: +*.iso - 所有的iso格式的文件。 +*.zip - 所有zip格式的文件。 + +我如何在一个基于Linux,OS X 或者Unix-like系统上的bash shell中删除特定的文件呢? + +Bash shell 支持丰富的文件模式匹配符例如: + +- * - 匹配所有的文件。 +- ? - 匹配文件名中的单个字母。 +- [...] - 匹配封闭括号中的任何一个字母。 + +### 策略 #1: 见识一下扩展的模式匹配符 ### + +这里你需要用系统内置的shopt命令来开启shell中的extglob选项,然后你就可以使用扩展的模式符了,这些模式匹配符如下: + +1. ?(pattern-list) - 匹配零次或一次给定的模式。 +1. *(pattern-list) -至少匹配零次给定的模式。 +1. +(pattern-list) - 至少匹配一次给定的模式。 +1. @(pattern-list) - 匹配一次给定的模式。 +1. !(pattern-list) - 匹配所有除给定模式以外的模式。 + +一个模式列表就是一个或多个用 | 分开的模式(文件名)。首先打开extgolb选项: + + shopt -s extglob + +#### 在Bash中删掉除*.zip和*.iso文件以外的所有文件 #### + +rm 命令的语法格式为: + + ## 仅保留 file1 文件 ## + rm !(file1) + + ## 仅保留 file1 和 file2 文件## + rm !(file1|file2) + + ## 仅保留 zip 文件 ## + rm !(*.zip) + + ## 仅保留 zip 和 iso 文件 ## + rm !(*.zip|*.iso) + + ## 你也可以使用完整的目录 ## + rm /Users/vivek/!(*.zip|*.iso|*.mp3) + + ## 传递参数 ## + rm [options] !(*.zip|*.iso) + rm -v !(*.zip|*.iso) + rm -f !(*.zip|*.iso) + rm -v -i !(*.php) + +最后,关闭 extglob 选项: + + shopt -u extglob + +### 策略 #2: 使用bash的 GLOBIGNORE 变量删除指定文件以外的所有文件 ### + +摘自 [bash(1)][1] 手册页: +> 一个用冒号分开的模式列表定义了被路径扩展忽略的文件的集合。如果一个文件同时与路径扩展模式和GLOBIGNORE中的模式匹配,那么它就从匹配列表中移除了。 + +要删除所有文件只保留 zip 和 iso 文件,应如下设置 GLOBIGNORE: + + ## 只在 BASH 中可行 ## + cd ~/Downloads/ + GLOBIGNORE=*.zip:*.iso + rm -v * + unset GLOBIGNORE + +### 策略 #3: 用 find 命令删除所有其它文件仅保留 zip 和 iso 文件 ### + + +如果你正在使用 tcsh/csh/sh/ksh 或者其它shell,你可以在Unix-like系统上试着用下面find命令的语法格式来删除文件: + + find /dir/ -type f -not -name 'PATTERN' -delete + +或者 + + ## 对于怪异的文件名可以使用 xargs ## + find /dir/ -type f -not -name 'PATTERN' -print0 | xargs -0 -I {} rm {} + find /dir/ -type f -not -name 'PATTERN' -print0 | xargs -0 -I {} rm [options] {} + + +为了删除 ~/source 目录下除 php 以外的文件,键入: + + find ~/sources/ -type f -not -name '*.php' -delete + +或者 + + find ~/sources/ -type f -not -name '*.php' -print0 | xargs -0 -I {} rm -v {} + +只保留 *.zip 和 *.iso 文件的语法如下: + + find . -type f -not \( -name '*zip' -or -name '*iso' \) -delete + + +更多信息参见[bash command man page][1]和[find command man page][2]。 + +-------------------------------------------------------------------------------- + +via: http://www.cyberciti.biz/faq/linux-bash-delete-all-files-in-directory-except-few/ + +译者:[Linchenguang](https://github.com/Linchenguang) 校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出 + +[1]:http://www.manpager.com/linux/man1/bash.1.html +[2]:http://www.manpager.com/linux/man1/find.1.html