Linux: rename file but keep extension?
In Windows/DOS, I can say rename myfile.* yourfile.*
to change the name but keep the extension. How is that accomplished on Linux?
The man page only suggests how to change the extension, but that's the opposite of what I want.
Bonus:
I actually want to put a photo's creation date into its filename, to get something like 20091231 2359 New Year.jpg
. I'm afraid that I need some non-trivial combination of commands to achieve that?
Solution 1:
Here's an answer for the bonus question.
I actually want to put a photo's creation date into its filename, to get something like 20091231 2359 New Year.jpg. I'm afraid that I need some non-trivial combination of commands to achieve that?
Assuming you want to take the photo's creation date from the EXIF data, you'll need a separate tool for that. Luckily it turns out that jhead
offers a trivial way to do exactly what you want, with its -n
option.
$ jhead -h
[...]
-n[format-string]
Rename files according to date. Uses exif date if present, file
date otherwise. If the optional format-string is not supplied,
the format is mmdd-hhmmss. If a format-string is given, it is
is passed to the 'strftime' function for formatting
In addition to strftime format codes:
'%f' as part of the string will include the original file name
[...]
Here's an example:
$ jhead -n%Y-%m-%d-%f New_year.jpg
New_year.jpg --> 2009-12-31-New_year.jpg
Edit: Of course, to do this for a bunch of photos, it'd be something like:
$ for i in *jpg; do jhead -n%Y-%m-%d-%f $i; done
To tweak the date formatting to your liking, take a look at the output of date --help
, for example; it will list the available format codes.
(jhead is widely available for different systems. If you are e.g. on Ubuntu or Debian, simply type sudo apt-get install jhead
to install it.)
Solution 2:
For just the renaming part, the 'rename' program will work. It's the same as the example you saw in the man page, just switched around.
justin@eee:/tmp/q$ touch myfile.{a,b,c,d}
justin@eee:/tmp/q$ ls
myfile.a myfile.b myfile.c myfile.d
justin@eee:/tmp/q$ rename -v s/myfile/yourfile/ myfile.*
myfile.a renamed as yourfile.a
myfile.b renamed as yourfile.b
myfile.c renamed as yourfile.c
myfile.d renamed as yourfile.d
justin@eee:/tmp/q$ ls
yourfile.a yourfile.b yourfile.c yourfile.d
justin@eee:/tmp/q$