I am a new Linux user. Now I need to clean up a download directory of files, in fact, I just want to delete from the ~/download/folder in addition to the following format files so other files:
1.*.iso-All documents in ISO format.
2.*.zip-All files in zip format.
How do I delete a particular file in a bash shell based on a linux,os X or unix-like system?
The Bash shell supports rich file pattern matching characters such as:
1.*-matches all the files.
2.? -Matches a single letter in the filename.
3.[...]-matches any one of the letters in the enclosing parentheses.
Strategy #1: See the extended pattern-matching character
Here you need to use the system's built-in SHOPT command to open the EXTGLOB option in the shell, and then you can use the extended mode character, which matches the following:
1.? (Pattern list)-matches a given pattern 0 or more times.
2.* (Pattern list)-matches a given pattern 0 or more times.
3.+ (schema list)-matches a given pattern at least once.
4.@ (schema list)-matches the given pattern once.
5.! (schema list)-does not match the given pattern.
A list of patterns is one or more uses | A separate pattern (filename).
First, you open the EXTGOLB option:
Copy Code code as follows:
Delete all files except the. zip and. iso files in bash
The syntax format for the RM command is:
Copy Code code as follows:
# # Keep only file1 files # #
Rm! (File1)
# # Keep only file1 and file2 files # #
Rm! (File1|file2)
# # Keep Zip files only # #
Rm! (*.zip)
# # Keep zip and ISO files only # #
Rm! (*.zip|*.iso)
# # You can also use the full directory # #
rm/users/vivek/! (*.zip|*.iso|*.mp3)
# # can also pass parameters # #
RM [Options]! (*.zip|*.iso)
Rm-v! (*.zip|*.iso)
Rm-f! (*.zip|*.iso)
RM-V-I! (*.php)
Policy #2: Use bash's globignore variable to delete all files except the specified file
Excerpt from bash (1) man page:
Copy Code code as follows:
This is a list of patterns separated by colons that define the collection of files to ignore by path expansion. If a file matching to the path expansion pattern also matches the pattern in Globignore, it is removed from the matching list.
To delete all files only the zip and ISO files are retained, set Globignore as follows:
Copy Code code as follows:
# # only works in BASH #
CD ~/downloads/
Globignore=*.zip:*.iso
RM-V *
Unset Globignore
Policy #3: Delete all other files with find command only zip and ISO files
If you are using a tcsh/csh/sh/ksh or other shell, you can try to delete the file on the Unix-like system using the syntax format of the Find command below:
Copy Code code as follows:
find/dir/-type f-not-name ' matching mode '-delete
Or
Copy Code code as follows:
# # for weird filenames can use Xargs # #
find/dir/-type f-not-name ' matching mode '-print0 | Xargs-0-I {} RM {}
find/dir/-type f-not-name ' matching mode '-print0 | Xargs-0-I {} RM [options] {}
To delete files in the ~/source directory other than PHP, type:
Copy Code code as follows:
Find ~/sources/-type f-not-name ' *.php '-delete
Or
Copy Code code as follows:
Find ~/sources/-type f-not-name ' *.php '-print0 | Xargs-0-I {} rm-v {}
The syntax for retaining only the *.zip and *.iso files is as follows:
Copy Code code as follows:
Find. -type f-not \ (-name ' *zip '-or-name ' *iso ')-delete