LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-10-2010, 08:08 PM   #16
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757

Quote:
Originally Posted by Crowey View Post
Is this old? I'm running a 64bit version of RHEL 4 Update 6.
I think '-execdir' was implemented in 4.2.x or so. So your find won't do. If you can not upgrade it, here is a small script that worked with subdirectories:
Code:
#!/bin/bash

count=$(find dir -depth -type d \! -iregex '.*/\..*' -name "*:2F*" | wc -l )
#echo $count

for n in $(seq 1 $count); do
	find /path/to/searchdir -type d \! -iregex '.*/\..*' -name "*:2F*" -exec rename 's/:2F/-/g' '{}' \; -quit
done
Notice that this one does not have the -depth option and the -quit comes after -exec.

[EDIT]
There might be a smarter way to implement the loop condition, e.g. a while that breaks when 'find' does not find any folders to change. However, this was not my focus here.

Last edited by crts; 05-10-2010 at 08:10 PM.
 
Old 05-10-2010, 08:22 PM   #17
Crowey
Member
 
Registered: Jun 2006
Location: Perth, WA
Distribution: RHEL
Posts: 37

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by crts View Post
I think '-execdir' was implemented in 4.2.x or so. So your find won't do. If you can not upgrade it, here is a small script that worked with subdirectories:
Code:
#!/bin/bash

count=$(find dir -depth -type d \! -iregex '.*/\..*' -name "*:2F*" | wc -l )
#echo $count

for n in $(seq 1 $count); do
	find /path/to/searchdir -type d \! -iregex '.*/\..*' -name "*:2F*" -exec rename 's/:2F/-/g' '{}' \; -quit
done
Notice that this one does not have the -depth option and the -quit comes after -exec.

[EDIT]
There might be a smarter way to implement the loop condition, e.g. a while that breaks when 'find' does not find any folders to change. However, this was not my focus here.
I'm beginning to feel rather bad about all this ...

