LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 09-10-2009, 09:38 AM   #1
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Rep: Reputation: 49
Clueless on how to remove spaces from all .jpg's beneath a folder


OK, I have a folder, that has multiple subfolders (which have subfolders of their own) containing a variety of different files. What I need to do is remove all the spaces from the filenames of .jpg files only in all those folders.

Please tell me there is a way to do this with some fancy command way above my skillset.
 
Old 09-10-2009, 09:52 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by rjo98 View Post
OK, I have a folder, that has multiple subfolders (which have subfolders of their own) containing a variety of different files. What I need to do is remove all the spaces from the filenames of .jpg files only in all those folders.

Please tell me there is a way to do this with some fancy command way above my skillset.
There is a way to do this with some fancy command!

Create a myscript.sh
Code:
#!/bin/bash
for file in "$@"
do
    newfilename="${file// /}"
    if [[ "$newfilename" != "$file" ]]; then
       echo mv "$file" "$newfilename"
    fi
done
Make it executable with chmod +x myscript.sh

Now run
Code:
find <my folder> -iname '*.jpg' -exec <full path to myscript.sh> {} +
When you are happy that all the commands echoed are what you want to execute then remove "echo" from myscript.sh and run the find command again.

Last edited by catkin; 09-10-2009 at 09:53 AM. Reason: Typo
 
Old 09-10-2009, 09:52 AM   #3
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Remove or substitute by _ or something else?

You could use find.

Code:
find . -type f -name '* *'.[jJ][pP][gG] | while read file; do mv "$file" "${file// /}"; done
# or
find . -type f -name '* *'.[jJ][pP][gG] | while read file; do mv "$file" "${file// /_}"; done

Last edited by i92guboj; 09-11-2009 at 06:00 AM. Reason: fixed typo
 
Old 09-10-2009, 10:03 AM   #4
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
thanks guys. i92, that looks similar to something i've seen someone else do before, so i might go with that. I'd just like to remove the spaces, don't need to substitute an underscore. so do i just use the top line of your last post? can you explain what the part after "do" does, i'm confused.
 
Old 09-10-2009, 10:03 AM   #5
malekmustaq
Senior Member
 
Registered: Dec 2008
Location: root
Distribution: Slackware & BSD
Posts: 1,669

Rep: Reputation: 498Reputation: 498Reputation: 498Reputation: 498Reputation: 498
rj098,

if you want to do it in auto fashion you will need a script and run it.
certainly this requires workable knowledge of bash scripting.
you can get similar script here click this. Or click this it will also give you another idea.

there is a good scripting guide click here.

hope it helps.

good luck.
 
Old 09-10-2009, 10:11 AM   #6
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by rjo98 View Post
thanks guys. i92, that looks similar to something i've seen someone else do before, so i might go with that. I'd just like to remove the spaces, don't need to substitute an underscore. so do i just use the top line of your last post?
Yes. But use this one instead:

Code:
find . -type f -name '* *'.[jJ][pP][gG] | while read file; do echo mv "$file" "${file// /}"; done
That will list the list of commands that will be run. If you like the results, then remove "echo" from the command and run it again to do the real work.

Quote:
can you explain what the part after "do" does, i'm confused.
find will generate a list of files that contains at least a space on their name, and whose extension is jpg, JPG, Jpg or some other combo with that letters. "while" will consume it, one item at a time, assigning a new file name on each loop to $file. On each loop, the command "mv" is run, and it moves the file pointed by $file to it's new name.

The part that is less clear to you is probably this:

