0

I've got a couple of thousand images that are saved as logs that need to be deleted.

To avoid the limit of rm and to do this across multiple servers, I used the following code

 Net::SSH::Multi.start(:on_error => :ignore) do |session|

    # define servers in groups for more granular access
    session.group :app do
      session.use 'example@example', :password=> 'example'
    end

# execute commands on a subset of servers
 session.with(:app).exec "find /tmp/motion -maxdepth 1 -not -name 'lastsnap.jpg' -print0 | sudo xargs -0 rm"
end

An ls -l lastsnap.jpg shows that lastsnap.jpg is linked to another file, like so

30 Jun 3 08:18 lastsnap.jpg -> 81-20140603081840-snap.jpg

This other file is constantly changed due to logging scenario that i mentioned above.

Reiterating the question, how do I delete every other logged file that is NOT lastsnap.jpg and it's linked file.

Thanks for the help :)

Kishe
  • 9
  • 3

2 Answers2

0
cd /tmp/motion
ls -1 | grep -v -E '$(basename `find . -lname lastsnap.jpg`)|lastsnap.jpg' | while read n ; do rm -rvf $n ; done

EDIT as per the comment

cd /tmp/motion; rm -rvf $(ls -1 | grep -v -E "$(basename `find . -lname lastsnap.jpg`)|lastsnap.jpg")

Note: Make sure that your file names don't have spaces in it. Other wise this method will not work and needs modification in order to accommodate spaces in the file name.

Suku
  • 3,820
  • 1
  • 21
  • 23
  • Is there any way to do this in one line? I forgot to mention that i'll have to do this across multiple servers and i'm using NET::SSH::MULTI, i'll edit the post for this – Kishe Jun 23 '14 at 04:35
  • I just ran this code through my boss and he told me not to use -rvf in case we need to whitelist anything else. can we do it such that it explicitly deletes only the jpg's that we're looking for and not the other way round? – Kishe Jun 23 '14 at 06:41
  • @Kishe this will explicitly delete the jpg's which we are looking only. – Suku Jun 23 '14 at 07:27
  • sorry for another dumb question, but i think your edit is deleting the linked file D: – Kishe Jun 24 '14 at 07:12
  • also i'm getting basename: missing operand <- what gives? :O – Kishe Jun 24 '14 at 07:54
  • *note the ' ' * changing this to '$(basename `find . -lname lastsnap.jpg`)|lastsnap.jpg'solved it – Kishe Jun 24 '14 at 08:07
  • @Kishe Thanks for correction. I have modified the same in answer. – Suku Jun 24 '14 at 09:04
0

I wrote a logic using find command. Check whether its useful to you. My directory contains following files

pyramid-stone.jpg
tallest_water_slide.jpg
SAOLA.JPG
testnap.jpg
silicon_valley_talent.jpg
The_Organic_Battery_From_Japan.jpg

Out of which testnap.jpg is a link

testnap.jpg -> pyramid-stone.jpg

So i wrote a small awk script to get the link name and where its pointing to

IG1=`ls -l | grep ^l | awk '{printf $(NF-2);}'`
IG2=`ls -l | grep ^l | awk '{printf $(NF);}'`

Then i used find command to print all jpg's instead of the link

find . -type f \( -iname "*.jpg" ! -iname $IG1 ! -iname $IG2 \)

OP is

./SAOLA.JPG
./silicon_valley_talent.jpg
./tallest_water_slide.jpg
./The_Organic_Battery_From_Japan.jpg

NOTE:You have add rm to remove files after the find command

Raghuram
  • 3,937
  • 2
  • 19
  • 25