Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

share|improve this question
What about to use command rename? In flag -exec – Marc Jul 25 '12 at 11:04
If rename works for you by all means use it. The trouble is there is no standard rename(1) command, so unless you tell everyone your version of rename we can't really help you with that approach. – jw013 Jul 25 '12 at 13:27
up vote 5 down vote accepted
find . -depth -type d -name doc -exec sh -c 'mv "${0}" "${0%/doc}/Doc"' {} \;
share|improve this answer
Thank you, this worked perfectly. – AllGamer May 14 at 16:16

In 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'
share|improve this answer

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^}"

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.

share|improve this answer
I don't think ${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. – jw013 Jul 25 '12 at 12:51
@jw013: You're right, I've amended the answer. – Thor Jul 25 '12 at 13:22
. ├── cos │ └── doc ├── doc └── tt └── doc └── kk – mzet Jul 25 '12 at 14:20
(Sorry for the above). Unfortunately your solution doesn't work for following directory structure: mkdir -p dir1/doc/dir2. In this case even though doc directory isn't the last subdirectory (Marc's requirement) it is renamed. – mzet Jul 25 '12 at 14:31
Well the OP didn't specify any other selection criteria besides -name doc, but if only leaf directories should be matched a check that $dir only contains . and .. subdirectories could be done. – Thor Jul 25 '12 at 14:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.