- How to Get Last Modified Date of File in Linux
- 1. Using stat command
- 2. Using date command
- 3. Using ls -l command
- 4. Using httpie
- Output
- Conclusion
- More Articles You May Like
- 1 thought on “How to Get Last Modified Date of File in Linux”. add one
- Linux Find Files By Date And List Files Modified On a Specific Date
- ls command example to find files by date
- find Command Example
- Linux find file by date using the date command
- Say hello to -newerXY option for find command
- To see all files modified on the 24/Sep/2017 in the current directory:
- How To Get / Print Current Date in Unix / Linux Shell Script
- Print current date and time in Unix shell script
- Print Current Date in Unix
- Getting the current date and time in Linux shell script
- A list of date command format codes
- Sample shell script to display the current date and time
- Conclusion
- Find Files By Access, Modification Date / Time Under Linux or UNIX
- -daystart option
- How To Append Current Date To Filename in Bash Shell
- Append current date to a filename
- date command FORMAT_STRING
- Bash shell append date to filename in Linux or Unix
How to Get Last Modified Date of File in Linux
Sometimes, you may be required to check detailed information about a file (timestamp) such as its last modified date. This can come in handy when you want to check when the file was last edited. Additionally, it ensures that you have the latest version of the file.
In this article, you will learn 4 ways to get the last modified date of file in Linux.
1. Using stat command
The ls -l command is just okay in giving you basic information about a file such as file ownership and permissions, file size, and creation date. The stat command returns detailed information file attributes such as the last time the file was accessed and modified.
The syntax is quite simple. stat is followed by the file name or the full path to the file.
From the above output, we can clearly see when the file was last accessed ( Access date ), Modify date, Change date among other parameters.
If you wish to view the modified date only and leave out all the other information, run the following command:
The -c option is used to return the date in a custom format, while the ‘%y’ flag displays the last modification time. For directories, the syntax remains the same. Simply replace the file name with that of the directory.
2. Using date command
The date command in its basic syntax displays the current date. However, when used with the -r option, you can display the last modification date of a file as shown.
3. Using ls -l command
The ls -l command is usually used for long listing — display additional information about a file such as file ownership and permissions, size and creation date. To list and display the last modified times, use the lt option as shown.
4. Using httpie
Another way you can check the last modified date is by using the httpie HTTP command-line client tool. The tool is usually used for interacting with HTTP servers and APIs and can also check when a file residing on a web server was last modified.
But first, you need to install it using the command:
On Ubuntu / Debian / Mint, run the command:
To check when a file on a web server was last modified, use the syntax:
Output
Conclusion
This wraps up this article. In this guide, we have featured various ways that you can use to list the last modified date of a file on a Linux system, and even a file hosted on a web server using the httpie tool. Hopefully, you won’t have an issue viewing when files were last modified.
More Articles You May Like
1 thought on “How to Get Last Modified Date of File in Linux”. add one
I prefer using ‘ls’ over all the others because ls allows you to control precisely how the date and time are displayed. I believe stat only gives the choice between seconds-since-epoch and human-readable, with no control over the human-readable format.
For ls, the relevant option is ‘—time-style’ and its format specifiers are fairly straightforward, using the same specifiers used by /bin/date. See ‘man date’ for all the available specifiers. My personal favorite is —time-style=»+%Y-%m-%d %H:%M:%S». I use this alias for my day-to-day ls needs.
alias l=»/bin/ls —time-style=\»+%Y-%m-%d %H:%M:%S\» —group-directories-first -lLFAGv»
Источник
Linux Find Files By Date And List Files Modified On a Specific Date
H ow do I find files by date under UNIX and Linux system? How search for files that created on a specific date on Linux or Unix-like system? How to get a list all files that have been modified on a specific date on Linux or Unix?
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | Linux or Unix |
Est. reading time | 4 mintues |
Linux and UNIX like operating systems do not store file creation time. However, you can use file access and modification time and date to find out file by date. For example, one can list all files that have been modified on a specific date.
Let us see how to find file by date on Linux. You need to use the ls command and find command.
ls command example to find files by date
The syntax is as follows:
You need to use the grep command/egrep command to filter out info:
$ ls -lt /etc/ | grep filename
ls -lt /etc/ | grep ‘Jun 20’
A better and recommended solution is the find command:
find . -type f -ls |grep ‘2017’
find . -type f -ls |grep ‘filename’
find /etc/ -type f -ls |grep ’25 Sep’
find Command Example
If you need a specific date range many days ago, than consider using the find command. In this example find files modified between Jan/1/2007 and Jan/1/2008, in /data/images directory:
You can save list to a text file called output.txt as follows:
find /data/images -type f -newer /tmp/start -not -newer /tmp/end > output.txt
Linux find file by date using the date command
Gnu find as various command line option to list files by a modification and access date/time stamp.
Say hello to -newerXY option for find command
The syntax is as follows:
find /dir/ -type f -newerXY ‘yyyy-mm-dd’
find /dir/ -type f -newerXY ‘yyyy-mm-dd’ -ls
The letters X and Y can be any of the following letters:
- a – The access time of the file reference
- B – The birth time of the file reference
- c – The inode status change time of reference
- m – The modification time of the file reference
- t – reference is interpreted directly as a time
To see all files modified on the 24/Sep/2017 in the current directory:
find . -type f -newermt 2017-09-24
## pass the -ls option to list files in ls -l format ##
find . -type f -newermt 2017-09-24 -ls
OR
find . -type f -newermt 2017-09-24 ! -newermt 2017-09-25
find . -type f -newermt 2017-09-24 ! -newermt 2017-09-25 -ls
Sample outputs:
Источник
How To Get / Print Current Date in Unix / Linux Shell Script
H ow do I get the current date in Unix or Linux shell scripting and store it into a shell variable? How do I print the current date using Unix shell script? How can I display the current time in Linux shell script?
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | Linux or Unix |
Est. reading time | 3 minutes |
You need to use the following syntax to print current date and time on screen:
Print current date and time in Unix shell script
To store current date and time to a variable, enter:
now=$(date)
OR
now=`date`
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Print Current Date in Unix
To print this date either use the printf or echo statement:
echo «$now»
echo «Current date: $now»
OR use the printf command:
printf «%s\n» «$now»
OR
printf «Current date and time in Linux %s\n» «$now»
Getting the current date and time in Linux shell script
You can format and display date using the following syntax:
Finding the current date and time in Linux or Unix using the date command
A list of date command format codes
FORMAT code | Description |
---|---|
%% | a literal % |
%a | locale’s abbreviated weekday name (e.g., Sun) |
%A | locale’s full weekday name (e.g., Sunday) |
%b | locale’s abbreviated month name (e.g., Jan) |
%B | locale’s full month name (e.g., January) |
%c | locale’s date and time (e.g., Thu Mar 3 23:05:25 2005) |
%C | century; like %Y, except omit last two digits (e.g., 20) |
%d | day of month (e.g., 01) |
%D | date; same as %m/%d/%y |
%e | day of month, space padded; same as %_d |
%F | full date; same as %Y-%m-%d |
%g | last two digits of year of ISO week number (see %G) |
%G | year of ISO week number (see %V); normally useful only with %V |
%h | same as %b |
%H | hour (00..23) |
%I | hour (01..12) |
%j | day of year (001..366) |
%k | hour, space padded ( 0..23); same as %_H |
%l | hour, space padded ( 1..12); same as %_I |
%m | month (01..12) |
%M | minute (00..59) |
%n | a newline |
%N | nanoseconds (000000000..999999999) |
%p | locale’s equivalent of either AM or PM; blank if not known |
%P | like %p, but lower case |
%q | quarter of year (1..4) |
%r | locale’s 12-hour clock time (e.g., 11:11:04 PM) |
%R | 24-hour hour and minute; same as %H:%M |
%s | seconds since 1970-01-01 00:00:00 UTC |
%S | second (00..60) |
%t | a tab |
%T | time; same as %H:%M:%S |
%u | day of week (1..7); 1 is Monday |
%U | week number of year, with Sunday as first day of week (00..53) |
%V | ISO week number, with Monday as first day of week (01..53) |
%w | day of week (0..6); 0 is Sunday |
%W | week number of year, with Monday as first day of week (00..53) |
%x | locale’s date representation (e.g., 12/31/99) |
%X | locale’s time representation (e.g., 23:13:48) |
%y | last two digits of year (00..99) |
%Y | year |
%z | +hhmm numeric time zone (e.g., -0400) |
%:z | +hh:mm numeric time zone (e.g., -04:00) |
%::z | +hh:mm:ss numeric time zone (e.g., -04:00:00) |
%. z | numeric time zone with : to necessary precision (e.g., -04, +05:30) |
%Z | alphabetic time zone abbreviation (e.g., EDT) |
Sample shell script to display the current date and time
Conclusion
You learned how to display the current date and time on Linux and Unix-like systems. We also explained how to store date or time in a shell variable. For more info see date command man page by typing the following date command or GNU/date help page here:
man date
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
Find Files By Access, Modification Date / Time Under Linux or UNIX
I do not remember where I saved pdf and text files under Linux. I have downloaded files from the Internet a few months ago. How do I find my pdf or text files?
You need to use the find command. Each file has three time stamps, which record the last time that certain operations were performed on the file:
You can search for files whose time stamps are within a certain age range, or compare them to other time stamps.
You can use -mtime option. It returns list of file if the file was last accessed N*24 hours ago. For example to find file in last 2 months (60 days) you need to use -mtime +60 option.
- -mtime +60 means you are looking for a file modified 60 days ago.
- -mtime -60 means less than 60 days.
- -mtime 60 If you skip + or – it means exactly 60 days.
So to find text files that were last modified 60 days ago, use
$ find /home/you -iname «*.txt» -mtime -60 -print
Display content of file on screen that were last modified 60 days ago, use
$ find /home/you -iname «*.txt» -mtime -60 -exec cat <> \;
Count total number of files using wc command
$ find /home/you -iname «*.txt» -mtime -60 | wc -l
You can also use access time to find out pdf files. Following command will print the list of all pdf file that were accessed in last 60 days:
$ find /home/you -iname «*.pdf» -atime -60 -type -f
List all mp3s that were accessed exactly 10 days ago:
$ find /home/you -iname «*.mp3» -atime 10 -type -f
There is also an option called -daystart. It measure times from the beginning of today rather than from 24 hours ago. So, to list the all mp3s in your home directory that were accessed yesterday, type the command
$ find /home/you -iname «*.mp3» -daystart -type f -mtime 1
- -type f – Only search for files and not directories
-daystart option
The -daystart option is used to measure time from the beginning of the current day instead of 24 hours ago. Find out all perl (*.pl) file modified yesterday, enter:
Источник
How To Append Current Date To Filename in Bash Shell
H ow do I append current date (mm_dd_yyyy format) to a filename (e.g., backup_mm_dd_yyyy.sql) under Linux and UNIX like operating systems? How can I append a current date from a variable to a filename under Linux or Unix bash shell? How do I append date to filename?
We use the date command to show or set the system date and time. Further we can show the current date and time in the given FORMAT. This page explains how to append current date to a filename using various command line options.
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | Bash running on Linux, macOS or Unix |
Est. reading time | 4 minutes |
Append current date to a filename
To get the current date in mm_dd_yyyy format use the following date format syntax:
You can store this to a variable name:
Alternat syntax for command substitution is as follows:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Now we can append the current date stored in $now to a filename as follows:
echo «Coping data to /tmp/filename-$
date command FORMAT_STRING
FORMAT_STRING (FORMAT) controls the output. Interpreted sequences are as follows (taken from GNU/date man page)
- %a – locale’s abbreviated weekday name (e.g., Sun)
- %A – locale’s full weekday name (e.g., Sunday)
- %b – locale’s abbreviated month name (e.g., Jan)
- %B – locale’s full month name (e.g., January)
- %C – century; like %Y, except omit last two digits (e.g., 20)
- %d – day of month (e.g., 01)
- %D – date; same as %m/%d/%y
- %F – full date; same as %Y-%m-%d
- %g – last two digits of year of ISO week number (see %G)
- %G – year of ISO week number (see %V); normally useful only with %V
- %H – hour (00..23)
- %I – hour (01..12)
- %j – day of year (001..366)
- %m – month (01..12)
- %M – minute (00..59)
- %n – a newline
- %N – nanoseconds (000000000..999999999)
- %p – locale’s equivalent of either AM or PM; blank if not known
- %P – like %p, but lower case
- %q – quarter of year (1..4)
- %r – locale’s 12-hour clock time (e.g., 11:11:04 PM)
- %R – 24-hour hour and minute; same as %H:%M
- %s – seconds since 1970-01-01 00:00:00 UTC
- %S – second (00..60)
- %T – time; same as %H:%M:%S
- %u – day of week (1..7); 1 is Monday
- %U – week number of year, with Sunday as first day of week (00..53)
- %V – ISO week number, with Monday as first day of week (01..53)
- %w – day of week (0..6); 0 is Sunday
- %W – week number of year, with Monday as first day of week (00..53)
- %x – locale’s date representation (e.g., 12/31/99)
- %X – locale’s time representation (e.g., 23:13:48)
- %y – last two digits of year (00..99)
- %Y – year
- %z – +hhmm numeric time zone (e.g., -0400)
- %:z – +hh:mm numeric time zone (e.g., -04:00)
- %::z – +hh:mm:ss numeric time zone (e.g., -04:00:00)
- %. z – numeric time zone with : to necessary precision (e.g., -04, +05:30)
- %Z – alphabetic time zone abbreviation (e.g., EDT)
Bash shell append date to filename in Linux or Unix
Finally, you can create a filename as follows:
Источник