bash 找到 *.tar 然后解压并删除文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25830095/
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
find *.tar then extract and delete the files
提问by CJ87
I'm trying to find a tar file, extract the files then remove all the extracted files - I'm able to perform the find and extraction or find the file and remove it but I'm not able to string all three together.
我正在尝试查找 tar 文件,提取文件,然后删除所有提取的文件 - 我能够执行查找和提取或查找文件并将其删除,但我无法将所有三个文件串在一起。
Here is my best attempt below. It runs without error but doesn't delete the extracted files so I'm stuck on how to remove the files I've extracted to the current directory.
下面是我最好的尝试。它运行没有错误,但不会删除提取的文件,所以我一直在思考如何删除我提取到当前目录的文件。
find ~ -name '*.tar' | xargs tar -xf && rm -f
I tried extracting the tar to another directory then removing the directory but couldn't get it to work while using xargs. I've tried searching quite a few different areas but couldn't find anything so I appreciate the help.
我尝试将 tar 解压缩到另一个目录,然后删除该目录,但在使用 xargs 时无法使其工作。我已经尝试搜索了很多不同的区域,但找不到任何东西,所以我很感激你的帮助。
回答by Barmar
The &&
ends the pipeline, it's not part of the xargs
command.
在&&
结束的管道,它不是一部分xargs
的命令。
You can just run the commands using the -exec
option to find
:
您可以使用以下-exec
选项运行命令find
:
find ~ -name '*.tar' -exec tar -xf {} \; -exec rm -f {} \;
回答by Cyrus
To run two or multiple commands with xargs:
使用 xargs 运行两个或多个命令:
find ~ -name '*.tar' | xargs -I {} sh -c 'tar -xf {} && rm -f {}'
Only after successfully unpacking the tar file is deleted.
只有成功解压后,tar 文件才会被删除。