Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I suck at writing scripts. I can admit that. I'd love a nice perl tutorial, if anybody has links to one. In the meantime...
Can somebody post a short script that will take a directory full of .jpg images, all of which have spaces in the filenames, and remove the spaces from the names? I'm trying to streamline the photo album for my daughter's website, but the camera we take the pictures always puts spaces in the filename...
I greatly appreciate whomever is feeling nice enough to help me here....
Ugh. I'm sorry, I usually try to search for this kind of stuff before I ask, and I have, but I cannot find the script from Tinkster to which you refer.
I've used "space" "filename" "script" and combinations of these (all with Tinkster as the username included in the post), and I still can't find what I'm looking for. Unless, of course, the script in the post I'm looking for is not being used simply to remove spaces from the name, but for some other reason and the space removal is just a side-effect or sub-feature of the script in question, in which case I'm not realizing it by reading through them.
Don't suppose you know where this post is...do you?
So now what? Is there something stupid I'm missing?
I have a directory containing 25 .jpg images, all named pic 001.jpg to pic 025.jpg.
There is nothing else in this directory except the script I'm trying to run. I just want the darned space taken out of the name, so that the script I have to mogrify them into smaller images will run.
Any other ideas?
Thanks,
Oh, and I have to do this once a month or more, so I'm not just being lazy or stupid and not realizing I can manually change the name of these...;-)
if [ -n "$1" ]
then
if [ -d "$1" ]
then
cd "$1"
else
echo invalid directory
exit
fi
fi
for i in *
do
OLDNAME="$i"
NEWNAME=`echo "$i" | tr ' ' '_' | tr A-Z a-z | sed s/_-_/-/g`
if [ "$NEWNAME" != "$OLDNAME" ]
then
TMPNAME="$i"_TMP
echo ""
mv -v -- "$OLDNAME" "$TMPNAME"
mv -v -- "$TMPNAME" "$NEWNAME"
fi
if [ -d "$NEWNAME" ]
then
echo Recursing lowercase for directory "$NEWNAME"
$0 "$NEWNAME"
fi
done
primarily this is for converting upper to lower case, but also does other handy formatting like replacing white space with underscores. it's also nicely recursive.
I wonder if the original script didn't copy over correctly. I like to use the code tags to help prevent that sort of problem. Also, you do know the script has to be executable in order to use ./scriptname . Otherwise, you would use sh scriptname.
Anywho, here is another sample which you would make executable and run like this ...
./scriptname /home/myimages
Note: when it looks ok, remove the echo from the move statement....
echo mv "$dir/$i" "$dir/$j"
to look like this....
mv "$dir/$i" "$dir/$j"
Code:
#!/bin/bash
#
usage()
{
echo "Usage: $0 [directory]"
exit 1;
}
test -d "$1" || usage
dir="$1"
ls $dir | grep -e "[:alnum:]" | \
while read i; do
j=`echo -n "$i" | sed -e 's/ /_/'`
echo mv "$dir/$i" "$dir/$j"
done
Thanks for another example. I don't know what's different about my machine that's dissallowing this.
Could I be mising a package of some sort? Besides bash, is there some language I don't have that I need? I thought I had installed just about all of it, I did the "custom" install and checked all the development tools and whatnot.
Well, I'll tinker around for awhile and see if I can get past this stupid "bad interpreter: No such file of directory" crap.
Let me know if any body has any ideas why none of these work...I'll try anything...once...
At the risk of looking for the obvious, do you have the shebang ( #!/bin/bash ) located at the very top left of the script? No lines or spaces before it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.