1

I have a lot of residue files from a failed rsync-command. They all have a file extension of 5 (random)characters. Is there a command that can find all files with an extension of 4 characters and more recursively?

Example of a rsync residue filename: foo.jpg.hzmkjt7

I've already found out a way to prevent this with rsync, but all I need to do now is clean those leftovers...

fedorqui
  • 275,237
  • 103
  • 548
  • 598
wtrdk
  • 131
  • 2
  • 12

2 Answers2

2

Using bash, one option would be to use globstar like this:

shopt -s globstar # enable the shell option
echo **/*.?????   # to test which files are matched
rm **/*.?????     # if you're happy

The pattern matches any files in the current directory or any subdirectories that end in a . followed by 5 characters.

Rather than matching any character with a ?, you could go more specific by changing the glob to something like **/*.[[:alnum:]][[:alnum:]][[:alnum:]][[:alnum:]][[:alnum:]] to match 5 alphanumeric characters.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • errr note the [bash] tag was added by me. Not sure if it is the case, sorry. Removing it to prevent more confusion. Sorry! – fedorqui Jul 09 '15 at 10:26
  • 1
    @fedorqui probably not a good idea to do that in general! I'm gonna leave my answer there in case it's useful. – Tom Fenech Jul 09 '15 at 10:28
  • 1
    Yes, you are right. Will keep it in mind, of course. +1 for the nice approach! – fedorqui Jul 09 '15 at 10:29
1

You can write a regular expression for this:

find /your/dir -regextype posix-extended -regex '.*\..{4,}$'

That is, look for files that end with . together with 4 or more.

Idea on how to use a regex from How to use regex in file find.

Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • 2
    Nice, but worth noting that `-regex` is a non-standard feature provided by GNU find. – Tom Fenech Jul 09 '15 at 10:17
  • Yes. I don't have any non-GNU `find` here, but I see different solutions in the linked question that may accomplish this when no GNU find is available. – fedorqui Jul 09 '15 at 10:21