3 ways to delete specific files in a directory using the bash shell

Source: Internet
Author: User
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 except for files in the following format, other files:

1.*.iso-all ISO format files.
2.*.zip-Files in all zip formats.

How do I remove a specific file from a bash shell 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 file name.
3.[...]-matches any one of the letters enclosed in parentheses.

Strategy #1: Take a look at 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 pattern characters, which are the following:

1.? (Pattern list)-matches 0 or one time given pattern.
2.* (Pattern list)-matches 0 or more times for a given pattern.
3.+ (Pattern list)-matches a given pattern at least once.
4.@ (Pattern list)-matches a given pattern once.
5.! (Pattern list)-does not match the given pattern.

A list of patterns is one or more uses | The separated mode (file name).

The first thing to do is open the EXTGOLB option:

Shopt-s Extglob

Delete all files except. zip and. iso files in bash
The syntax format for the RM command is:

# # Keep only the file1 file # #rm  ! file1) # # Keep only file1 and file2 files # # RM  ! ( File1|file2) # # keep only zip files # #rm  ! *.zip) # # keep only zip and ISO files # #rm  ! *.zip|*.iso) # # You can also use the full catalog # #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

From bash (1) manual page:

This is a colon-separated list of patterns that define the set of files to ignore by way of path expansion. If a file matching to the path expansion mode also matches the pattern in Globignore, it is removed from the matching list.

To delete all files only keep the zip and ISO files, you should set Globignore as follows:

# # only works in BASH # #cd ~/downloads/globignore=*.zip:*.isorm-v *unset Globignore

Policy #3: Delete all other files with the Find command to keep only the zip and ISO files

If you are using Tcsh/csh/sh/ksh or another shell, you can try to delete the file using the syntax format of the Find command on the Unix-like system:

find/dir/-type f-not-name ' matching mode '-delete

Or

# # for weird filenames You 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 [option] {}

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 for preserving only the *.zip and *.iso files is as follows:

Find. -type f-not \ (-name ' *zip '-or-name ' *iso ' \)-delete


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.