-3

I can see ls -R lists all files and folders. I can grep the file name from the list.

What are the advantages of "find" ?

Also, any caveats if I use ls -R instead of find?

phuclv
  • 30,396
  • 15
  • 136
  • 260
Bijo
  • 1

2 Answers2

1

ls lists files on or inside the arguments while find finds files, their purpose is completely different and they have nothing to do with each other

  • ls doesn't do any globbing, so if you want to find names matching something in any subdirectories then it doesn't work at all, unless your shell supports globstar. In that case find . -name '*some*thing*.*some*ext' is equivalent to ls **/*some*thing*.*some*ext
  • ls won't list hidden files and folders unless -a is used so ls -R isn't the equivalent to find . at all
  • Some find implementations like GNU or BSD also support matching with regex. You can't use ls to lists files matching a regex
  • Piping ls output to commands like ls -Ra | grep is a bad thing to do because it won't work with files containing newlines in their names. It's worse if ls is aliased to use some arguments that print special characters differently. Many people also use ls output in for loops and that might fail in even more cases. See Why not parse ls (and what to do instead)?, Parsing ls, not recommended, Why you shouldn't parse the output of ls(1). With find it's always safe: find . -name 'pattern' -exec command {} or find . -name 'pattern' -print0 | xargs -0 your command
  • Because ls is just a listing tool, you can't find files matching complex conditions like (*.txt in folderA) or (*.cfg in folderB and grandparent is folderC) or (isExecutable or hasMode(0624) or (file.ext and isNewerThan(today)) or (somefile in folders at 3-5 levels deep)...
phuclv
  • 30,396
  • 15
  • 136
  • 260
1

Fundamental difference between ls and find

This other answer is good, it makes several valid points; but it does not explicitly state what I find the fundamental difference between ls and find. I won't repeat the points, the purpose of my answer is to emphasize the fundamental difference. Here it is:

ls lists files. find evaluates an expression.

The specification of find or man 1 find in your Linux/Unix may welcome you with the following line:

find - find files

and you may wonder what the difference between "finding" and "listing" is. This "find files" is kinda misleading. The real purpose of find is revealed later in the description:

The find utility shall recursively descend the directory hierarchy from each file specified by path, evaluating a Boolean expression composed of the primaries […] for each file encountered.

If the expression contains (explicit or implicit) -print, then yes, find "finds files", i.e. it prints their pathnames, therefore it's similar to ls. But you are not limited to this. You can build custom, arbitrary expressions. You can make find do something with files or because of files.

I have used find to answer many question on this site and thus I can give you several examples that illustrate what I mean. Where I link to an answer, I urge you to also read the respective question to see what problem the answer is supposed to solve.

My examples:

  • This answer describes how find works in general (the "theory" part) and then (later parts) concentrates on finding files where tests base on pathnames. From it you can learn the "philosophy" of find.

  • This answer builds custom tests that use grep for the purpose of finding files.

  • This question is a basic example of the ability of find to do something to files (in this particular case: to delete files).

  • The "portable solution" in this answer makes find run a shell (or shells) to do a custom action to files (in this case: to move them). Additionally there's an implementation of a counter that limits the solution to 1000 successful actions.

  • This answer uses find not to find (list) files per se, nor to do anything to files, but to do something because of files. The goal is to count files. The "analysis" part may give you some insight into what ls is not designed for.

I consider ls as a rather specialized tool whose output is designed to be human-readable, while find is more general and its scope reaches beyond finding or listing files. find alone cannot fully replace ls though; ls supports options for sorting entries, find does not (see this question).


Specific difference between verbatim ls -R and sole find

any caveats if I use ls -R instead of find?

Aside from the general "do not parse ls" advice and other points made by the already linked answer, I note that in general the output of ls -R consists of blocks of the form:

./path/to/subdir:
filename 1
filename 2
another filename

If you "grep the file name from the list" then you will see zero or more entries, each being either a pathname of some subdirectory (with : appended!) or a filename of some file. It's easy to get filename(s) without information where (in what subdirectory) the file(s) are.

On the other hand sole find (which for many implementations is equivalent to find . -print) gives you pathnames, always. While using grep on the output of find is not necessarily the best idea (the best idea may be to create a good expression with -name, -path, -regex (if supported) or so), it's almost always better than using grep on the output of ls -R; unless you just want to know if a file with a matching name exists, not where it exists.