当使用“ls -l”获取目录列表时,如何用逗号显示文件大小?
您可以执行“ls -l
”来获取详细的目录列表,如下所示:
-rw-rw-rw- 1 alice themonkeys 1159995999 2008-08-20 07:01 foo.log
-rw-rw-rw- 1 bob bob 244251992 2008-08-20 05:30 bar.txt
但请注意,您必须沿着屏幕滑动手指才能找出这些文件大小的数量级。
在目录列表中的文件大小中添加逗号的好方法是什么,如下所示:
-rw-rw-rw- 1 alice themonkeys 1,159,995,999 2008-08-20 07:01 foo.log
-rw-rw-rw- 1 bob bob 244,251,992 2008-08-20 05:30 bar.txt
You can do 'ls -l
' to get a detailed directory listing like this:
-rw-rw-rw- 1 alice themonkeys 1159995999 2008-08-20 07:01 foo.log
-rw-rw-rw- 1 bob bob 244251992 2008-08-20 05:30 bar.txt
But notice how you have to slide your finger along the screen to figure out the order of magnitude of those file sizes.
What's a good way to add commas to the file sizes in the directory listing, like this:
-rw-rw-rw- 1 alice themonkeys 1,159,995,999 2008-08-20 07:01 foo.log
-rw-rw-rw- 1 bob bob 244,251,992 2008-08-20 05:30 bar.txt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我刚刚发现它内置于 GNU Core Utils 中并且适用于 ls 和 du!
它可以在 Ubuntu 上运行,但遗憾的是不能在 OSX 上运行。 有关块大小变体的更多信息此处
I just discovered that it's built-in to GNU Core Utils and works for ls and du!
It works on Ubuntu but sadly doesn't on OSX. More on variants of block size here
如果您只关心数量级,
ls -lh
会执行如下操作:If the order of magnitude is all you're interested in,
ls -lh
does something like this:我不认为“ls”具有这种能力。 如果您正在寻找可读性,“ls -lh”将为您提供更易于人类解析的文件大小。
I don't think 'ls' has exactly that capability. If you are looking for readability, 'ls -lh' will give you file sizes that are easier for humans to parse.
这是对 commafy.pl 的改进,它允许您使用 ls 来列出或不列出文件大小。 将
ls
别名为commafy.pl
以使用它。Here's an improvement to commafy.pl, it allows you to use ls with or without listing the file sizes. Alias
ls
tocommafy.pl
to use it.这个常见的 sed 脚本应该可以工作:
但是,我同意之前的评论,建议 ls -lh 可能是达到所需效果的更好的通用解决方案。
This common sed script should work:
However, I agree with the earlier comment suggesting
ls -lh
is probably the better general solution for the desired effect.这是在 OS X 上,因此您可能需要根据您的 Unix 风格对其进行一些调整。 我在 ~/.bashrc 点文件中为此目的创建了这样一个函数。 诀窍是在 awk printf 格式字符串中使用 ' 来表示文件大小。 需要注意的是:awk 在某种程度上破坏了“总”第一行,并且也丢失了终端颜色。 否则,它的优点之一是它尝试使列尽可能保持对齐。 对我来说,这可以立即直观地估计文件有多大。 -h 开关解决方案没问题,但你的大脑需要转换那些 K、B、G。 下面的解决方案的最大优点是您可以通过管道将其排序并且排序会理解它。 例如“lc | sort -k5,5nr”。
This is on OS X, so you might have to tweak it a bit for your Unix flavor. I created such a function for this purpose in my ~/.bashrc dot file. The trick is using ' in the awk printf format string for the file size. A caveat: the awk mangles the "total" first line somewhat, and loses terminal coloration as well. Otherwise, one of its merits is that it tries to keep columns aligned as much as possible. To me this instantly gives a visual estimation of how big a file is. The -h switch solution is okay, but your brain needs to convert those Ks, Bs, Gs. The biggest advantage to the solution below is that you can pipe it to sort and sort would understand it. As in "lc | sort -k5,5nr" for example.
这是一个 perl 脚本,它将过滤 '
ls -l
' 的输出以添加逗号。如果您调用脚本 commafy.pl,则可以将“ls”别名为“
ls -l |” commafy.pl
'。Here's a perl script that will filter the output of '
ls -l
' to add the commas.If you call the script commafy.pl then you can alias 'ls' to '
ls -l | commafy.pl
'.事实上,我正在为一名年轻的实习生寻找测试,这似乎很理想。 这就是他的想法:
它以极其低效的方式给出了大小的数量级。 我将创建这个社区维基,因为我们都对您如何评价他的代码感兴趣,但我不希望我的代表受苦。
请随意发表评论(温柔一点,他是个新手,尽管你不会通过他的 shell 脚本猜到它:-)。
Actually, I was looking for a test for a young trainee and this seemed ideal. Here's what he came up with:
It gives the order of magnitude for the size in a horribly inefficient way. I'll make this community wiki since we're both interested how you rate his code, but I don't want my rep suffering.
Feel free to leave comments (be gentle, he's a newbie, though you wouldn't guess it by his shell scripting :-).
这是我几年前写的,在 stdin 上工作;
读取标准输入和 在数字中插入逗号以提高可读性,并发送到标准输出。
例子;
https://github.com/mikemakuch/commas
I wrote this several years ago, works on stdin;
Read stdin & insert commas in numbers for readability emit to stdout.
example;
https://github.com/mikemakuch/commas
将为最新版本的 GNU ls 执行此操作。
will do this for recent versions of GNU ls.