Print a file's last modified date in Bash
You can use thestat
command
stat -c %y "$entry"
More info
%y time of last modification, human-readable
Append a file's last modification date to its name
find . -type f -exec bash -c 'for arg; do arg=${arg#./} mod=$(stat -c %x "$arg") base=${arg%.*} ext=${arg#$base}; echo mv -i "$arg" "${base}_${mod%% *}$ext"; done' _ {} +
(multiline version for readability):
find . -type f -exec bash -c 'for arg; do
arg=${arg#./} mod=$(stat -c %x "$arg") base=${arg%.*} ext=${arg#$base}
echo mv -i "$arg" "${base}_${mod%% *}$ext"
done' _ {} +
I left the echo
there for you to see what it's going to run before actually running it. Remove it once you're sure you want to move the files.
It adds what you want before the extension but it will completely FAIL if:
- A filename doesn't contain a dot
- That file is in a folder path which does contain a dot
It will also not work properly on files with double extensions, e.g. .tar.gz
Explanation:
I'm passing all files to a bash script with find . -type f -exec bash -c '...' _ {} +
The bash script does the same action for all files: get the modification date
, discover the basename
and the .extension
, then rename the file to basename_date.extension
how to use shell script checking last changed time of a file
You can get the last modification time of a file with stat
, and the current date with date
. You can use format strings for both to get them in "seconds since the epoch":
current=`date +%s`
last_modified=`stat -c "%Y" $file`
Then, it's pretty easy to put that in a condition. For example:
if [ $(($current-$last_modified)) -gt 180 ]; then
echo "old";
else
echo "new";
fi
Displaying Last modification date
It seems you are looking for this:
stat -c %y $filename
Or this:
date -r $filename
Or this (most portable):
fn=$filename perl -e 'print scalar localtime((stat("$ENV{fn}"))[9])'
On the other hand, stat -c %y
was already in your post, and you wrote:
Everything works (I think) except it shows more than just the date. What command would I use to just display the date of last modification?
Which makes me think that perhaps you want only the date part? One (lazy) solution for that is using shell commands like cut
, awk
, sed
to extract just what you need, for example:
$ stat -c %y sample.txt
2013-10-12 09:24:08.096820646 -0700
$ stat -c %y sample.txt | cut -f1 -d' '
2013-10-12
A better solution is to generate dates in the desired format, but that depends on the command you used. For example stat
cannot do this, but date
and perl
can, for example:
date +%Y-%m-%d -r $filename
how to extract date only from the creation/modified date of a file?
You can use the date command to extract the modification time of a file by passing it as reference.
date -r filename
prints the last modified timestamp of the file.
Sat Jul 3 16:03:53 IST 2021
date +%d -r filename
print just the day you asked for, but with a leading zero.
03
date +%-d -r filename
prints the day, without a leading zero.
3
Related Topics
How to Make the Watch Command Interpret Vt100 Sequences
Remove All Text from Last Dot in Bash
Difference Between Linux Kernel and Unix Kernel(Such as Freebsd) from Programmer's Point of View
What's the Purpose of the Ud2 Opcode in the Linux Kernel
How to Create a File with Any Given Size in Linux
How to Install a Node.Js Server at Chat.Mydomain.Com on a Hostgator Vps Hosting
Getting Current Working Directory Within Kernel Code
Convert Multiple Sftp Command Lines to a Single Line Command Line
Sshpass: Command Not Found Error
.Bashrc Syntax Error: Unexpected End of File
Are Pid-Files Still Flawed When Doing It 'Right'
Changing Pecl Installation Directory
Add Suffix to Each Line with Shell Script