0

How can I delete nay file that has a number in its name from directory /www/ and up? Like, it would delete

a1.php

a2.php

123213.php

asdasd5.php

Would this work?

ls | grep \([1-9]\) -f | xargs rm

I haven't tested it yet, don't want to mess up anything.

2
  • I suggest you create a test folder and chuck some test files into the folder.
    – miltonb
    Apr 1, 2014 at 0:39
  • 2
    this question belongs on Unix.SE
    – warren
    Apr 3, 2014 at 16:26

1 Answer 1

1

I would use find. Something like:

find /www/ -name "*[0-9]*" -type f -exec rm {} \;

This will find all the files only (-type f) in /www with at least one number in the filename and execute rm FILE on it.

Out of safety try it first out with echo, such as:

find /www/ -name "*[0-9]*" -type f -exec echo {} \;

2
  • Stars are needed around the [0-9] as in *[0-9]* Apr 1, 2014 at 9:46
  • Absolutely! I'm sorry the stars were there but they weren't escaped with the slash so they weren't displayed correctly :( I'm sorry, now i corrected.
    – fede.evol
    Apr 1, 2014 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.