Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

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 have lots of files that need an extension appended to them but they are in a directory with lots of subdirectories. Currently the files have no extension at all. How would I rename just the files but not rename any of the directories or subdirectories?

share|improve this question
Thanks for the quick response! – Kevin Jun 20 '12 at 17:05
up vote 3 down vote accepted

Iterate all and test whether it's a regular file or not.

for file in ./*; do
    [[ -f $file ]] && mv "$file" "$file.ext"

In case you need to avoid adding extension on files that already have it:

for file in ./*; do
    if [[ -f $file && $file != *.ext ]]; then
        mv "$file" "$file.ext"
share|improve this answer
For some reason this script ignores file names that include spaces in them, is there anyway to include them as well? – Kevin Jun 20 '12 at 19:00
@Kevin, it should handle them just fine. Did you modify anything? If you remove any of the "-quotes, it will fail on filenames containing whitespace or other syntactical characters. – geirha Jun 21 '12 at 0:02
@geirha shouldn't -f $file have quotes as well ? – Serg Jun 1 '15 at 10:31
@Serg With [ ... ] instead, then quotes would be necessary, but [[ ... ]] is a shell keyword and is parsed differently, so quotes there are optional. See BashFAQ 31 for more on the differences. – geirha Jun 1 '15 at 12:33

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.