If I run as you posted (but I assume that the dir for the count variable, was meant to be the search path ...

Code:
#!/bin/bash

count=$(find /MFData/Restore/IKEATEST01/ -depth -type d \! -iregex '.*/\..*' -name "*:2F*" | wc -l )

for n in $(seq 1 $count); do
    find /MFData/Restore/IKEATEST01/ -type d \! -iregex '.*/\..*' -name "*:2F*" -exec rename 's/:2F/-/g' '{}' \; -quit
done
I would get this error message:

Quote:
find: invalid predicate `-quit'
find: invalid predicate `-quit'
find: invalid predicate `-quit'
find: invalid predicate `-quit'
find: invalid predicate `-quit'
And if I took -quit out of the script, it would run, but it hadn't renamed the directories.
 
Old 05-10-2010, 08:42 PM   #18
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by Crowey View Post
I'm beginning to feel rather bad about all this ...

If I run as you posted (but I assume that the dir for the count variable, was meant to be the search path ...
Yes, sorry for the typo.

Quote:
And if I took -quit out of the script, it would run, but it hadn't renamed the directories.
Apparently your find does not support this option, too. Well, I have one more - nasty and ugly - solution which worked.
Code:
#!/bin/bash

count=$(find /MFData/Restore/IKEATEST01/ -type d \! -iregex '.*/\..*' -name "*:2F*" | wc -l )
#echo $count

for n in $(seq 1 $count); do
	find /MFData/Restore/IKEATEST01/ -type d \! -iregex '.*/\..*' -name "*:2F*" -exec rename 's/:2F/-/g' '{}' \; 
done
It will spit out a lot of 'No such file or directory' errors but it will hopefully do the job anyway - i.e. if your 'find' supports at least the '-exec' option. Otherwise we might have to make the solution even 'uglier'.

[EDIT]
Oops, I just saw that you already tried it without '-quit'. Thinking about it...

Last edited by crts; 05-10-2010 at 08:45 PM.
 
Old 05-10-2010, 08:57 PM   #19
Crowey
Member
 
Registered: Jun 2006
Location: Perth, WA
Distribution: RHEL
Posts: 37

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by crts View Post
Yes, sorry for the typo.


Apparently your find does not support this option, too. Well, I have one more - nasty and ugly - solution which worked.
Code:
#!/bin/bash

count=$(find /MFData/Restore/IKEATEST01/ -type d \! -iregex '.*/\..*' -name "*:2F*" | wc -l )
#echo $count

for n in $(seq 1 $count); do
	find /MFData/Restore/IKEATEST01/ -type d \! -iregex '.*/\..*' -name "*:2F*" -exec rename 's/:2F/-/g' '{}' \; 
done
It will spit out a lot of 'No such file or directory' errors but it will hopefully do the job anyway - i.e. if your 'find' supports at least the '-exec' option. Otherwise we might have to make the solution even 'uglier'.

[EDIT]
Oops, I just saw that you already tried it without '-quit'. Thinking about it...
Mate, I'm unbelievably grateful for your help here ... but sadly still not working.

Just to make sure I didn't do something silly, here's my full script:

Code:
#!/bin/bash

count=$(find /MFData/Restore/IKEATEST01/ -depth -type d \! -iregex '.*/\..*' -name "*:2F*" | wc -l )
#echo $count

for n in $(seq 1 $count); do
	find /MFData/Restore/IKEATEST01/ -type d \! -iregex '.*/\..*' -name "*:2F*" -exec rename 's/:2F/-/g' '{}' \; 
done
And FYI, just in case useful, version of rename running is ...

Quote:
rename from util-linux-2.12a
Oh, there were no error messages this time, just ran (very) quickly, but no change to directory names.
 
Old 05-10-2010, 10:00 PM   #20
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Ok, here is one that needs two scripts
First script:
Code:
#!/bin/bash
# script.sh
count=$(find /MFData/Restore/IKEATEST01/ -depth -type d \! -iregex '.*/\..*' -name "*:2F*" | wc -l )
#echo $count

for n in $(seq 1 $count); do
	find /MFData/Restore/IKEATEST01/ -type d \! -iregex '.*/\..*' -name "*:2F*" -exec ./sscript.sh '{}' \; 
done
Second script (called sscript.sh):
Code:
#!/bin/bash

newname=$(echo "$1" | sed -e 's/:2F/-/g')
mv "$1" "$newname"
However, I am beginning to suspect that the ':2F' might not be really there. It could be just the representation of an unprintable character. As you said, this are mac names that probably contain '/' in their names. To verify this please do
Code:
mkdir -p /path/to/dummydir/dir:2Fone/sub:2Fdir/sub:2Fdir2
Now run the suggested solution for /path/to/dummydir and let us know if it works. You can skip the solutions that need -exec and -quit.

[EDIT]
I just found out that my distro has a perl implementation of 'rename' which might work differently then your 'rename'.

Last edited by crts; 05-10-2010 at 10:02 PM.
 
Old 05-10-2010, 10:05 PM   #21
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Is there a reason why we can't just use the simple version we presented earlier?
Code:
while read dir
do
    mv "$dir" "${dir%:2F*}"
done< <(find /Data/ -type d -name '*:2F*')
I created the structure you presented and this worked for me. I did also alter the find as wasn't sure the necessity of the regex portion??
 
1 members found this post helpful.
Old 05-10-2010, 10:10 PM   #22
Crowey
Member
 
Registered: Jun 2006
Location: Perth, WA
Distribution: RHEL
Posts: 37

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by crts View Post
Ok, here is one that needs two scripts
First script:
Code:
#!/bin/bash
# script.sh
count=$(find /MFData/Restore/IKEATEST01/ -depth -type d \! -iregex '.*/\..*' -name "*:2F*" | wc -l )
#echo $count

for n in $(seq 1 $count); do
	find /MFData/Restore/IKEATEST01/ -type d \! -iregex '.*/\..*' -name "*:2F*" -exec ./sscript.sh '{}' \; 
done
Second script (called sscript.sh):
Code:
#!/bin/bash

newname=$(echo "$1" | sed -e 's/:2F/-/g')
mv "$1" "$newname"
However, I am beginning to suspect that the ':2F' might not be really there. It could be just the representation of an unprintable character. As you said, this are mac names that probably contain '/' in their names. To verify this please do
Code:
mkdir -p /path/to/dummydir/dir:2Fone/sub:2Fdir/sub:2Fdir2
Now run the suggested solution for /path/to/dummydir and let us know if it works. You can skip the solutions that need -exec and -quit.

[EDIT]
I just found out that my distro has a perl implementation of 'rename' which might work differently then your 'rename'.
Mate, your blood's worth bottling! Success at long last.

I did get following output when script(/s) ran:

Quote:
find: /MFData/Restore/IKEATEST01/IKEAP0642 Perth_2/WIP/offlines 5:2F12:2F07: No such file or directory
find: /MFData/Restore/IKEATEST01/IKEAP0730 Xmas_thought_of_you/WIP/Edits/Offlines_1:2F11:2F07: No such file or directory
find: /MFData/Restore/IKEATEST01/IKEAP0730 Xmas_thought_of_you/WIP/Edits/Offlines_30:2F10:2F07: No such file or directory
find: /MFData/Restore/IKEATEST01/IKEASA0028 Light:2FBalls:2FKey Launch: No such file or directory
find: /MFData/Restore/IKEATEST01/IKEASA0141 Key:2FBalls end frame change: No such file or directory
But, curiously, the directories were renamed.

I'll try your mkdir request shortly (I did want to let you know of your success first though), but out of interest I started cp of one directory to see how it would resolve the directory name, and it did so thus ...

Quote:
IKEASA0028\ Light\:2FBalls\:2FKey\ Launch/
I'm not exactly sure, but it is escaping the : is it not (ie., that the : actually exists)???
 
Old 05-10-2010, 10:26 PM   #23
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
colon is not considered a standard character as it has other implications to commands when not escaped
 
Old 05-10-2010, 10:33 PM   #24
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by grail View Post
Is there a reason why we can't just use the simple version we presented earlier?
Code:
while read dir
do
    mv "$dir" "${dir%:2F*}"
done< <(find /Data/ -type d -name '*:2F*')
I created the structure you presented and this worked for me. I did also alter the find as wasn't sure the necessity of the regex portion??
The OP said that none of the solutions have worked so far. Also, when I tried this there were problems if more than one subdirectory had the pattern in its name. The parent directory would be renamed and then the subdirectory could no longer be found.
 
Old 05-10-2010, 11:42 PM   #25
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Fair point Sorry I missed that
 
Old 05-10-2010, 11:55 PM   #26
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Ok ... just thought of something (be gentle if I have missed something - again), but how about:

Code:
while read dir
do
    mv "$dir" "${dir%:2F*}"
done< <(find /Data/ -type d -name '*:2F*' | sort -r)
 
Old 05-11-2010, 12:03 AM   #27
Crowey
Member
 
Registered: Jun 2006
Location: Perth, WA
Distribution: RHEL
Posts: 37

Original Poster
Rep: Reputation: 16
Smile

Quote:
Originally Posted by grail View Post
Ok ... just thought of something (be gentle if I have missed something - again), but how about:

Code:
while read dir
do
    mv "$dir" "${dir%:2F*}"
done< <(find /Data/ -type d -name '*:2F*' | sort -r)
Mate, firstly I really am very grateful for everybody's efforts, especially you & crts. Secondly, I ran your latest version, like so:

Code:
#!/bin/bash

while read dir
do
    mv "$dir" "${dir%:2F*}"
done< <(find /MFData/Restore/IKEATEST02/ -type d -name '*:2F*' | sort -r)
However, it seemed to just delete the an instance of :2F and everything after it. Vis, it went from this:

Quote:
drwxrwxrwx 5 1328 EVERYONE 8.0K Apr 9 2009 IKEAP0642 Perth_2
drwxrwxrwx 6 1328 EVERYONE 8.0K Apr 9 2009 IKEAP0730 Xmas_thought_of_you
drwxrwxrwx 6 1328 EVERYONE 8.0K Apr 9 2009 IKEASA0028 Light:2FBalls:2FKey Launch
drwxrwxrwx 3 1328 EVERYONE 8.0K Apr 9 2009 IKEASA0141 Key:2FBalls end frame change
to this:

Quote:
drwxrwxrwx 5 1328 EVERYONE 8.0K Apr 9 2009 IKEAP0642 Perth_2
drwxrwxrwx 6 1328 EVERYONE 8.0K Apr 9 2009 IKEAP0730 Xmas_thought_of_you
drwxrwxrwx 6 1328 EVERYONE 8.0K Apr 9 2009 IKEASA0028 Light:2FBalls
drwxrwxrwx 3 1328 EVERYONE 8.0K Apr 9 2009 IKEASA0141 Key
Fascinating stuff!
 
Old 05-11-2010, 12:36 AM   #28
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Sorry, but this thread looks too much like a soap opera. I.e. replacing three characters and renaming a directory is trivial in any programming language.
 
0 members found this post helpful.
Old 05-11-2010, 12:48 AM   #29
Crowey
Member
 
Registered: Jun 2006
Location: Perth, WA
Distribution: RHEL
Posts: 37

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by Sergei Steshenko View Post
Sorry, but this thread looks too much like a soap opera. I.e. replacing three characters and renaming a directory is trivial in any programming language.
No offense mate, but its not trivial to me, or to the organisation I work for.

I stated clearly at start that I'm a gumby at this - I'm learning bash as quickly, and as in-depth, as I can.

Some folks are helping, and I'm VERY grateful.

But if you don't want to help ...
 
Old 05-11-2010, 12:48 AM   #30
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by Sergei Steshenko View Post
Sorry, but this thread looks too much like a soap opera. I.e. replacing three characters and renaming a directory is trivial in any programming language.
So you're a soap opera fan, otherwise you wouldn't have read it to the end.
 
  


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
Renaming directories with Regex? Possible? smaddox Linux - General 9 11-09-2009 06:34 PM
renaming directories from upper case to lower case, help!! linux_teller Linux - Newbie 3 03-07-2008 05:15 AM
Bash - Mass file renaming problem smudge|lala Linux - Software 2 02-14-2007 06:02 PM
dumb newbie question about renaming directories Lleb_KCir Linux - General 2 10-26-2004 08:37 PM
Mandrake File Renaming Problem omega147 Mandriva 15 10-21-2003 12:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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