LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 12-13-2004, 10:23 AM   #1
jeffreybluml
Member
 
Registered: Mar 2004
Location: Minnesota
Distribution: Fedora Core 1, Mandrake 10
Posts: 405

Rep: Reputation: 30
script to remove spaces from multiple filenames


Hello again,

Hoping somebody is feeling generous today...

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....

Thanks as always,

Jeff
 
Old 12-13-2004, 11:07 AM   #2
Bruce Hill
HCL Maintainer
 
Registered: Jun 2003
Location: McCalla, AL, USA
Distribution: Arch, Gentoo
Posts: 6,940

Rep: Reputation: 129Reputation: 129
Search LQ. Tinkster has posted a script which does just that...
 
Old 12-13-2004, 12:30 PM   #3
jeffreybluml
Member
 
Registered: Mar 2004
Location: Minnesota
Distribution: Fedora Core 1, Mandrake 10
Posts: 405

Original Poster
Rep: Reputation: 30
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?

Thanks, sorry to be so trivial...
 
Old 12-13-2004, 12:47 PM   #4
Bruce Hill
HCL Maintainer
 
Registered: Jun 2003
Location: McCalla, AL, USA
Distribution: Arch, Gentoo
Posts: 6,940

Rep: Reputation: 129Reputation: 129
I searched "remove spaces" and "Chinaman" and found this script
Quote:
#!/bin/bash
IFS='
'
j=`find $1 -printf "%d\n" | sort -u | tail -n 1`
j=$((j-1))
echo "Max dir depth:" $j
for (( i=0; i<=j ; i++ ))
do
for name in `find -mindepth $i -maxdepth $i -iname "* *" -printf "%p\n"`
do
newname=`echo "$name" | tr " " "_"`
echo "$name" "$newname"
mv "$name" "$newname"
done
done
##########
Hope that's it. I'm in America, and on my main PC in China I have a
file named "good_commands" which I use to copy and paste in such
cases...

This script will replace the space with an _ (underscore). The space is
considered an escape character, IIRC...
 
Old 12-13-2004, 01:07 PM   #5
jeffreybluml
Member
 
Registered: Mar 2004
Location: Minnesota
Distribution: Fedora Core 1, Mandrake 10
Posts: 405

Original Poster
Rep: Reputation: 30
Thank you!!

I'm getting the following after I try to execute it...

##################################

[root@localhost 12-9-04]# ./space_remover

: bad interpreter: No such file of directory

##################################

Have I simply used it wrong?

Please advise, and thank you again...
 
Old 12-13-2004, 01:11 PM   #6
visaris
Member
 
Registered: Dec 2004
Distribution: gentoo
Posts: 190

Rep: Reputation: 30
Replace the

#!/bin/bash

At the top of your file with a shell you have installed. Maybe:

#!/bin/csh

or

#!/bin/tcsh

or maybe

#!/bin/zsh

I just hope that script will work with other shells....
 
Old 12-13-2004, 01:20 PM   #7
jeffreybluml
Member
 
Registered: Mar 2004
Location: Minnesota
Distribution: Fedora Core 1, Mandrake 10
Posts: 405

Original Poster
Rep: Reputation: 30
Still no go. I'm using FC1 and bash, FYI...

Any other ideas?

Sorry for needing my hand held....
 
Old 12-13-2004, 01:21 PM   #8
visaris
Member
 
Registered: Dec 2004
Distribution: gentoo
Posts: 190

Rep: Reputation: 30
Type "which bash" and place that after the #! at the top of the file.

Or just

Code:
your_prompt# echo '#!'`which bash` > new_script && cat old_script >> new_script
and then run newscript (may have to set permissions on it "chmod 755 new_script")

Last edited by visaris; 12-13-2004 at 01:26 PM.
 
Old 12-13-2004, 02:12 PM   #9
jeffreybluml
Member
 
Registered: Mar 2004
Location: Minnesota
Distribution: Fedora Core 1, Mandrake 10
Posts: 405

Original Poster
Rep: Reputation: 30
I really do appreciate the assistance...

Still no go. Bash is where it should be...

which bash

/bin/bash

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...;-)
 
Old 12-13-2004, 02:23 PM   #10
visaris
Member
 
Registered: Dec 2004
Distribution: gentoo
Posts: 190

Rep: Reputation: 30
Hell, try stripping off the #! line and do:

bash script_name

If that doesn't work, I have no idea what is wrong, the script works fine for me.
 
Old 12-13-2004, 02:34 PM   #11
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
i've a similar one i use a lot
Code:
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.
 
Old 12-13-2004, 02:44 PM   #12
jeffreybluml
Member
 
Registered: Mar 2004
Location: Minnesota
Distribution: Fedora Core 1, Mandrake 10
Posts: 405

Original Poster
Rep: Reputation: 30
AAAAHHHHHHHHHHHHHHHHHHHHHH!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Sorry....

Still doesn't work!!

I assume this means there's something wrong (or at least different) with my environment.

What could it be?

Anybody?

acid, I tried yours as well, to no avail. First run I got

'/namer line 13: syntax error near unexpected token `do
'/namer line 13: `do

So I added #!/bin/bash to the beginning, and now I get the same results sa with the first one...

Seriously though, thanks for the attention so far.

Any other ideas...anybody?
 
Old 12-13-2004, 03:25 PM   #13
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
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
 
Old 12-13-2004, 05:59 PM   #14
jeffreybluml
Member
 
Registered: Mar 2004
Location: Minnesota
Distribution: Fedora Core 1, Mandrake 10
Posts: 405

Original Poster
Rep: Reputation: 30
Geez, it still won't work!

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...
 
Old 12-13-2004, 06:12 PM   #15
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

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


Reply



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
Bash script to remove capitalisation and spaces form a filename scuzzman Programming 11 05-18-2008 12:28 PM
script to change multiple filenames in a directory jeffreybluml Linux - Newbie 8 12-06-2006 01:46 AM
need help with script to remove all metachars from filenames BrianK Programming 5 08-20-2005 11:10 PM
Changing multiple filenames with a script brecki Linux - Newbie 8 01-30-2004 03:10 PM
spaces in filenames ebone Linux - General 2 11-12-2001 11:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:08 PM.

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