truncate file names (using perl's rename)
ZIYAD A. M. AL-BATLY
zamb at saudi.net.sa
Wed May 11 07:32:18 UTC 2005
On Wed, 2005-05-11 at 15:12 +0900, toyfactory wrote:
> I'm sorry if this is not Ubuntu specific but...
>
> I'm trying to figure out an easy way to truncate filenames to a specific
> length while retaining the extension. I've been playing with regular
> expressions in Perl but, although I think I've figured out a way to do
> it, I've not quite got it working properly from the command line. In
> the meantime I discovered there's a rename command included with Perl.
> Can anyone save me an afternoon of unproductive Googling and head
> scratching and just tell me how to do it? For example:
>
> using 'rename <complicated regex here> *.txt'
>
> 'this_filename_is_simply_too_long_for_my_liking.txt'
>
> becomes
>
> 'this_filename.txt'
>
> Cheers,
>
> Nick
>
BACKUP YOUR FILES BEFORE DOING ANYTHING TO THEM!
!!DON'T BE STUPID!!
YOU HAVE BEEN WARNED, USE IT AT YOUR OWN RISK!
-------8<-------
#!/bin/sh
find * -name '*.*' -maxdepth 0 -type f | while read filename
do
mv -b "$filename" "$(echo "$filename" | \
sed -e 's#^\(.\{8\}\).*\?\.\([^\.]*\)$#\1.\2#')"
done
------->8-------
PRECAUTIONS:
* The above script assume that all files have extensions!
* The above script dose *not* deal with filenames that end having
the same name after truncating! Example:
file00000000000000000001.ext and file00000000000000000002.ext,
both will have the same name after truncating their names. In
that case, the second file will ends having the name
'file0000.ext~'.
* Change the '{8}' (which indicates the number of letters the
filenames will have) to your liking!
Ziyad.
More information about the ubuntu-users
mailing list