Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

deleting a lot of files in a directory

 

deleting a lot of files in a directory


Hi,

I wanna make a script to delete alot of files in a directory.

Today I do it this way but it is not a good solution:

ls -1 VFSweden1LIB1051001* | xargs rm
ls -1 VFSweden1LIB1051002* | xargs rm
ls -1 VFSweden1LIB1051003* | xargs rm
ls -1 VFSweden1LIB1051004* | xargs rm
ls -1 VFSweden1LIB1051005* | xargs rm

and so on..

I want to create a script doing this for me.

Complete name of files:

VFSweden1LIB105110100010050007

I want the script to replace the:

1051101

with

1051102, 1051103 and so on..

I hope you understand me and can help me with this..

Thanks and Regards,
Paul
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor

Re: deleting a lot of files in a directory

Hi Paul:

# cd chosen_path
# find . -xdev -type f -name "VFSweden1LIB10511*" | xargs rm

Regards!

...JRF...
Geoff Wild
Honored Contributor

Re: deleting a lot of files in a directory

find /somedir -name VFSweden1LIB* |xargs rm -f {};

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Sandman!
Honored Contributor

Re: deleting a lot of files in a directory

Hi Paul,

IMHO...you don't need a script just the "rm" command with a wildcard character in the filename serving as a placeholder for all similarly named files. Assuming that your current working dir is the one that contains those files then you could simply do:

# rm VFSweden1LIB105*00010050007

cheers!
Arturo Galbiati
Esteemed Contributor

Re: deleting a lot of files in a directory

Hi Paul,
why not simply?
rm VFSweden1LIB105110[1-9]00010050007

This will remove file from VFSweden1LIB105110100010050007 to
VFSweden1LIB105110900010050007

HTH,
Art
Yogeeraj_1
Honored Contributor

Re: deleting a lot of files in a directory

hi,

maybe the "mtime" switch to "find" can also help. (assuming the files are timestamped)

or also create a loop that increments a counter starting from 1051101 ...

e.g.
count=1051101
while test $count -lt 1051121
do
#do something for 0,1,2...9
echo "deleting VFSweden1LIB"$count
rm "VFSweden1LIB"$count
count=$(($count+1))
done

hope this helps too!

kind regards
yogeeraj


No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Kent Ostby
Honored Contributor

Re: deleting a lot of files in a directory

I tend to get a bit nervous about doing rm's when I can't see what's going to be done so I like to do something like:

# cd chosen_path

# find . -xdev -type f -name "VFSweden1LIB10511*" | awk '{print "rm ",$1}' > rm_script

Then you can visually check it to see if it's about the right size and then run it.

chmod +x rm_script
./rm_script

"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Chaz Emery
Advisor

Re: deleting a lot of files in a directory

Kent, I certainly agree not seeing what I am removing makes me nervous.

By the way, if you like using xargs in general, but especially when for file removal, the -t (trace) option is a nice and easy way to see exaclty what is happening or being removed:

example:
ls /tmp/garbage* | xargs -t rm
rm /tmp/garbage1 /tmp/garbage2 /tmp/garbage3

Or Even nicer when combined with the xargs -i (insert) option:
# ls /tmp/garbage* | xargs -t -i rm {}
rm /tmp/garbage1
rm /tmp/garbage2
rm /tmp/garbage3

If you want to use this in a script, be sure to redirect error output to stdout, because the trace output is considered debug or error output.
example: (no error redirect):
# ls /tmp/garbage* | xargs -t -i rm {} > out.txt
rm /tmp/garbage1
rm /tmp/garbage2
rm /tmp/garbage3

# cat out.txt
{{{EMPTY}}}

example: (with error redirected to stdout)
# ls /tmp/garbage* | xargs -t -i rm {} > out.txt 2>&1
{{{No screen Output}}}
# cat out.txt
rm /tmp/garbage1
rm /tmp/garbage2
rm /tmp/garbage3

example: If you want output to show in both file and screen, use '|tee' instead of '>' for the initial redirect of stdout to a file:
# ls /tmp/garbage* | xargs -t -i rm {} |tee out.txt 2>&1
rm /tmp/garbage1
rm /tmp/garbage2
rm /tmp/garbage3
# cat out.txt
rm /tmp/garbage1
rm /tmp/garbage2
rm /tmp/garbage3