DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Logan has posted 12 posts at DZone. View Full User Profile

Rename All *.gif Files In A Directory (prefix)

07.17.2007
| 24235 views |
  • submit to reddit
        // My mind always seems to draw a blank when I want to do this sort of thing, so here's a concise little self-reminder

for file in $(echo *.gif); do mv ${file} prefix.${file}; done
    

Comments

Snippets Manager replied on Fri, 2011/09/09 - 6:10am

what if you want to rename the files in the subdirectory too??? whats the command for that? thanks

Snippets Manager replied on Tue, 2007/08/28 - 7:43pm

I believe there's an even easier way to accomplish this: for i in *.gif; do mv -i "$i" "prefix.$i"; done This should work for every file name (even with spaces). Also, I really suggest using -i there. ;-) Also, the `rename' command might come in handy (not necessarily for adding prefixes, though) when renaming huge amounts of files.