I want to find all directories with the last subdirectory named doc, for then rename them to Doc. How can be renamed?
I've the first part:
find -type d -name 'doc'
which returns directories paths like:
./foo/bar/doc
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityIn zsh, you can use the zmv
function to mass-rename files:
zmv '(**/)doc' '${1}Doc'
If you have non-directories called doc
, make sure not to match them by adding a glob qualifier:
zmv -Q '(**/)doc(/)' '${1}Doc'
If your intention is to capitalize the directory and you're using bash
4+ this should do:
find . -type d -name doc -print0 \
| while read -rd $'\0' file; do
dname=$(dirname "$file")
fname=$(basename "$file")
mv "$file" "$dname"/"${fname^}"
done
Note the use of \0
to ensure the correct handling of unusual filenames.
As jw013 points out in the comments ${var^}
doesn't work as I expected. I've amended the answer to separate the path into directory and filename and apply the ^
operator only to $fname
.
Btw, thanks rush for adding the missing pipe.
${var^}
expansion does what you want it to. var=./foo/bar/doc; echo ${var^}
gives ./foo/bar/doc
. The ^
expansion only affects the first character.
mkdir -p dir1/doc/dir2
. In this case even though doc directory isn't the last subdirectory (Marc's requirement) it is renamed.
-name doc
, but if only leaf directories should be matched a check that $dir
only contains .
and ..
subdirectories could be done.
rename
works for you by all means use it. The trouble is there is no standardrename(1)
command, so unless you tell everyone your version ofrename
we can't really help you with that approach.