• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 381
  • Last Modified:

Rename some of the files in a directory tree v2

Regarding my question:
http://www.experts-exchange.com/OS/Linux/Q_22734079.html

I'm still have problem with space in file name.  the mv command is break :(

Change it to:

find /home/damian/test  -iname \*_old`; do mv "$dir" `echo "$dir" | sed -e 's/_old//'`; done
 or
find /home/damian/test  -iname \*_old`; do mv \"$dir\" `echo \"$dir\" | sed -e 's/_old//'`; done
didn't solve it.
0
addady
Asked:
addady
  • 2
  • 2
1 Solution
 
ravenplCommented:
either set
IFS="
"

note the newline, You are setting IFS to newline character

OR

find /whatever | while read dir; do
 dirn=$( echo "$dir" | sed -e 's/_old//' )
 mv "$dir" "$dirn"
done

OR
rename "_old" "" *_old
0
 
addadyAuthor Commented:
Let me be more specific:

#find /tmp/ -iname \*.genc__12-24__26-07-2007                
/tmp/051008 - Elt/P1010039.JPG.genc__12-24__26-07-2007
/tmp/051008 - Elt/P1010037.JPG.genc__12-24__26-07-2007
/tmp/051008 - Elt/P1010036.JPG.genc__12-24__26-07-2007

The directory name contain space

The command:
#for FILE in "`find /tmp/ -iname \*.genc__12-24__26-07-2007`"; do NEW=`echo $FILE | sed -e 's/.genc__12-24__26-07-2007/.genc/'`;mv "$FILE" "$NEW"; done


fail because of the space in the directory name.

Please provide example.

Thanks
0
 
ravenplCommented:
I already answered You Q.

Example 1
IFS="
"
for FILE in `find /tmp/ -iname \*.genc__12-24__26-07-2007`; do NEW=`echo $FILE | sed -e 's/.genc__12-24__26-07-2007/.genc/'`;mv "$FILE" "$NEW"; done

Example 2
find /tmp/ -iname \*.genc__12-24__26-07-2007 | while read FILE; do
 NEW=$( echo $FILE | sed -e 's/.genc__12-24__26-07-2007/.genc/' )
 mv "$FILE" "$NEW";
done

Example 3
rename .genc__12-24__26-07-2007 "" /tmp/051008\ -\ Elt/*.genc__12-24__26-07-2007
0
 
Duncan RoeSoftware DeveloperCommented:
At work I found a sed command right after the find did it for me:

for FILE in "`find /tmp/ -iname \*.genc__12-24__26-07-2007|sed - e 's/ /\\ /g'`"; do NEW=`echo $FILE | sed -e 's/.genc__12-24__26-07-2007/.genc/'`;mv "$FILE" "$NEW"; done

works for any number of spaces, including consecutive
0
 
addadyAuthor Commented:

Hello ravenpl: and


ravenpl:
Example 1 work (Accept as solution)

Example 2 did some of the job. it wirk well on dir like:
"/tmp/051008 - Eilat"
but fail on dir like:
"/tmp/Tomer - Yom Huledet  5/"
The error:
mv: cannot move `/tmp/Tomer - Yom Huledet  5/Thumbs.db.genc__12-24__26-07-2007' to `/tmp/Tomer - Yom Huledet 5/Thumbs.db.genc': No such file or directory

Example 3
Why the subdir name is part of the command? it suppose to work on all /tmp

duncan_roe:
If fail on:
sed: -e expression #1, char 1: unknown command: `-'
mv: cannot stat `': No such file or directory
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now