我需要在文件中搜索tar.gz文件而不提取它。之后,我需要将搜索到的文件(如果有的话)复制到另一个文件夹中。
到目前为止,我已经有了这个,但是这一行的复制部分给了我一个错误。
gunzip -c file.tar.gz | tar tvf - | grep filename | -exec cp {} /folder/another_folder \;
Run Code Online (Sandbox Code Playgroud)
或者有没有更好的方法来搜索文件中的tar.gz文件而不提取它?
slm*_*slm 10
你的版本tar不支持switch-z吗?
$ tar ztvf file.tar.gz | grep fliename
Run Code Online (Sandbox Code Playgroud)
如果文件在存档中完全存在,这将返回文件的名称。
如果您想先搜索文件,并且只有在存在时才将其解压缩,您可以执行类似的操作。
$ arc="<tarball>"; file="<file to extract>"; \
tar ztvf $arc | grep $file && tar zxvf $arc $file
Run Code Online (Sandbox Code Playgroud)
$ tar ztvf ffmpeg.static.64bit.2013-10-05.tar.gz
-rwxr-xr-x root/root 19579304 2013-10-05 00:06 ffmpeg
-rwxr-xr-x root/root 19528712 2013-10-05 00:06 ffprobe
Run Code Online (Sandbox Code Playgroud)
$ arc="ffmpeg.static.64bit.2013-10-05.tar.gz"; file="ffmpeg"; \
tar ztvf $arc | grep $file && tar zxvf $arc $file
-rwxr-xr-x root/root 19579304 2013-10-05 00:06 ffmpeg
ffmpeg
Run Code Online (Sandbox Code Playgroud)
确认
$ ll ffmpeg
-rwxr-xr-x 1 manny manny 19579304 Oct 5 00:06 ffmpeg*
Run Code Online (Sandbox Code Playgroud)
如果要将提取的文件输出到其他位置,可以使用tar的-C开关。
$ arc="<tarball>"; file="<file to extract>"; \
tar ztvf $arc | grep $file && tar zxvf $arc -C /path/to/dir $file
Run Code Online (Sandbox Code Playgroud)
$ arc="ffmpeg.static.64bit.2013-10-05.tar.gz"; \
file="ffmpeg"; tar ztvf $arc | grep $file && tar zxvf $arc -C /tmp $file
Run Code Online (Sandbox Code Playgroud)
确认
$ ll /tmp/ffmpeg
-rwxr-xr-x 1 manny manny 19579304 Oct 5 00:06 /tmp/ffmpeg*
Run Code Online (Sandbox Code Playgroud)
$ arc=cp210x.tar.gz; file="cp210x/usb-serial/Makefile"; \
tar ztvf $arc | grep $file && tar zxvf $arc -C /tmp $file
Run Code Online (Sandbox Code Playgroud)
确认
$ ll /tmp/cp210x/usb-serial/Makefile
-rw-rw-r-- 1 manny manny 388 May 13 01:37 /tmp/cp210x/usb-serial/Makefile
Run Code Online (Sandbox Code Playgroud)
如果您想提取文件模式,也可以使用通配符。
$ arc=cp210x.tar.gz; file='*Makefile'; \
tar ztvf $arc | grep -E "$file" && tar zxvf $arc -C /tmp --wildcards "$file"
Run Code Online (Sandbox Code Playgroud)
确认
$ find /tmp/cp210x -ls | grep Makefile
26881948 4 -r--r--r-- 1 manny manny 171 Mar 14 2012 /tmp/cp210x/Linux_3.x.x_VCP_Driver_Source/Makefile
26881960 4 -rw-rw-r-- 1 manny manny 388 May 13 01:37 /tmp/cp210x/usb-serial/Makefile
Run Code Online (Sandbox Code Playgroud)
以上涉及一些额外的变化。我们正在提取匹配的所有内容'*Makefile'。请注意,我们现在将它用单引号和双引号包裹起来。这是为了防止*Makefile意外扩展。
grep现在包括 switch -E,因为我们现在正在搜索正则表达式,而不仅仅是tar. 此外, to 的参数grep也不用双引号括起来。
我们现在使用切换--wildcards到第二个,tar以便我们可以基于模式而不只是单个文件进行提取。这个参数现在也用双引号括起来以保护它。
恐怕你无法一步完成这件事。您无法(轻松)将存档的一部分提取到与存档位置不同的位置。你也许可以这样做:
pattern=filename
file_path="$(tar tzf file.tar.gz 2>/dev/null| grep "$pattern")"
if [ ! -n "$file_path ];then
tar xvzf file.tar.gz "$file_path"
cp "$file_path" /new/location
else
echo "No files matching $pattern found in archive"
fi
Run Code Online (Sandbox Code Playgroud)
现在来说说上面的“容易”的话。GNU 实现tar有一个--strip-components选项,可以获取一个数字,并在提取之前从文件名中删除许多组成部分。因此,您可以计算基本名称之前有多少个组件,以便您可以执行某种“在此处提取”操作,然后进行复制:
pattern=filename
file_path="$(tar tzf file.tar.gz | grep "$pattern" )"
file_name="$(basename "$file_path")"
[ -z "$file_path" ] && echo "No files matching $pattern found." && exit 1
components=0
while [ "$file_path" != '/' ];do
file_path=$(dirname "$file_path")
components=$((components+1))
done
tar xvzf file.tar.gz --strip-components "$components" "$file_path" && cp "$file_name" /new/location
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
41526 次 |
| 最近记录: |