views:

513

answers:

3

An NTFS directory is open in a bash shell. what command will recursively truncate all filenames in a directory to the 255 character limit required for ext3?

A: 
$ cat truncname 
#!/bin/bash
# requires basename, dirname, and sed
mv $1 `dirname $1`/`basename $1 | sed 's/^\(.\{0,255\}\).*/\1/'`
$ chmod a+x truncname 
$ find . -exec ./truncname {} \;
Dave
I tried running the shell script on a folder and got the following error:prompt:/media/DUALOS$ sh ~/truncname.sh stuff/home/username/truncname.sh: 1: $: not foundmv: cannot move `stuff' to a subdirectory of itself, `./stuff/stuff'/home/username/truncname.sh: 5: $: not found/home/username/truncname.sh: 6: $: not found
doomdayx
A: 

If you have access to a Windows shell, you can use:

@echo off
setlocal EnableDelayedExpansion

REM  loop over all files in the cwd
for /f %%a in ('dir /a-d /b') do (
   REM  store this filename in a variable so we can do substringing
   set ThisFileName=%%a
   REM  now take a substring
   set ThisShortFileName=!ThisFileName:~0,255!
   REM  finally, the rename:
   echo ren %%a !ThisShortFileName!
)


:EOF
endlocal

(Note: I have added an echo before the rename command just so you can visually verify that it works before actually running it. Works on my box.)

I'm sure somebody who's on a *nix box right now could make a similar script for bash, but I'm stuck in Windows world :)

Good luck!

Mike
This worked, but I had to make the file names even shorter because the directories counted toward the 255 characters.
doomdayx
A: 

Assuming that the shell is sitting in the NTFS directory as it's PWD:

for f in *; do mv $f ${f:0:255}; done

Similar to Dave's sed based version, but avoids an exec per file. Will blow up on a really huge dir, because of the max commandline limit, and doesn't do subdirs.

reedstrm