Thursday, June 18, 2015

linux - Recursive rename files and folders


I need to rename every subfolder of a specified path.
For example I have a directory structure like this:


project/
/x
/something
/somethingElse
/x
/x.someext
/notXButTheresXInASubfolder
/something
/x

and I need to change it to:


project/
/y
/something
/somethingElse
/y
/y.someext
/thisContainsXIntheNameButIsNotx
/something
/y

I would do this ideally with a bash script, but I have no idea on how to do it...


Answer



DIRS=$(find /path/to/project -type d -name "x" | sort -r)
while read R; do
test -z "$R" && continue;
B=$(dirname "$R")
mv "$R" "$B/y"
done < <(echo -en "$DIRS")

No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...