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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
12-15-2004, 11:15 AM
|
#1
|
|
Member
Registered: Jun 2003
Location: Rotherham, South Yorkshire, England
Distribution: Debian
Posts: 117
Rep:
|
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?
|
|
|
|
12-15-2004, 11:42 AM
|
#2
|
|
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:
|
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).
|
|
|
|
12-15-2004, 08:55 PM
|
#3
|
|
Member
Registered: Aug 2004
Distribution: debian, SuSE
Posts: 365
Rep:
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:10 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|