Linux Rename file with only time/date stamp
mv myfile.txt `date +%Y_%m_%d_%H:%M:%S`.txt
Bash: Rename file name to unix timestamp of modification date
find . -type f -exec \
sh -c '
for i do
d=$(dirname "$i")
[ "$d" = / ] && d=
n=${i##*/}
echo mv "$i" "$d/u-$(stat -c %Y "$i") $n"
done' _ {} +
This operates recursively in the current directory (
.
). It only targets regular files (not directories etc). Modify-type f
and other flags if needed.It just prints the mv commands, so you can review them. Remove the
echo
to run for real.We use
find
to list the target files, and its-exec
flag to pass this list to a shell loop where we can parse and modify the filenames, includingstat
to get the modification time.I don't know your use case, but a better solution may be to just save the output of:
find . -type f -printf '%p u-%T@\n'
in a file, for later reference (this prints the file path and modification time on the same line). Also, maybe a snapshot (if possible).
Renaming files in unix of particular date
You can modify a.sh
like this :
find . -maxdepth 1 -type f -iname "*.temp" -exec bash -c 'mv $0 ${0//.temp/}' {} \;
newermt
is for the date the file is modified last time.
and can execute it as
./a.sh 2017-09-27
To test if this command works on command line, you can try this outside your script:
find . -maxdepth 1 -type f -iname "*.temp" -exec bash -c 'mv $0 ${0//.temp/}' {} \;
linux-shell: renaming files to creation time
Naming based on file system date
In the linux shell:
for f in *.jpg
do
mv -n "$f" "$(date -r "$f" +"%Y%m%d_%H%M%S").jpg"
done
Explanation:
for f in *.jpg
doThis starts the loop over all jpeg files. A feature of this is that it will work with all file names, even ones with spaces, tabs or other difficult characters in the names.
mv -n "$f" "$(date -r "$f" +"%Y%m%d_%H%M%S").jpg"
This renames the file. It uses the
-r
option which tellsdate
to display the date of the file rather than the current date. The specification+"%Y%m%d_%H%M%S"
tellsdate
to format it as you specified.The file name,
$f
, is placed in double quotes where ever it is used. This assures that odd file names will not cause errors.The
-n
option tomv
tells move never to overwrite an existing file.done
This completes the loop.
For interactive use, you may prefer that the command is all on one line. In that case, use:
for f in *.jpg; do mv -n "$f" "$(date -r "$f" +"%Y%m%d_%H%M%S").jpg"; done
Naming based on EXIF Create Date
To name the file based on the EXIF Create Date (instead of the file system date), we need exiftool
or equivalent:
for f in *.jpg
do
mv -n "$f" "$(exiftool -d "%Y%m%d_%H%M%S" -CreateDate "$f" | awk '{print $4".jpg"}')"
done
Explanation:
The above is quite similar to the commands for the file date but with the use of exiftool
and awk
to extract the EXIF image Create Date.
The
exiftool
command provides the date in a format like:$ exiftool -d "%Y%m%d_%H%M%S" -CreateDate sample.jpg
Create Date : 20121027_181338The actual date that we want is the fourth field in the output.
We pass the
exiftool
output toawk
so that it can extract the field that we want:awk '{print $4".jpg"}'
This selects the date field and also adds on the
.jpg
extension.
how to rename an existing file with today's date
This is because a file name cannot contain slashes /
. When you indicate
mv foo.c foo`date +%D`.c
It internally does:
mv foo.c foo01/10/14.c
so it tries to move the file foo.c
into foo01/10
directory.
Instead, you can do:
date "+%F"
Which returns the date in a 2014-01-10
format and will expand into
mv foo.c foo2014-01-10.c
Related Topics
Sort Logs by Date Field in Bash
Increase of Virtual Memory Without Increse of Vmsize
How to Forward Localhost Port on My Container to Localhost on My Host
Docker Alpine Executable Binary Not Found Even If in Path
Apache Server Keeps Crashing, "Caught Sigterm, Shutting Down"
Why Do I Get "Suspended (Tty Output)" in One Terminal But Not in Others
Why Linux Kernel Use Trap Gate to Handle Divide_Error Exception
Difference Between Bash Pid and $$
What Character Sequence Should I Not Allow in a Filename
How to Get the Interface Name/Index Associated with a Tcp Socket
How to Create Opengl Context via Drm (Linux)
Get Link Speed Programmatically
How Is the Address of the Text Section of a Pie Executable Determined in Linux
Possible Values for 'Uname -M'
Linux Keyboard Event Capturing /Dev/Inputx