如何递归列表文件的大小和上次修改时间?

时间:2023-02-07 15:06:12

Given a directory i'm looking for a bash one-liner to get a recursive list of all files with their size and modified time tab separated for easy parsing. Something like:

给定一个目录我正在寻找一个bash one-liner来获取所有文件的递归列表,其大小和修改时间选项卡分开以便于解析。就像是:

cows/betsy       145700    2011-03-02 08:27
horses/silver    109895    2011-06-04 17:43

4 个解决方案

#1


9  

You can use stat(1) to get the information you want, if you don't want the full ls -l output, and you can use find(1) to get a recursive directory listing. Combining them into one line, you could do this:

如果不需要完整的ls -l输出,可以使用stat(1)来获取所需的信息,并且可以使用find(1)来获取递归目录列表。将它们组合成一行,您可以这样做:

# Find all regular files under the current directory and print out their
# filenames, sizes, and last modified times
find . -type f -exec stat -f '%N %z %Sm' '{}' +

If you want to make the output more parseable, you can use %m instead of %Sm to get the last modified time as a time_t instead of as a human-readable date.

如果要使输出更易于解析,可以使用%m而不是%Sm将最后修改时间作为time_t而不是人类可读日期。

#2


6  

find is perfect for recursively searching through directories. The -ls action tells it to output its results in ls -l format:

find非常适合递归搜索目录。 -ls动作告诉它以ls -l格式输出结果:

find /dir/ -ls

On Linux machines you can print customized output using the -printf action:

在Linux机器上,您可以使用-printf操作打印自定义输出:

find /dir/ -printf '%p\t%s\t%t\n'

See man find for full details on the format specifiers available with -printf. (This is not POSIX-compatible and may not be available on other UNIX flavors.)

有关-printf可用的格式说明符的完整详细信息,请参阅man find。 (这不是POSIX兼容的,可能在其他UNIX版本上不可用。)

#3


2  

find * -type f -printf '%p\t%s\t%TY-%Tm-%Td %Tk:%TM\n'

find * -type f -printf'%p \ t%s \ t%TY-%Tm-%Td%Tk:%TM \ n'

If you prefer fixed-width fields rather than tabs, you can do things like changing %s to %10s.

如果您更喜欢固定宽度字段而不是制表符,则可以执行将%s更改为%10s之类的操作。

I used find * ... to avoid the leading "./" on each file name. If you don't mind that, use . rather than * (which also shows files whose names start with .). You can also pipe the output through sed 's/^\.\///'.

我使用find * ...来避免每个文件名的前导“./”。如果您不介意,请使用。而不是*(也显示名称以。开头的文件)。你也可以通过sed的/^\.\///'来输出输出。

Note that the output order will be arbitrary. Pipe through sort if you want an ordered listing.

请注意,输出顺序是任意的。如果您想要有序列表,请通过排序进行管道。

#4


1  

You could try this for recursive listing from current folder called "/from_dir"

您可以尝试从当前名为“/ from_dir”的文件夹进行递归列表

find /from_dir/* -print0 | xargs -0 stat -c “%n|%A|%a|%U|%G” > permissions_list.txt



Lists files and directories passes through to stat command and puts all the info into a file called permissions_list.txt


列表文件和目录将传递给stat命令,并将所有信息放入名为permissions_list.txt的文件中

“%n|%A|%a|%U|%G” will give you the following result in the file:

“%n |%A |%a |%U |%G”将在文件中显示以下结果:

from_
    dir|drwxr-sr-x|2755|root|root
    
from_dir/filename|-rw-r–r–|644|root|root


Cheers!


#1


9  

You can use stat(1) to get the information you want, if you don't want the full ls -l output, and you can use find(1) to get a recursive directory listing. Combining them into one line, you could do this:

如果不需要完整的ls -l输出,可以使用stat(1)来获取所需的信息,并且可以使用find(1)来获取递归目录列表。将它们组合成一行,您可以这样做:

# Find all regular files under the current directory and print out their
# filenames, sizes, and last modified times
find . -type f -exec stat -f '%N %z %Sm' '{}' +

If you want to make the output more parseable, you can use %m instead of %Sm to get the last modified time as a time_t instead of as a human-readable date.

如果要使输出更易于解析,可以使用%m而不是%Sm将最后修改时间作为time_t而不是人类可读日期。

#2


6  

find is perfect for recursively searching through directories. The -ls action tells it to output its results in ls -l format:

find非常适合递归搜索目录。 -ls动作告诉它以ls -l格式输出结果:

find /dir/ -ls

On Linux machines you can print customized output using the -printf action:

在Linux机器上,您可以使用-printf操作打印自定义输出:

find /dir/ -printf '%p\t%s\t%t\n'

See man find for full details on the format specifiers available with -printf. (This is not POSIX-compatible and may not be available on other UNIX flavors.)

有关-printf可用的格式说明符的完整详细信息,请参阅man find。 (这不是POSIX兼容的,可能在其他UNIX版本上不可用。)

#3


2  

find * -type f -printf '%p\t%s\t%TY-%Tm-%Td %Tk:%TM\n'

find * -type f -printf'%p \ t%s \ t%TY-%Tm-%Td%Tk:%TM \ n'

If you prefer fixed-width fields rather than tabs, you can do things like changing %s to %10s.

如果您更喜欢固定宽度字段而不是制表符,则可以执行将%s更改为%10s之类的操作。

I used find * ... to avoid the leading "./" on each file name. If you don't mind that, use . rather than * (which also shows files whose names start with .). You can also pipe the output through sed 's/^\.\///'.

我使用find * ...来避免每个文件名的前导“./”。如果您不介意,请使用。而不是*(也显示名称以。开头的文件)。你也可以通过sed的/^\.\///'来输出输出。

Note that the output order will be arbitrary. Pipe through sort if you want an ordered listing.

请注意,输出顺序是任意的。如果您想要有序列表,请通过排序进行管道。

#4


1  

You could try this for recursive listing from current folder called "/from_dir"

您可以尝试从当前名为“/ from_dir”的文件夹进行递归列表

find /from_dir/* -print0 | xargs -0 stat -c “%n|%A|%a|%U|%G” > permissions_list.txt



Lists files and directories passes through to stat command and puts all the info into a file called permissions_list.txt


列表文件和目录将传递给stat命令,并将所有信息放入名为permissions_list.txt的文件中

“%n|%A|%a|%U|%G” will give you the following result in the file:

“%n |%A |%a |%U |%G”将在文件中显示以下结果:

from_
    dir|drwxr-sr-x|2755|root|root
    
from_dir/filename|-rw-r–r–|644|root|root


Cheers!