LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-06-2007, 07:15 PM   #1
resolute155
LQ Newbie
 
Registered: Dec 2006
Posts: 9

Rep: Reputation: 0
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
 
Old 12-06-2007, 07:28 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,355

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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)

Last edited by chrism01; 12-07-2007 at 12:12 AM.
 
Old 12-06-2007, 08:03 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
for file in *.IMG
do   
   newfile=${file/ /}     
   mv "$file" $new_file
done
 
Old 12-06-2007, 08:05 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by chrism01 View Post
Code:
....
    $new_file=`echo $file |sed -e 's/ //'` 
...
too much Perl ?

Last edited by ghostdog74; 12-06-2007 at 08:06 PM.
 
Old 12-06-2007, 08:29 PM   #5
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 195

Rep: Reputation: 40
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert static library (Filename.a) to dynamic shared object (filename.so) afx2029 Linux - Software 4 08-17-2007 06:07 AM
shell script:filename containing space ! Dr_Death_UAE Programming 3 12-16-2006 06:46 PM
for script: listing files which have space in filename hamish Linux - General 5 10-22-2005 09:25 AM
change uploaded files from filename.avi to filename.avi.html like www.rapidshare.de latheesan Linux - Newbie 3 06-16-2005 04:33 AM
Changing to a directory with a space in the filename? guitargeek Linux - General 5 02-13-2003 05:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration