How to mass add file extension?
Shell globs should work even with spaces in the names if used properly e.g.
rename -nv -- 's/$/.jpg/' *
or
for file in *; do echo mv -- "$file" "$file.jpg"; done
[NOTE: these are 'no ops' until the n
switch or the echo
are removed - so you can check the correct replacement before committing]
If you do want to automatically distinguish between jpg and avi files that would also be possible using a more complex loop and the file
or mimetype
command
If you have a more complex hierarchy, you'll need a slightly more sophisticated approach. You can either use bash's globstar
option which lets **
match zero or more directories and subdirectories or you can use find
.
-
globstar
shopt -s globstar for f in **/*; do [ -f "$f" ] && mv "$f" "$f".jpg; done
The
[ -f "$f" ] &
ensures that themv
command is only run on files and not directories. -
find
find . -type f -print0 | xargs -0 -I{} mv "{}" "{}".jpg
The
-type f
ensures we only find files and no directories and theprint0
causesfind
to print its output separated with the null character (\0
) instead of newlines. This ensures that we deal correctly with file names containing spaces or newlines or other weirdness.xargs
takes a list of input and runs the command given on it. The-0
flag tells it to expect null-separated input and the-I{}
will make it replace each instance of{}
with each of the input records (files in this case) passed.