Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
add comment

1 Answer 1

up vote 2 down vote accepted

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

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

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"
    fi
done
share|improve this answer
1  
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
add comment

Your Answer

 
discard

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.