How to shorten file name recursively using grep and rename
Hi, I have spent hours trying to solve what I thought was a simple problem. I have a large number of file names that are too long in a number of folders.
I am trying to shorten the length of each file name using: Code:
find . -name '*' -print0 | xargs -0 rename -n 's/^(.*\/?).(.{25})(\..*)$/$1$2$3/' * NB: The path and file name may have spaces in them. Also, I am using the "rename -n" flag to ensure no changes made while testing. And I'm on ubuntu. Please help save my sanity. |
I can't find any reference to a Linux "rename" command with that syntax---can you provide a link?
Assuming that the syntax is legal, it looks like the SED "s" command. But--using SED rules--I see a bunch of problems. Finally, I wonder about using find when you are accepting any and all results. |
I did find the "s" syntax in the PERL rename command......
|
Here's the regex for what you want:
Code:
's/(.*\/)(.{25}).*([.].*$)/$1$2$3/' |
Well....
My system has a "rename" command, but it does not seem to recognize the "s" syntax (and it's not in the man page) Google finds a paucity of info on the version used above Are you saying that the above version of rename is in fact a PERL script? |
You're correct!. It's a PERL script. Here's what I dug up on Debian Squeeze:
Code:
$ file /usr/bin/rename Code:
$ cat /usr/bin/prename Code:
$ man rename |
Thanks towheedm, the regex worked a treat. I didn't realise I was using a PERL script, I came across it in a post somewhere. Once again, thanks for the help.
|
Same problem
Guys I've same problem but I don't know how to use the above script :(
I've many files with long file name so rsync and many applications don't work fine with these long names. May I get so help, |
Did you try the find command in post #1 using the regex from post #4?
Code:
find . -name '*' -print0 | xargs -0 rename -n 's/(.*\/)(.{25}).*([.].*$)/$1$2$3/' * |
Quote:
Code:
./.local/share/Trash/files/cache/images/248741.banner.jpg renamed as ./.local/share/Trash/files/cache/images/248741.banne.jpg UPDATED: Another wrong rename Code:
./.kodi/userdata/Thumbnails/d/d524604e.jpg renamed as ./.kodi/userdata/Thumbnails/d/d52.jpg |
Try changing the second capture group from (.{25}) to (.{0,25})
|
Quote:
Code:
./.kodi/userdata/Thumbnails/8/8afbda1a.jpg renamed as ./.kodi/userdata/Thumbnails/8/8afbda1a |
Dunno.
Works fine here with sed: Code:
$ echo "./.kodi/userdata/Thumbnails/8/8afbda1a.jpg" | sed -r 's/(.*\/+)(.{0,25}).*([.].*$)/\1\2\3/' Code:
#!/usr/bin/perl |
All times are GMT -5. The time now is 10:55 AM. |