Linux 如何使用BASH查找文件的元信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11898586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:03:21  来源:igfitidea点击:

how to find the meta information of a file using BASH

linuxbashmetadata

提问by Andy

How to find the meta-information of a file in BASH? And how to extract and print it separately using cut and grep commands?

如何在 BASH 中查找文件的元信息?以及如何使用 cut 和 grep 命令分别提取和打印它?

回答by HonkyTonk

I'm just guessing here, but have you tried the command file? It will try to identify what kind of file it is.

我只是在这里猜测,但是您是否尝试过该命令file?它将尝试识别它是什么类型的文件。

回答by derobert

Instead of parsing the output of lsusing cut/grep, you should just use statwhich takes a -cargument to specify the output format.

而不是解析ls使用cut/的输出grep,您应该只使用statwhich 接受一个-c参数来指定输出格式。

anthony@Zia:~$ stat -c '%n : %A : %U : %s' afiedt.buf .XCompose 
afiedt.buf : -rw-r--r-- : anthony : 178
.XCompose : lrwxrwxrwx : anthony : 38

You can change the output format however you'd like; check the stat(1) manpagefor details.

您可以随意更改输出格式;查看stat(1) 联机帮助页了解详细信息。

回答by Burhan Khalid

Execute stat -lon the file:

stat -l在文件上执行:

[~]$ stat -l test.py
-rw-r--r-- 1 burhan staff 84 Aug  3 01:08:34 2012 test.py

To store this information in a variable:

将此信息存储在变量中:

[~]$ foo=$(stat -l test.py)
[~]$ echo $foo
-rw-r--r-- 1 burhan staff 84 Aug 3 01:08:34 2012 test.py

To get specific information only man statand check the format specifiers.

仅获取特定信息man stat并检查格式说明符。