Hadoop 与Bash:删除匹配范围的文件名
假设 HDFS 中有一个文件列表,具有公共前缀和递增后缀。例如,
part-1.gz, part-2.gz, part-3.gz, ..., part-50.gz
我只想在目录中保留几个文件,比如 3 个。任何三个文件都可以。这些文件将用于测试,因此文件的选择并不重要。
什么是简单的&删除其他 47 个文件的最快方法是什么?
Say you have a list of files in HDFS with a common prefix and an incrementing suffix. For example,
part-1.gz, part-2.gz, part-3.gz, ..., part-50.gz
I only want to leave a few file in the directory, say 3. Any three files will do. The files will be used for testing so the choice of files doesn't matter.
What's the simples & fastest way to delete the 47 other files?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这里有几个选项:
手动将三个文件移动到新文件夹,然后删除旧文件夹。
使用
fs -ls
获取文件名,然后拉出前 n 个,然后 rm 它们。在我看来,这是最可靠的方法。hadoop fs -ls /path/to/files
为您提供 ls 输出hadoop fs -ls /path/to/files | grep '部分' | awk '{print $8}'
仅打印出文件名(相应地调整 grep 以获取所需的文件)。hadoop fs -ls /路径/到/文件| grep '部分' | awk '{print $8}' | head -n47 抓取前 47 个
将其放入 for 循环并 rm 它们:
您可以使用 xargs 代替 for 循环:
感谢 Keith 的启发
Few options here:
Move three files manually over to a new folder, then delete the old folder.
Grab the files names with
fs -ls
, then pull the top n, then rm them. This is the most robust method, in my opinion.hadoop fs -ls /path/to/files
gives you ls outputhadoop fs -ls /path/to/files | grep 'part' | awk '{print $8}'
prints out only the file names (adjust the grep accordingly to grab the files you want).hadoop fs -ls /path/to/files | grep 'part' | awk '{print $8}' | head -n47
grabs the top 47Throw this into a for loop and rm them:
Instead of a for-loop, you could use
xargs
:Thanks to Keith for the inspiration
在巴什?
您想要保留哪些文件以及为什么?他们的名字是什么?在上面的示例中,您可以执行以下操作:
这将删除除part-1.gz、part-2.gz 和part-3.gz 之外的所有文件。
您还可以执行以下操作:
这将删除除列出的最后三个文件之外的所有文件。
您也可以这样做:
如果目录中有成百上千个文件,这会更安全。
In Bash?
What files do you want to keep and why? What are their names? In the above example, you could do something like this:
which will remove all files except part-1.gz, part-2.gz, and part-3.gz.
You can also do something like this:
Which will remove all except the last three files listed.
You could also do this:
Which is safer if you have hundreds and hundreds of files in the directory.
您需要保留前三个还是最后三个?
删除除前三个以外的所有内容: 要
删除除最后三个以外的所有内容:
请注意,这些命令不依赖于文件的实际数量,也不依赖于三个以上文件的存在,也不依赖于原始列表的精确排序,但它们确实取决于数字位于连字符之后的事实。
xargs
的参数并不是绝对必要的,但它们在某些情况下可能会有所帮助。Do you need to keep the first three or the last three?
To remove all but the first three:
To remove all but the last three:
Note that these commands don't depend on the actual number of files, nor on the existence of more than three, nor on the precise sorting of the original listing, but they do depend on the fact that the number is after a hyphen. The parameters to
xargs
aren't strictly necessary, but they may be helpful in certain situations.awk:
awk :