LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 12-15-2004, 11:15 AM   #1
doctorwebbox
Member
 
Registered: Jun 2003
Location: Rotherham, South Yorkshire, England
Distribution: Debian
Posts: 117

Rep: Reputation: 15
bash and filenames with spaces


I have been trying to learn a little bash programming. I was trying to write a script for work, part of which involved handling all jpg files in a directory. My script falls over when the files it is trying to process have spaces in the filenames. I cannot find a way around but I realise that there must be one. This is what I have tried:

#!/bin/sh
SOURCEDIR='/path/to/sourcedir/'
DESTDIR='/path/to/destdir'
cd $SOURCEDIR
for i in `ls *.jpg`
do
cp $i $DESTDIR
done

This works fine unless any of the filenames have spaces so I tried:

#!/bin/sh
SOURCEDIR='/path/to/sourcedir/'
DESTDIR='/path/to/destdir'
cd $SOURCEDIR
for i in `ls -Q *.jpg`
do
cp $i $DESTDIR
done

This also doesn't work. I have tried putting quotes around various different pars but without success. What am I doing wrong?
 
Old 12-15-2004, 11:42 AM   #2
sigsegv
Senior Member
 
Registered: Nov 2004
Location: Third rock from the Sun
Distribution: NetBSD-2, FreeBSD-5.4, OpenBSD-3.[67], RHEL[34], OSX 10.4.1
Posts: 1,197

Rep: Reputation: 46
for i in *.jpg; do cp "$i" /path/to/destdir; done

That should do it. This is basically what you have, just quoting the $i variable, and suited for the shell instead of a script file (you need to be in sourcedir of course).
 
Old 12-15-2004, 08:55 PM   #3
amfoster
Member
 
Registered: Aug 2004
Distribution: debian, SuSE
Posts: 365

Rep: Reputation: 34
better yet is to convert all your filenames containing spaces to use underscores. You can try this in the directory with the space names:

#!/bin/bash

ls | while read each
do
echo "$each" | grep "\ " >/dev/null 2>&1
if [ $? -eq 0 ]
then
newname=`echo "$each" | tr ' ' '_'`
mv "$each" $newname
echo "Renamed \"$each\" to $newname"
fi

done
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash scripting: filenames with spaces joanq Programming 2 05-24-2005 12:50 PM
Bash double spaces in filenames ilikejam Programming 2 02-01-2005 10:34 AM
Spaces in filenames with BASH edenning Programming 12 01-27-2005 07:10 AM
bash, loops and spaces in filenames shy Programming 5 11-08-2004 07:43 AM
BASH scripting problem, spaces in filenames / using basename textures Programming 24 11-16-2003 01:41 AM


All times are GMT -5. The time now is 09:02 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration