Operating System - HP-UX
1834018 Members
2139 Online
110063 Solutions
New Discussion

Re: finding and removing a file that ends in .<number>

 
SOLVED
Go to solution
Vic S. Kelan
Regular Advisor

finding and removing a file that ends in .<number>

Hi,

I would like to search for and delete files ending in a *..

For example test.3457, test.1233

find / -name 'test.*' -exec rm -f {} \;
would take out all files with test.* including files such as test.dontdelete for example. I want to restrict this to only .

Any script I can use for this kind of task available or can find do this somehow?

Thanks!
8 REPLIES 8
Rick Garland
Honored Contributor

Re: finding and removing a file that ends in .<number>

You can change the -print function to the -exec rm {}\;

This will list the files so you can be sure you want to remove them.

find / -name "*.[0-9]" -print
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: finding and removing a file that ends in .<number>

find . -type f -name 'test.*' | grep -E -e '.*\.[0-9]*$' | while read FNAME
do
echo "${FNAME}"
# rm ${FNAME}
done

Uncomment the rm when you are confident.
If it ain't broke, I can fix that.
Rick Garland
Honored Contributor

Re: finding and removing a file that ends in .<number>

1 thing I just noticed, this will only find those files with 1 numerical digit at the end.

The files you are wanting, do they all end in 4 digits? In which case,

find / -name "*.[0-9][0-9][0-9][0-9]" -print
Rick Garland
Honored Contributor

Re: finding and removing a file that ends in .<number>

Clay has better solution...
Vic S. Kelan
Regular Advisor

Re: finding and removing a file that ends in .<number>

Superb!
Clay, Rick thanks a lot.

Both worked and as you suggested Rick, Clays hits it on the spot!!
THANKS!!! and such prompt response too!!!
A. Clay Stephenson
Acclaimed Contributor

Re: finding and removing a file that ends in .<number>

Actually, I didn't quite get it right because I would also match on any file that ends in simply "." because I am matching zero or more digits but what I should have matches was one or more digits:

WRONG: !!!!
find . -type f -name 'test.*' | grep -E -e '.*\.[0-9]*$' | while read FNAME
do
echo "${FNAME}"
# rm ${FNAME}
done

RIGHT:
find . -type f -name 'test.*' | grep -E -e '.*\.[0-9]+$' | while read FNAME
do
echo "${FNAME}"
# rm ${FNAME}
done

Note the change from '*' to '+' -- not that I'm nit-picky.

If it ain't broke, I can fix that.
Muthukumar_5
Honored Contributor

Re: finding and removing a file that ends in .<number>

You can try as,

find / -name "test.[0-9]*" | xargs rm -f

-exec rm -f {} will make problem when file count is increased. You can use xargs to avoid that.

hth.
Easy to suggest when don't know about the problem!
Amit Agarwal_1
Trusted Contributor

Re: finding and removing a file that ends in .<number>

If the file count is large, then you can use +(plus) delimiter instead of ;(semicolon) for -exec.

find . -exec rm -f {} \+

This will accumulate all the file names and will call 'rm -f' only once at the end. Same as xargs. However if the command line goes beyond a limit size, find would break it into multiple commands and execute them intelligently.

-Amit