如何在 BASH 中移动少于 5 个字节的所有文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20014201/
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
How to move all files with fewer than 5 bytes in BASH?
提问by Village
I have a folder containing many audio files, but some are ~4 bytes and seem to contain nothing, they are 0 seconds long and have no sound. I want to move them to a folder called "temp/".
我有一个包含许多音频文件的文件夹,但有些是 ~4 字节,似乎什么都不包含,它们的长度为 0 秒并且没有声音。我想将它们移动到名为“temp/”的文件夹中。
How can I move all of the files in the folder that have fewer than 5 bytes to this folder?
如何将文件夹中少于 5 个字节的所有文件移动到此文件夹?
回答by Kyle Kelley
Find!
找!
You can use findto do this for you:
您可以使用find为您执行此操作:
find . -type f -maxdepth 1 -size -5c -exec mv {} temp/ \;
Explanation
解释
-size -5c
grabs all the files less than 5 bytes. The -
indicates less than and the c
indicates bytes.
-size -5c
抓取所有小于 5 个字节的文件。该-
表示小于和c
指示字节。
-maxdepth 1
prevents you from trying to move the files on top of themselves when it tries to recurse into temp/ (after moving your initial files).
-maxdepth 1
当它尝试递归到 temp/ (移动初始文件后)时,防止您尝试将文件移动到它们自身之上。
-exec mv {} temp/ \;
simply runs mv
on each file to put them in temp (the {} is substituted for the name of the file). The escaped semicolon marks the end of the mv command for exec.
-exec mv {} temp/ \;
只需mv
在每个文件上运行以将它们放入临时文件中({} 被替换为文件名)。转义的分号标志着 exec 的 mv 命令的结束。
There are other sizes available as well:
还有其他尺寸可供选择:
`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)
回答by Matthew Schinckel
find -size 1c
will give you all files that are exactlyone byte.
find -size 1c
将为您提供恰好为一个字节的所有文件。
As @user1666959mentions, you can also use find . -type f -size -4c
, which will find all files in the current directory (and subdirectories), that are 4 bytes and smaller.
正如@user1666959 所提到的,您还可以使用find . -type f -size -4c
,它将查找当前目录(和子目录)中的所有文件,即 4 个字节或更小。
$ find . -maxdepth 1 -type f -size -4c -exec mv {} temp/ \;
(Yes, you will need the trailing \;
.
(是的,您将需要尾随\;
.
Note that find -size
allows for other exact file size matches (such as 1k
), but also allows you to search for files that take up the designated number of blocks on the disk (leaving off the unit).
请注意,find -size
允许其他精确的文件大小匹配(例如1k
),但也允许您搜索占用磁盘上指定块数的文件(离开该单元)。
$ man find
Provides a heap more info about how to use it to search.
提供堆有关如何使用它进行搜索的更多信息。
回答by Basilevs
find . -maxdepth 1 -type f -size -5c -exec mv '{}' temp/ \;
回答by mattdee123
One solution would be:
一种解决方案是:
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/
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/
It finds all files, lists their sizes, sorts by that, takes all that are size 0,1,2,3,4,5, gets just the filenames, and then runs the mv command on them!
它查找所有文件,列出它们的大小,按此排序,获取大小为 0、1、2、3、4、5 的所有文件,仅获取文件名,然后对它们运行 mv 命令!