This is a little mystery regarding the difference between -exec {} and xargs that I'm trying to understand. I would appreciate any guidance on this. As background, I've just learned about the "locate" command and I successfully created a database ~/.u.db of a.network drive using this command (this is in Windows WSL, by the way):
$ updatedb -l 0 -o ~/.u.db -U /mnt/u
I'm trying to use it to find all.m files from my current working directory and display the detailed file information. Normally I use the "find" command like this:
$ find . -type f -name '*.m' -exec ls -l \{\} \;
which successfully gives me a list of.m files with their file properties like this:
{all the file info} ./{relative path}/filename1.m
{all the file info} ./{relative path}/filename2.m
when I try to run the locate command in a similar manner I get an error.
$ locate -d ~/.u.db "$PWD*/*.m" -exec -ls -l \{\} \;
locate: invalid option -- 'x'
Just for reference, this works properly (without the extra file info that I'm looking for):
$ locate -d ~/.u.db "$PWD*/*.m"
/{absolute path}/filename1.m
/{absolute path}/filename2.m
I found that I can use xargs like this successfully:
$ locate -d ~/.u.db "$PWD*/*.m" | xargs ls -l
{all the file info} /{absolute path}/filename1.m
{all the file info} /{absolute path}/filename2.m
I'm just trying to understand why I should use xargs in this case and not -exec {}. I would appreciate any insight into the difference in behavior.
I'm not sure how much this varies, but on the Linuxes I have at hand — both running Debian derivatives — the locate
command does not have an -exec
switch. They all use the GNU version of locate
, which follows the GNU convention that long options are introduced with --
and short options with -
, and that short options can be combined if they don't take arguments. In other words, -exec
is eqivalent to -e -x -e -c
, which is why it complains about an invalid -x
switch.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.