What are the convenient ways to clean up a directory in Linux if you need to delete all files in the directory except for files of a specified type? The following will be shared for you.
shell mode in Linux is a string of the following special characters, also known as wildcards or metacharacters:
* - Matches zero or more characters
? - Matches any single character
[seq] - Matches any character in seq
[!seq] - Matches any characters that are not in the sequence
Delete files by extension pattern matching operators, here are the different extension pattern matching operators, where the pattern list is a list of one or more filenames, separated by characters | :
*(pattern-list) - Matches zero or more specified patterns
? (pattern-list) - Matches the specified pattern zero or once
+(pattern list) - Matches one or more instances of the specified pattern
@(pattern-list) - Matches one of the specified patterns
! (pattern-list) - Matches anything other than a given pattern
Start by enabling the extglob shell option:
# shopt -s extglob
If you need to delete all files except filename:
$ rm -v ! ("filename")
Delete all files except filename1 and filename2:
$ rm -v ! ("filename1"|"filename2")
The following example shows how to interactively delete all files except for all.zip files:
$ rm -i ! (*.zip)
You can delete all files except.zip and.odt files:
$ rm -v ! (*.zip|*.odt)
When you're done, you can turn off the extglob shell option as follows:
$ shopt -u extglob
Delete the file using the Linux find command, or use the pipeline in combination with the xargs command:
$ 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] {}
Delete all files except.gz:
$ find . -type f -not -name '*.gz'-delete
Using pipelines and xargs, you can modify the above situation as follows:
$ find . -type f -not -name '*gz' -print0 | xargs -0 -I {} rm -v {}
The following command will clear all files in the current directory except.gz,.odt, and files:.jpg:
$find. -type f - not \ (-name '* gz - or - the name' * odt - or - the name '*.jpg' \] - delete
But the last method only works for bash. Delete the file using the Bash GLOBIGNORE variable, which stores a list of schemas (file names) separated by colons and will be ignored by the pathname extension. With this method, go to the directory you want to clean and set the GLOBIGNORE variable as follows:
$ cd test
$ GLOBIGNORE=*.odt:*.iso:*.txt
In this case, all files except, and.odt will be removed from the.iso current.txt directory.
Now run the command to clean the directory:
$ rm -v *
After that, close the GLOBIGNORE variable:
$ unset GLOBIGNORE
Deleting a file is an operation that requires extreme caution. Before performing the deletion, it is important to clearly understand the motivation for deleting the file - whether it is to remove certain types of outdated files or simply to free up disk space. The purpose of this article is to provide a way to help you keep important files that end with a specific extension when you need to empty all the files in your directory.