Question

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

Was it helpful?

Solution 2

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.

OTHER TIPS

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

find . -printf "%CY-%Cm-%Cd %CH:%CM\n"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top