Loading...

"find -ls" with proper time-style

Answer #1 100 %

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.

Answer #2 100 %

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

find . -printf "%CY-%Cm-%Cd %CH:%CM\n"

You’ll also like:


© 2023 CodeForDev.com -