Code:
"${file// /}"
This is a feature of the bash shell (meaning that other shells don't have it). Bash has some basic string processing capabilities. In this case, we use this to substitute all occurrences of " " (blank space) by "" (nothing). The syntax is this:

Code:
${variable//substring_to_substitute/new_substring}
That results in a string that's very similar to the original file name, but doesn't contain any space, since all the blanks have been substituted by "". There's a similar form which will only substitute the first instance of the substring:

Code:
${variable/substring_to_substitute/new_substring}
You might want to check this article (and the responses which expand it):

http://www.linuxjournal.com/article/8919

There is much more about this in the bash manual page, look for the section called "EXPANSION".

Last edited by i92guboj; 09-10-2009 at 10:25 AM. Reason: silly typo missing bracket
 
Old 09-10-2009, 10:12 AM   #7
vonbiber
Member
 
Registered: Apr 2009
Distribution: slackware 14.1 64-bit, slackware 14.2 64-bit, SystemRescueCD
Posts: 533

Rep: Reputation: 129Reputation: 129
Quote:
Originally Posted by rjo98 View Post
OK, I have a folder, that has multiple subfolders (which have subfolders of their own) containing a variety of different files. What I need to do is remove all the spaces from the filenames of .jpg files only in all those folders.
1. copy and paste the shell script below
2. replace 'absolute_path_to_the_top_directory' by the
absolute path to your top folder
3. save and make the shell script executable,
then run the script
it will just echo what it's doing. If it looks ok you can
run it for real by uncommenting the line
# mv "$f" "$nf"
and removing this line
echo "mv \"$f\" \"$nf\""

I assume you have write permission in those folders

Code:
##### copy and paste below this line #####
#!/bin/sh

TOP=absolute_path_to_the_top_directory

if [ ! -d $TOP ]; then
        echo "sorry $TOP doesn't exist or is not a directory"
        exit 1
fi

find $TOP/* | while read f
do
        if [ -d $f ]; then
                continue
        fi
        g="${f##*/}"
        ext="${g##*.}"
        if [ $(echo $ext | tr '[:upper:]' '[:lower:]') == jpg ]; then
                ng="$(echo $g | sed 's? \{1,\}??g')"
                nf="${f%/*}/$ng"
                if [ "$nf" != "$f" ]; then
#                       mv "$f" "$nf"
                        echo "mv \"$f\" \"$nf\""
                fi
        fi
done
##### end of copy above this line
 
Old 09-10-2009, 10:18 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by i92guboj View Post
Yes. But use this one instead:

Code:
find . -name '* *'.[jJ[pP][gG] | while read file; do echo mv "$file" "${file// /}"; done
Neat!

A closing bracket missing though
Code:
find . -name '* *'.[jJ][pP][gG] | while read file; do echo mv "$file" "${file// /}"; done
 
Old 09-10-2009, 10:25 AM   #9
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Good catch, catkin

/me goes fixing posts above yet again
 
Old 09-10-2009, 11:09 AM   #10
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
Very cool! i like that it gets all occurances and not just the first occurance of a space. Thanks to everybody, this is going to help me out BIG TIME.
 
Old 09-10-2009, 11:16 AM   #11
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
so where you have the . at the beginning of the line, i should put in my full path to the root folder of where i want the magic to happen, or should i just cd /full/path then run it exactly as you have it written?
 
Old 09-10-2009, 11:21 AM   #12
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Either way will work, anyway, until you remove the "echo" it's safe to run it, and you can see what will it do. You could as well use a relative path (in fact, "." itself is a relative path).

ps. When running interactively I preffer to cd to the dir, because that way the paths are shorter and the output is less confusing, but besides that it's a merely cosmetic thing.

Last edited by i92guboj; 09-10-2009 at 11:23 AM.
 
Old 09-10-2009, 11:23 AM   #13
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
ok, thanks, i'll probably just cd to the folder i want it to start at and run it with the . then

many thanks to everyone!
 
Old 09-10-2009, 11:26 AM   #14
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
CORRECT>find . -name '* *'.[jJ][pP][gG] | while read file; od echo mv "$file" "${file// /}"; done (y|n|e|a)?


it said that when i typed in the command, do i just type y and let it rip?
 
Old 09-10-2009, 11:31 AM   #15
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by rjo98 View Post
CORRECT>find . -name '* *'.[jJ][pP][gG] | while read file; od echo mv "$file" "${file// /}"; done (y|n|e|a)?


it said that when i typed in the command, do i just type y and let it rip?
It shouldn't ask you anything, if should either work or fail. Maybe you are using a different shell. Can you post the output of this command?

Code:
echo $0
By the way, there's a typo in the command above. It's "do" instead of "od".
 
  


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
remove xtra spaces lipun4u Linux - Newbie 2 09-16-2008 01:56 AM
remove all spaces from a line in C xeon123 Programming 12 10-23-2007 01:29 AM
How to remove spaces from a string pawan_songara Programming 14 08-30-2006 09:20 PM
how to find the spaces on the folder??? ashley75 Linux - General 4 04-10-2006 03:56 PM
Is it possible to remove spaces Alex_jacobson Linux - General 4 01-16-2005 10:45 AM

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

All times are GMT -5. The time now is 12:13 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