LinuxQuestions.org
Visit Jeremy's Blog.
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, 01:02 PM   #31
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49

I ran it with the echo and the 2nd half of the mv still has the spaces removed from the folder names so i'm assuming it will fail if i remove the echo.
 
Old 09-10-2009, 01:25 PM   #32
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
I ran it with the echo and the 2nd half of the mv still has the spaces removed from the folder names so i'm assuming it will fail if i remove the echo.
Oops! Mea culpa! Of course it does. Maybe switch to my method of calling a script from find, but using this updated script
Code:
#!/bin/bash
for file in "$@"
do
    dir="${file%/*}/"
    name="${file#$dir}"
    newname="${name// /}"
    if [[ "$newname" != "$name" ]]; then
       echo mv "$file" "${dir}$newname"
    fi
done
 
Old 09-10-2009, 01:27 PM   #33
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
I see, it was a silly error on my side. If you want to preserve the directories but change all the regular files, try this instead.

Code:
find . -name '* *'.[jJ][pP][gG] | while read file; do
  oldfile=$(basename "$file")
  newfile="${oldfile// /}"
  echo mv "$file" "${file%$oldfile}$newfile"
done

Last edited by i92guboj; 09-10-2009 at 01:28 PM. Reason: tidied it a bit for readability
 
Old 09-10-2009, 01:30 PM   #34
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
the echo'd lines look like it'll work! i'm going to run it. could you explain the stuff you added so i can understand it?
 
Old 09-10-2009, 01:32 PM   #35
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
Looks like that worked perfectly!
 
Old 09-10-2009, 01:39 PM   #36
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Yes. It just needed some retouching, so it would strip the spaces from the file name, but not the path.

Code:
oldfile=$(basename "$file")
basename returns the file name only, so "basename /path/to/file.jpg" would return "file.jpg". That base name is stored in $oldfile.

Code:
newfile="${oldfile// /}"
This is the same I did originally, I remove the blanks from $oldfile and store the result into $newfile.

Code:
echo mv "$file" "${file%$oldfile}$newfile"
There you have a new operator, ${file%$oldfile} cuts the first occurrence of the substring $oldfile inside $file starting from the right. So the result would be the path without the old filename. Finally I add $newfile to that path, obtaining the wanted result.
 
Old 09-10-2009, 01:41 PM   #37
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
Thanks again!!
 
Old 09-10-2009, 02:14 PM   #38
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
So could I add that line to a crontab to have it run every so often on that root folder with all the jpgs?
 
Old 09-11-2009, 05:56 AM   #39
doublejoon
Member
 
Registered: Oct 2003
Location: King George, VA
Distribution: RHEL/CentOS/Scientific/Fedora, LinuxMint
Posts: 370

Rep: Reputation: 44
Quote:
Originally Posted by i92guboj View Post
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
The last example you had with the underscore did not work.

This worked for me to replace the spaces with underscores
Code:
find . -type f -name '* *'.[jJ][pP][gG] | while read file; do mv "$file" "${file// /_}"; done
[/QUOTE]
 
Old 09-11-2009, 05:59 AM   #40
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Yes, that was a typo. Sorry.

*goes to fix another post*
 
Old 09-11-2009, 06:05 AM   #41
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
So could I add that line to a crontab to have it run every so often on that root folder with all the jpgs?
Change the directory name "." to the full path of the desired directory and change "find" to /usr/bin/find and it should work ...
 
Old 09-11-2009, 06:19 AM   #42
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
I'd rather convert this into a script and put it under one of the /etc/cron.*/ directories depending on the frequency, and yes, you will need to use fully qualified paths.
 
Old 09-11-2009, 06:46 AM   #43
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
I'd rather convert this into a script ...
What are the advantages of doing so (apart from keeping the crontab to an easily visible width!)?
 
Old 09-11-2009, 06:52 AM   #44
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Well, none, I guess It's just a matter of cleanliness and personal preference.

I guess each person puts the limit according to his/her own personal tastes. I prefer to keep crontab readable, if I need to write something that's more than a simple command, I rather write a script and either add it to the crontab or put it into /etc/crond.*/. But besides that, as long as the syntax allows it, you can use whatever fits you better.
 
  


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 09:23 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