Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

What does "-ls" do when used with "find"?

Writer Matthew Barrera

According to man find:

-ls

True; list current file in ls -dils format on standard output. The block counts are of 1K blocks, unless the environment variable POSIXLY_CORRECT is set, in which case 512-byte blocks are used. See the UNUSUAL FILENAMES section for information about how unusual characters in filenames are handled.

I don't understand the ls -dils part.

man ls has "-d list directories themselves, not their contents".

Consider a folder with three .png files:

$ ls
01-default.png 02-jungle.png 03-snow.png
$ 

I get

$ find . -type f -ls 12983011 28 -rw-rw-r-- 1 dkb dkb 25964 Mar 10 17:28 ./01-default.png 12982994 24 -rw-rw-r-- 1 dkb dkb 21857 Mar 10 17:28 ./03-snow.png 12983031 28 -rw-rw-r-- 1 dkb dkb 25964 Mar 10 17:28 ./02-jungle.png
$

But, if the -ls means the same as ls -dils, I should see something similar with ls -dils but all I get is

$ ls -dils
13631944 4 drwxrwxr-x 2 dkb dkb 4096 Aug 30 21:22 .
$ 

Whereas, ls -ils is more like what I get with find used with -ls:

$ ls -ils
total 80
12983011 28 -rw-rw-r-- 1 dkb dkb 25964 Mar 10 17:28 01-default.png
12983031 28 -rw-rw-r-- 1 dkb dkb 25964 Mar 10 17:28 02-jungle.png
12982994 24 -rw-rw-r-- 1 dkb dkb 21857 Mar 10 17:28 03-snow.png
$ 

So I was wondering if there's a typo in the man find page and whether ls -dils should really be ls ils in the part I quoted at the top of the question.

1

1 Answer

For each file it finds, the find command with the -ls action displays that file in a format similar to what this command would display, when you replace path with that file's path:

ls -dils path

This is to say that find with -ls is similar to running many commands that start with ls -dils, one for each file found. It is not similar to running this actual command with no path argument:

ls -dils

The ls command exhibits special behavior when run with no path arguments: it behaves as if a single path argument . has been passed. You've been comparing the behavior of the find command with -ls to the behavior of the ls command without path arguments, but you should compare its behavior to that of ls with path arguments. For example, try this:

ls -dils ./*

(Because ls sorts its output, as mentioned below, an even better comparison would be to many separate ls -dils ./filename commands, one for each filename.)

With those particular files, the output is the same with and without -d. However, find can find directories. You can see the difference between the ls commands with and without the -d option by creating a directory and trying both ls -dils ./* and ls -dils ./*. For example:

ek@Cord:~/tmp$ touch a b c
ek@Cord:~/tmp$ mkdir d
ek@Cord:~/tmp$ ls -dils ./*
147774362773104141 0 -rw-rw-rw- 1 ek ek 0 Aug 30 12:35 ./a 71213169107810485 0 -rw-rw-rw- 1 ek ek 0 Aug 30 12:35 ./b 23080948090377031 0 -rw-rw-rw- 1 ek ek 0 Aug 30 12:35 ./c 25051272927352243 0 drwxrwxrwx 1 ek ek 512 Aug 30 12:35 ./d
ek@Cord:~/tmp$ ls -ils ./*
147774362773104141 0 -rw-rw-rw- 1 ek ek 0 Aug 30 12:35 ./a 71213169107810485 0 -rw-rw-rw- 1 ek ek 0 Aug 30 12:35 ./b 23080948090377031 0 -rw-rw-rw- 1 ek ek 0 Aug 30 12:35 ./c
./d:
total 0

To reiterate one particular important point: because find with -ls is similar to running many separate ls -dils path commands, it does not behave the same as any particular ls command. (It does behave somewhat similarly to an ls -dils paths..., where paths... is all the paths find found, but it's not all that similar to that either, because the ls command sorts its output, so the results would often appear in a different order.)


Another way to see this is to run a find command whose results include directories with the -ls action, and then run the corresponding find commands that use the -exec action to run actual ls commands with different options. For example:

ek@Cord:~/tmp$ find . -ls
54043195528455285 0 drwxrwxrwx 1 ek ek 512 Aug 30 12:35 .
147774362773104141 0 -rw-rw-rw- 1 ek ek 0 Aug 30 12:35 ./a 71213169107810485 0 -rw-rw-rw- 1 ek ek 0 Aug 30 12:35 ./b 23080948090377031 0 -rw-rw-rw- 1 ek ek 0 Aug 30 12:35 ./c 25051272927352243 0 drwxrwxrwx 1 ek ek 512 Aug 30 12:35 ./d
ek@Cord:~/tmp$ find . -exec ls -dils {} \;
54043195528455285 0 drwxrwxrwx 1 ek ek 512 Aug 30 12:35 .
147774362773104141 0 -rw-rw-rw- 1 ek ek 0 Aug 30 12:35 ./a
71213169107810485 0 -rw-rw-rw- 1 ek ek 0 Aug 30 12:35 ./b
23080948090377031 0 -rw-rw-rw- 1 ek ek 0 Aug 30 12:35 ./c
25051272927352243 0 drwxrwxrwx 1 ek ek 512 Aug 30 12:35 ./d
ek@Cord:~/tmp$ find . -exec ls -ils {} \;
total 0
147774362773104141 0 -rw-rw-rw- 1 ek ek 0 Aug 30 12:35 a 71213169107810485 0 -rw-rw-rw- 1 ek ek 0 Aug 30 12:35 b 23080948090377031 0 -rw-rw-rw- 1 ek ek 0 Aug 30 12:35 c 25051272927352243 0 drwxrwxrwx 1 ek ek 512 Aug 30 12:35 d
147774362773104141 0 -rw-rw-rw- 1 ek ek 0 Aug 30 12:35 ./a
71213169107810485 0 -rw-rw-rw- 1 ek ek 0 Aug 30 12:35 ./b
23080948090377031 0 -rw-rw-rw- 1 ek ek 0 Aug 30 12:35 ./c
total 0

In the version that runs -ls without -d, the first entries displayed for a, b, c, and d are the output of the first ls command run, with the path .. To verify this, try it with -ok in place of -exec (which operates interactively, prompting you before each command, and therefore makes clear which commands produce which outputs).

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy