d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Linux Command Help
Add Reply New Topic New Poll
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Aug 17 2014 01:28pm
Im trying to come up with a command that that will remove the substring ".d" from directory names.

I figure i should start with something like:
Code

find ./ -maxdepth 1 -type d -name "*.d" -exec <substring removing command here> \;


but im not sure what to put after -exec to rename the directories

lmk! :)
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Aug 17 2014 02:04pm
Code
find ./ -maxdepth 1 -type d -name "*al" | awk '{print "mv "$0" "gensub(".d", "", $0)" "}'


If the output looks alright then append | sh to the end of the command:

Code
find ./ -maxdepth 1 -type d -name "*al" | awk '{print "mv "$0" "gensub(".d", "", $0)" "}' | sh
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Aug 17 2014 02:06pm
Replace -name "*al" with -name "*.d"

My bad.
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Aug 17 2014 02:53pm
I think i can get that to work so ty. However, from running the first command to check the output, it looks like gensub() is treating ".d" as a regular expression and matching any character in front of 'd'.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Aug 17 2014 03:27pm
Quote (SelfTaught @ Aug 17 2014 04:53pm)
I think i can get that to work so ty. However, from running the first command to check the output, it looks like gensub() is treating ".d" as a regular expression and matching any character in front of 'd'.


Try escaping the period.
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Aug 17 2014 05:34pm
Quote (AbDuCt @ Aug 17 2014 01:27pm)
Try escaping the period.


That worked. I wasn't sure it was going to at first because I was trying to escape with only one backslash and apparently it needs two. Man i suck with regular expressions xD
Member
Posts: 23,911
Joined: Aug 21 2007
Gold: 2,494.65
Trader: Trusted
Aug 20 2014 10:03am
Code

find . -type d -name "\.d" | sed -e "p;s#\.d##" | xargs -n2 mv


doesnt work if the path includes a ".d" though
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll