> Good day,
> I need to write a shellscript and I hope you can help me:
> I have got 30 directories, each has a different name. In the
> directories, files are stored which are built up like this: XXX.000,
> XXX.001 ....up to XXX.500
> XXX can be everything from i.e. test1(.066) to actock77(.003).
> Now I need to find the file with the highest extension (i.e. XXX.050)
> and delete all files that have a lower extension (XXX.000 - XXX.049),
> so that only XXX.050 remains. Approximately 2300 files are to be
> deleted.
> After that, the computer should switch to the next directory and
> continue the progress.
One way to list the files in order is ls -r XXX.*
and we do not want the highest one so pipe to sed -n '2,$p'
and then we want to remove them: pipe to xargs -r rm
(Gnu xargs has the -r option which means do nothing if
there are no files -- if your xargs does not have this then
you need to work out what should happen if there are
no files. Or install Gnu xargs. Or create "dummy" files first.
Or count how many files there are first.)
So, assuming we know the directories in advance (say, A, B, C):
for dir in A B C
do
ls -r i${dir}/XXX.* | sed -n '2,$p'
done | xargs -r rm
Or you may decide it is clearer if the xargs is inside the loop:
for dir in A B C
do
ls -r i${dir}/XXX.* | sed -n '2,$p' | xargs -r rm
done
if we do not know the directories in advance, then we can do:
for dir in `find topleveldir -type d`
...
John.