Quantcast
Channel: What does "-ls" do when used with "find"? - Ask Ubuntu
Viewing all articles
Browse latest Browse all 2

Answer by Eliah Kagan for What does "-ls" do when used with "find"?

$
0
0

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).


Viewing all articles
Browse latest Browse all 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>