I have a bunch of files that have just hashes as names and no file endings. (It's an iPhone backup to be precise.) I know there are SQLite databases amongst these files.
How do I find them?
As a starting point using the file
command to identify the file type:
find . -print0 | xargs -0 file
Result:
./.X11-unix: sticky directory
./.Test-unix: sticky directory
./test.db: SQLite 3.x database
Then add some grepping to filter out results.
find
to run the file
command, no need for xargs
: find . -type f -exec file {} + | grep SQLite
.
xargs
and -exec
are not exact equivalents - I understand that -exec
could fail if the argument list is too long (too many files found). "When you use -exec to do the work you run a separate instance of the called program for each element of input. So if find comes up with 10,000 results, you run exec 10,000 times. With xargs, you build up the input into bundles and run them through the command as few times as possible, which is often just once." Source: xargs vs. exec {}
-exec
with +
: "This variant of the -exec action runs the specified command on the selected files, but the command line is built by appending each selected file name at the end; the total number of invocations of the command will be much less than the number of matched files." from man find
. Or the man
page from OSX: "Same as -exec, except that ``{}'' is replaced with as many pathnames as possible for each invocation of utility. This behaviour is similar to that of xargs(1)."
Commented
Mar 22, 2020 at 14:17
find
will never fail if there are too many files. When using ;
instead of +
, the warning you quoted simply explains it will run as many processes as there are files, but it runs them sequentially and separately so there is no issue about the argument list ever being too long.
The utility file
identifies files based on their magic number and other identifying characteristics.
file <filename>
will output something like
<filename>: SQLite 3.x database
To obtain all sqlite3 database files in a directory, you could do something like
file * | grep SQLite