My photo importer moved files from my camera and renamed them, but I forgot to specify that it should keep the file extension. So I have several folders with hundreds of JPG & AVI files that are all missing their proper extension.
There aren't that many video files so I can manually rename them either before of after a general mass-rename to JPG.
To make things more complicated, the filenames contain spaces, so I can't just mv "*" "*.jpg"
because, well, that doesn't work (but that's what I'd do, coming from DOS/Windows).
I know Linux is full of powerful commands. How can I mass-rename files to add a .jpg extension, when those files contain spaces?
Please don't say "don't use spaces" because that's really not what I'm asking about. Thanks.
Shell globs should work even with spaces in the names if used properly e.g.
or
[NOTE: these are 'no ops' until the
n
switch or theecho
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
ormimetype
commandIf 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 usefind
.globstar
The
[ -f "$f" ] &
ensures that themv
command is only run on files and not directories.find
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.Please refer to
man rename
before you try thisThis should work like a charm.
In a terminal:
Please change "jpg" to whatever you need.
rename
can use perl expresions.. your best friendAt first line you entered your files directory
The
s
the before expresion indicates a substitutionThen will search for any file with an alphanumeric and underscore name , but without extension
And replace add to it . a dot jpg
Finally apply to any file in first line folder
I would advise you use a program called
pyrenamer
.Open it, and in the Renamed file name pattern, enter
{1}.jpg
.I encounter this situation quite often administering systems, especially when dealing with filenames created by windows applications. To add
.jpg
extensions on filenames without any extension, consider this command list.find's
-print0
generates NULL characters for separators, xargs's-0
is for accepting those NULL separators. This will also do this for any subdirectories, so be sure to replace that './example' path.rename
is a great command, but it's distro specific in both implementation and specification. I generally use things likerename
on the command line, but I always write in GNU utils standards for my scripts.Use this command: