truncate file names (using perl's rename)
Colin Watson
cjwatson at ubuntu.com
Wed May 11 09:15:13 UTC 2005
On Wed, May 11, 2005 at 03:12:03PM +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'
You don't have to use a pure regex with rename; you only have to set $_
somehow. A single regex is often the hard way. For example, to truncate
the base of the filename to 13 characters as above, try:
rename 'my ($base, $ext) = /(.*)\.(.*)/;
$_ = substr($base, 0, 13) . ".$ext";' *.txt
Cheers,
--
Colin Watson [cjwatson at ubuntu.com]
More information about the ubuntu-users
mailing list