Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"find -ls" with proper time-style

i use the following command to generate a filelist which i compare sometimes to see if something is changed:

find /directory -xdev -ls

My Problem is that the time in the output is not always in the same format:

Sep 19 08:48 ./pool/f/f/0/ff046cc5b7188073cbd68207c52bddc5
Nov  2 06:24 ./pool/f/f/0/ff0e803c36d89315a6b3663ed1295f71
Jan 18  2012 ./pool/f/f/0/ff07f60465d8deb7a1aa38096d0b798d
Jan 18  2012 ./pool/f/f/0/ff07436f519bddf1d340afde5a240375

For the ls-command there is the option --time-format=long-iso to force the same time-format for all files. Is it possible to combinate this with the find-command?

Thanks

like image 685
Denno Avatar asked Jan 07 '14 15:01

Denno


People also ask

What timestamp does ls show?

The default timestamp is mtime . mtime is the modification time, the last time the file was written to. It's the time that is displayed by ls -l .

Can we use WC with ls?

ls | wc The output from the ls command is piped into the wc command. So it will count the words which are in the output of ls. So you see simply the number of files read by ls. ls > wc This creates a new file in your current working directory with the name wc with the output of your ls command.

How do I sort ls by date?

In order to ls by date or list Unix files in last modifed date order use the -t flag which is for 'time last modified'. or to ls by date in reverse date order use the -t flag as before but this time with the -r flag which is for 'reverse'.


2 Answers

You can use various options to -printf (man find)

find . -printf "%CY-%Cm-%Cd %CH:%CM\n"
like image 148
Ken Avatar answered Oct 17 '22 03:10

Ken


There's nothing in my manpage about changing the format of the -ls parameter. In fact, on my system, the output of -ls is not influenced by various environment variables that affect the output of the ls command itself. I assume that the format of the -ls parameter is internal to find and does not involve the actual ls command. To me, this makes programming sense. Why run an external command? Just simulate the display.

The only way I can think of to get around this is to use -exec or -print0 to pass the results to the actual ls command. A bit of warning: If you pass the name of the directory, ls will print the contents of that directory, so you'll need to pass -d to the ls command or add -type f into your find query. I checked the manpage for find on Linux, and found that it's suppose to be the same output as -dils, so I used that. Since-dis included, I didn't have to addtype -f` to my find query:

This is using the -exec which will send each and every file or directory individually to the ls command. If you have 10,000 files, ls will be called 10,000 times.

$ find /directory -xdev -exec ls -dils --time-style=long-iso {} \;

This maybe more efficient:

$ find /directory -xdev -print0 | xargs -0 ls -dils --time-style=long-iso

This will group as many file names as possible that will fit into the command buffer and pass them at once to the ls command. It will call the ls command as many times as needed to complete all of the files. For example, if you have 10,000 files in your find command, the ls command will be called maybe once or twice instead of 10,000 times.

The problem is that xargs has issues with funny file names, and there are some security issues as pointed out in the manpage:

It is not possible for xargs to be used securely, since there will always be a time gap between the production of the list of input files and their use in the commands that xargs issues. If other users have access to the system, they can manipulate the filesystem during this time window to force the action of the commands xargs runs to apply to files that you didn’t intend. For a more detailed discussion of this and related problems, please refer to the ‘‘Security Considerations’’ chapter in the findutils Texinfo documentation. The -execdir option of find can often be used as a more secure alternative.

The -print0 parameter uses a NUL character to separate out file names instead of a NL and the -0 parameter tells xargs to use the NUL character as a file name separator rather than whitespace (the characters in the $IFS environment variable).

This means using -print0 | xags -0 works almost all of the time, but you may still decide that -exec ls is a better way to go.

like image 5
David W. Avatar answered Oct 17 '22 03:10

David W.