in linux terminal, how do I show the folder's last modification date, taking its content into consideration?
Something like:
find /path/ -type f -exec stat \{} --printf="%y\n" \; |
sort -n -r |
head -n 1
Explanation:
- the find command will print modification time for every file recursively ignoring directories (according to the comment by IQAndreas you can't rely on the folders timestamps)
- sort -n (numerically) -r (reverse)
- head -n 1: get the first entry
Directory last modified date
The mtime (modification time) on the directory itself changes when a file or a subdirectory is added, removed or renamed.
Modifying the contents of a file within the directory does not change the directory itself, nor does updating the modified times of a file or a subdirectory. Additionally, adding, removing or renaming files/directories in subdirectories does not propagate up to the directory. If you change the permissions on the directory, the ctime changes but the mtime does not.
Get folder (or sub-files/folders) last modification date and time
Solution:
find . -exec stat -f "%m" \{} \; | sort -n -r | head -1
Explanation:
- the
find
command traverses the current directory (.
) and for each file encountered executes (-exec
) the commandstat -f "%m"
.stat -f "%m"
prints the last modification unix timestamp of the file. sort -n -r
sorts the output of thefind
command numerically (-n
) in reverse order (-r
). This will list the latest modification timestamp first.head -1
then extracts the first line of the output fromsort
. This is the latest modification unix timestamp of all the files.
How can I list (ls) the 5 last modified files in a directory?
Try using head or tail. If you want the 5 most-recently modified files:
ls -1t | head -5
The -1 (that's a one) says one file per line and the head says take the first 5 entries.
If you want the last 5 try
ls -1t | tail -5
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
Get most recent file in a directory on Linux
ls -Art | tail -n 1
This will return the latest modified file or directory. Not very elegant, but it works.
Used flags:
-A
list all files except .
and ..
-r
reverse order while sorting
-t
sort by time, newest first
How to recursively find the latest modified file in a directory?
find . -type f -printf '%T@ %p\n' \
| sort -n | tail -1 | cut -f2- -d" "
For a huge tree, it might be hard for sort
to keep everything in memory.
%T@
gives you the modification time like a unix timestamp, sort -n
sorts numerically, tail -1
takes the last line (highest timestamp), cut -f2 -d" "
cuts away the first field (the timestamp) from the output.
Edit: Just as -printf
is probably GNU-only, ajreals usage of stat -c
is too. Although it is possible to do the same on BSD, the options for formatting is different (-f "%m %N"
it would seem)
And I missed the part of plural; if you want more then the latest file, just bump up the tail argument.
Related Topics
How to Bake Credential into Docker Image for Git
What Is the Meaning of Question Marks '' in Linux Kernel Panic Call Traces
How to Find All Files with a Filename That Ends with Tilde
Explanation of Convertor of Cidr to Netmask in Linux Shell Netmask2Cdir and Cdir2Netmask
Ldconfig Error:"Is Not a Symbolic Link" When Using Linux Loader
Matlab Execute Script from Linux Command Line
How to Read Ring Buffer Within Linux Kernel Space
Expand a Possible Relative Path in Bash
Redirecting Output of Bash for Loop
How to Define a Bash Alias as a Sequence of Multiple Commands
Dump Conf from Running Nginx Process
How to Pass a File Argument to My Bash Script Using a Terminal Command in Linux
Add a Newline Only If It Doesn't Exist