Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display modified date time with 'find' command?

With a find command, I can display directories names with multiple levels. The following command display all directories under /var path with a depth of 2:

find /var -maxdepth 2 -type d; 

The result shows:

/var /var/log /var/log/sssd /var/log/samba /var/log/audit /var/log/ConsoleKit /var/log/gdm /var/log/sa 

With a stat command, I can find the modified date time:

stat /var/log/samba | grep 'Modify:' 

The result is:

Modify: 2014-01-02 11:21:27.762346214 -0800  

Is there a way to combine the two commands so that directories will be listed with modified date time?

like image 734
Purres Avatar asked Jan 02 '14 22:01

Purres


People also ask

How can I tell what time a file was modified?

Windows file properties You can also see the modified date by viewing the file properties. Right-click the file and select Properties. In the Properties window, the Created date, Modified date, and Accessed date is displayed, similar to the example below.

How does Unix determine file modification time?

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.

What is atime in find command?

Linux Commands - find command - date accessed (-atime) The -atime option can be used to return files and directories that were accessed x or more days ago. For example, to return files and directories that were accessed 14 or more days ago below the tmp directory.

How to get last modified date and time for a file?

For example, to get the last modified time for the file ‘E:commands.docx’ the command would be: To get the modified date and time for all files and sub folders in the current directory the command would be: Using Forfiles command. Using forfiles command we can get modified date and time for all the files in a directory.

How to search for files modified after n days from today?

Please note that /D + (number of days) is practically not useful. This option says to search for the files modified after n days from today’s date. If there are no files meeting the condition, the command prints the following message.

How do I get the date and time of a file?

To get the modified date and time for all files and sub folders in the current directory the command would be: dir /T:W. To get modified date/time only for files in the current directory(i.e exclude directories from files) dir /T:W /A:-D.

How to search for files based on modification time in Linux?

‘find’ is a very powerful Linux command which provides various options for searching files based on different criteria. One of these options allows users to search for files based on the modification/access/creation time of the file.


1 Answers

The accepted answer works but it's slow. There's no need to exec stat for each directory, find provides the modification date and you can just print it out directly. Here's an equivalent command that's considerably faster:

 find /var -maxdepth 2 -type d -printf "%p %TY-%Tm-%Td %TH:%TM:%TS %Tz\n" 
like image 179
kzar Avatar answered Oct 27 '22 04:10

kzar