Let's say I want to list all php files in a directory including sub-directories, I can run this in bash:
ls -l $(find. -name *.php -type f)
The problem with this is that if there are no php files at all, the command that gets executed is ls -l, listing all files and directories, instead of none.
This is a problem, because I'm trying to get the combined size of all php files using
ls -l $(find . -name *.php -type f) | awk '{s+=$5} END {print s}'
如果没有与*.php模式匹配的文件,则最终返回所有条目的大小.
我该如何防范此事件?
fed*_*qui 17
I suggest you to firstly search the files and then perform the ls -l (or whatever else). For example like this:
find . -name "*php" -type f -exec ls -l {} \;
Run Code Online (Sandbox Code Playgroud)
and then you can pipe the awk expression to make the addition.
如果你想避免解析ls来获得总大小,你可以说:
find . -type f -name "*.php" -printf "%s\n" | paste -sd+ | bc
Run Code Online (Sandbox Code Playgroud)