标题: find命令的一个坑(shell对*号的自动扩展) https://scz.617.cn/unix/201612061739.txt $ ls -l dir/somefile* -rw-r--r-- 1 root root 0 Dec 6 17:24 dir/somefile -rw-r--r-- 1 root root 0 Dec 6 17:32 dir/somefile_other $ ls -l somefile.ext -rw-r--r-- 1 root root 0 Dec 6 17:24 somefile.ext $ find dir -name somefile* -ls 你会发现上述find命令没有输出,即没有找到dir/somefile。这里有个坑,不是find 的,而是shell的。 $ strace -e execve find dir -name somefile* -ls execve("/usr/bin/find", ["find", "dir", "-name", "somefile.ext", "-ls"], [/* 18 vars */]) = 0 用strace观察,"find dir -name somefile* -ls"由于shell对*号的自动扩展,实际 变成: $ find dir -name somefile.ext -ls 这条命令当然找不到dir/somefile。解决办法是避免shell对*号的自动扩展,比如: $ find dir -name "somefile*" -ls 441426 0 -rw-r--r-- 1 root root 0 Dec 6 17:24 dir/somefile 441427 0 -rw-r--r-- 1 root root 0 Dec 6 17:32 dir/somefile_other 我实际碰上了这个坑。执行find时当前目录下有somefile.ext,死活找不到dir/somefile, 切换当前目录后找到了。我们这种人对任何异常都很警惕,细究之后发现是个老坑。