Python Bash命令使用find批处理文件并按大小排序
我正在寻找Linux命令,该命令以文件大小的升序批处理当前目录中的所有文件 作为一个具体示例,myPython Bash命令使用find批处理文件并按大小排序,python,linux,bash,Python,Linux,Bash,我正在寻找Linux命令,该命令以文件大小的升序批处理当前目录中的所有文件 作为一个具体示例,myhello.py打印文件名: print 'hello', sys.argv[1] 如果我的当前目录中有文件file1、file2和file3,其大小(file1)使用ls ls使用-S开关可以方便地按大小排序 for x in $(ls -S); do python hello.py $x done 或作为一行:表示x,单位为美元(ls-S);做
hello.py
打印文件名:
print 'hello', sys.argv[1]
如果我的当前目录中有文件file1
、file2
和file3
,其大小(file1)使用ls
ls
使用-S
开关可以方便地按大小排序
for x in $(ls -S); do
python hello.py $x
done
或作为一行:表示x,单位为美元(ls-S);做python hello.py$x;完成
或者使用xargs
,如下所示:ls-1-S | xargs-n1 python hello.py
,但要小心,因为这会将文件名中的空格分割成多个文件,下面将详细介绍*
使用find而不更改hello.py
说明:
du
使用文件大小进行注释排序
按该大小列排序cut
删除额外的大小列,以仅保留第二列,即文件名xargs
在每行调用hello.pyfind . -type f | xargs du | sort -n | cut -f 2 | python hello.py
*如果您需要支持带有空格的文件名,我们应该使用以0结尾的行,因此:
使用ls
ls
使用-S
开关可以方便地按大小排序
for x in $(ls -S); do
python hello.py $x
done
或作为一行:表示x,单位为美元(ls-S);做python hello.py$x;完成
或者使用xargs
,如下所示:ls-1-S | xargs-n1 python hello.py
,但要小心,因为这会将文件名中的空格分割成多个文件,下面将详细介绍*
使用find而不更改hello.py
说明:
du
使用文件大小进行注释排序
按该大小列排序cut
删除额外的大小列,以仅保留第二列,即文件名xargs
在每行调用hello.pyfind . -type f | xargs du | sort -n | cut -f 2 | python hello.py
*如果您需要支持带有空格的文件名,我们应该使用以0结尾的行,因此:
有没有可能坚持“查找”并使用管道呢?“ls-1-S | xargs-n1 hello.py”应该完成这项工作?可能是ls的a-R开关?当然,但我想他会继续使用
find
谢谢你的详细回答!有没有可能坚持“查找”并使用管道呢?“ls-1-S | xargs-n1 hello.py”应该完成这项工作?可能是ls的a-R开关?当然,但我想他会继续使用find
谢谢你的详细回答!仅供参考,没有“Linux命令”这样的东西。您在shell(例如Bash)中调用的是程序(如/bin/ls
)或shell命令(如cd
)。因此,您应该始终提到您正在使用的实际shell,因为它们之间的语法差异有时非常显著。仅就您的信息而言,没有“Linux命令”这样的东西。您在shell(例如Bash)中调用的是程序(如/bin/ls
)或shell命令(如cd
)。因此,您应该始终提到您正在使用的实际shell,因为它们之间的语法差异有时非常显著。
# hello.py
import sys
def process(filename):
print 'hello ', filename
if __name__ == '__main__':
for filename in sys.stdin.readlines():
process(filename)
find . -type f | xargs du | sort -n | cut -f 2 | python hello.py
find . -type f -print0 | xargs -0 du | ...