I am a new Linux user. Now I need to clean up the files in a download directory. In fact, I just want to delete all the files in the ~/Download/ folder except the files in the following formats:
1.*.iso - all iso formats document.
2.*.zip - All files in zip format.
How do I delete specific files in a bash shell on a Linux, OS X or Unix-like system?
Bash shell supports rich file pattern matching characters, such as:
1.* - matches all files.
2.? - Matches a single letter in a file name.
3.[...] - Matches any letter in the enclosing bracket.
Strategy #1: Meet the extended pattern matching characters
Here you need to use the system’s built-in shopt command to enable the extglob option in the shell, and then you can use the extended pattern matching characters , these pattern matching characters are as follows:
1.? (pattern list) - Matches the given pattern zero or once.
2.*(Pattern List) – Matches the given pattern zero or more times.
3.+(pattern list) - Match the given pattern at least once.
4.@(pattern list) - Match the given pattern once.
5.!(pattern list) - does not match the given pattern.
A pattern list is one or more patterns (file names) separated by |.
First turn on the extgolb option:
shopt -s extglob
Delete all files except .zip and .iso files in Bash
The syntax format of the rm command is:
## 仅保留 file1 文件 ## rm !(file1) ## 仅保留 file1 和 file2 文件## rm !(file1|file2) ## 仅保留 zip 文件 ## rm !(*.zip) ## 仅保留 zip 和 iso 文件 ## rm !(*.zip|*.iso) ## 你也可以使用完整的目录 ## rm /Users/vivek/!(*.zip|*.iso|*.mp3) ## 也可以传递参数 ## rm [选项] !(*.zip|*.iso) rm -v !(*.zip|*.iso) rm -f !(*.zip|*.iso) rm -v -i !(*.php)
Strategy #2: Use bash's GLOBIGNORE variable to delete all files except the specified file
Excerpted from the bash(1) man page:
This is a colon-separated list of patterns, by path The expansion mode defines the set of files to be ignored. If a file matching a path expansion pattern also matches a pattern in GLOBIGNORE, it is removed from the match list.
To delete all files and keep only zip and iso files, set GLOBIGNORE as follows:
## 只在 BASH 中可行 ## cd ~/Downloads/ GLOBIGNORE=*.zip:*.iso rm -v * unset GLOBIGNORE
Strategy #3: Use the find command to delete all other files and keep only zip and iso files
If you are using tcsh/csh/sh/ksh or other shells, you can try to delete files using the following find command syntax on Unix-like systems:
find /dir/ -type f -not -name '匹配模式' -delete
or
## 对于怪异的文件名可以使用 xargs ## find /dir/ -type f -not -name '匹配模式' -print0 | xargs -0 -I {} rm {} find /dir/ -type f -not -name '匹配模式' -print0 | xargs -0 -I {} rm [选项] {}
If you want to delete files other than php in the ~/source 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 keep only *.zip and *.iso files is as follows:
find . -type f -not \( -name '*zip' -or -name '*iso' \) -delete
For more shell articles related to deleting specific files, please pay attention to the PHP Chinese website!