LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   space in filename difficulty (https://www.linuxquestions.org/questions/programming-9/space-in-filename-difficulty-605061/)

resolute155 12-06-2007 07:15 PM

space in filename difficulty
 
I have a bunch of files that accidentally got written with a space in them:

301_A_L_mask _face.img
301_A_L_mask _scene.img
301_A_R_mask _face.img
301_A_R_mask _scene.img
302...
303...
etc.


How can I batch re-write these files to erase the space? For example,

301_A_L_mask _face.img -> 301_A_L_mask_face.img


Any help would be greatly appreciated!

Thanks,
David

chrism01 12-06-2007 07:28 PM

Code:

for file in `ls *.img`
do
    $new_file=`echo $file |sed -e 's/ //'`
    cp "$file" $new_file
done

If that works, use 'mv' instead of 'cp'

Edit: as per ghostdog74's comment:
new_file=`echo $file |sed -e 's/ //'`
only use '$' when reading a bash var, not when writing to one (yeah, Perl is different)

ghostdog74 12-06-2007 08:03 PM

Code:

for file in *.IMG
do 
  newfile=${file/ /}   
  mv "$file" $new_file
done


ghostdog74 12-06-2007 08:05 PM

Quote:

Originally Posted by chrism01 (Post 2982690)
Code:

....
    $new_file=`echo $file |sed -e 's/ //'`
...


too much Perl ? :)

PAix 12-06-2007 08:29 PM

I feel very inellegant folks, a bit filename specific perhaps:
Code:

#!/bin/bash

> bashout
for file in $(ls -1 3[0-9][0-9]_A_[LR]_mask\ _*.img | tr -s " " "," )
do
  echo $file |
    awk '
    BEGIN {FS=","}
          { print ("mv " $1 "\\ " $2 " " $1 $2) } ' >> bashout
done

chmod 755 bashout
cat bashout
#./bashout

If the output looks ok then execute the bashscript written ./bashout or just remove the comment in the last line of the script above and run it again.

Whatever you do be safe.


All times are GMT -5. The time now is 06:56 AM.