[英]How to find out if ls command output is file or a directory Bash
ls command outputs everything that is contained in current directory. ls命令输出当前目录中包含的所有内容。 For example ls -la will output something like this 例如ls -la将输出如下内容
drwxr-xr-x 3 user user 4096 dec 19 17:53 .
drwxr-xr-x 15 user user 4096 dec 19 17:39 ..
drwxrwxr-x 2 user user 4096 dec 19 17:53 tess (directory)
-rw-r--r-- 1 user user 178 dec 18 21:52 file (file)
-rw-r--r-- 1 user user 30 dec 18 21:47 text (file)
And what if I want to know how much space does all files consume. 如果我想知道所有文件占用多少空间,该怎么办。 For that I would have to sum $5 from all lines with ls -la | awk '{ sum+=$5 } END{print sum}'
为此,我必须从ls -la | awk '{ sum+=$5 } END{print sum}'
所有行中总计5美元ls -la | awk '{ sum+=$5 } END{print sum}'
ls -la | awk '{ sum+=$5 } END{print sum}'
. ls -la | awk '{ sum+=$5 } END{print sum}'
。 So how can I only sum size of files and leave directories behind? 那么,如何只对文件大小求和而不留目录呢?
You can use the following : 您可以使用以下内容:
find . -maxdepth 1 -type f -printf '%s\n' | awk '{s+=$1} END {print s}'
The find
command selects all the files in the current directory and output their size. find
命令选择当前目录中的所有文件并输出它们的大小。 The awk
command sums the integers and output the total. awk
命令将整数相加并输出总数。
Don't. 别。
One of the most quoted pages on SO that I've seen is https://unix.stackexchange.com/questions/128985/why-not-parse-ls-and-what-do-to-instead . 我见过的关于SO引用最多的页面之一是https://unix.stackexchange.com/questions/128985/why-not-parse-ls-and-what-do-to-instead 。
That being said and as a hint for further development, ls -l | awk '/^-/{s+=$5} END {print s}'
话虽如此,并暗示进一步发展, ls -l | awk '/^-/{s+=$5} END {print s}'
ls -l | awk '/^-/{s+=$5} END {print s}'
will probably do what you ask. ls -l | awk '/^-/{s+=$5} END {print s}'
可能会满足您的要求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.