我有一个包含许多音频文件的文件夹,但有些是~4个字节,似乎什么也没有,它们是0秒长,没有声音。我想将它们移动到名为“temp /".
的文件夹中如何将文件夹中少于5个字节的所有文件移动到此文件夹?
答案 0 :(得分:18)
您可以使用find为您执行此操作:
find . -type f -maxdepth 1 -size -5c -exec mv {} temp/ \;
-size -5c
抓取所有小于5个字节的文件。 -
表示小于,c
表示字节。
-maxdepth 1
阻止您在尝试递归到temp /(移动初始文件之后)时尝试移动文件。
-exec mv {} temp/ \;
只需在每个文件上运行mv
即可将它们放入temp({}代替文件名)。转义的分号标志着exec的mv命令的结束。
还有其他尺码可供选择:
`b' for 512-byte blocks (this is the default if no suffix is
used)
`c' for bytes
`w' for two-byte words
`k' for Kilobytes (units of 1024 bytes)
`M' for Megabytes (units of 1048576 bytes)
`G' for Gigabytes (units of 1073741824 bytes)
答案 1 :(得分:2)
find -size 1c
将为您提供完全一个字节的所有文件。
如@user1666959所述,您还可以使用find . -type f -size -4c
,它将查找当前目录(和子目录)中4个字节以及更小的所有文件。
$ find . -maxdepth 1 -type f -size -4c -exec mv {} temp/ \;
(是的,您需要尾随\;
。
请注意,find -size
允许其他确切的文件大小匹配(例如1k
),但也允许您搜索占用磁盘上指定数量的块的文件(不要单元)。
$ man find
提供有关如何使用它进行搜索的更多信息。
答案 2 :(得分:1)
find . -maxdepth 1 -type f -size -5c -exec mv '{}' temp/ \;
答案 3 :(得分:1)
一种解决方案是:
find . -type f -maxdepth 1 | xargs du | sort -n |grep "^[0-5]\t"|sed "s/[0-5]//"|sed "s/^.//"|xargs -I ARG mv ARG temp/
它找到所有文件,列出它们的大小,按类别排序,取所有大小为0,1,2,3,4,5,只获取文件名,然后对它们运行mv命令!