将子文件夹中的所有.bz2提取到另一个文件夹? [英] Extract all .bz2 in subfolders to another folder?

查看:81
本文介绍了将子文件夹中的所有.bz2提取到另一个文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件夹层次结构如下:

I have a folder hierarchy like below:

folder
-- subfolder1
---- file1.bz2
---- file2.bz2
---- file3.bz2
---- ...
-- subfolder2
---- file4.bz2
---- ...
-- subfolder3
---- file5.bz2
---- ...
-- ...

每个 .bz2 内都是一个 .ppm 文件.我想将 folder 中的所有 .bz2 提取到另一个文件夹,例如 newFolder .

Inside each .bz2 is a .ppm file. I want to extract all of the .bz2 within folder to another folder, say newFolder.

// assume file.ppm is extracted from file.bz2
newFolder
-- file1.ppm
-- file2.ppm
-- file3.ppm
-- file4.ppm
-- file5.ppm
-- ...

或者只是 1.ppm 2.ppm 等.如何一口气用bash做到这一点?

Alternatively just 1.ppm, 2.ppm, etc. How can this be done in bash in one fell swoop?

推荐答案

您可以使用 find -exec ,但是您将需要一个帮助程序脚本来处理参数需要从原始文件名中删除 .bz2 扩展名并添加 .ppm 扩展名所需的扩展名.

You can use find and -exec, but you will need a helper script to handle the parameter expansions required to remove the .bz2 extension from the original filename and add the .ppm extension.

您将切换到 newfolder 目录,然后所需的find命令的常规格式为:

You will change to the newfolder directory, and then the general form of the find command you will want is:

$ find /path/to/folder -type f -name "*bz2" -exec /path/to/helper.sh '{}' \;

您的帮助程序脚本将解压缩bzip文件,保留原始文件,并将解压缩的输出重定向到当前目录中的新文件.(您还应该进行最小程度的验证,以确保在盲目尝试解压缩之前将参数传递给 helper.sh ).辅助脚本可能很简单:

Your helper script will decompress the bzip file preserving the original and redirecting the decompressed output to the new file in the current directory. (you should also do minimal validation that an argument was passed to helper.sh before blindly attempting to decompress). The helper script could be a simple:

#!/bin/bash

[ -z "$1" ] && return 1   ## validate argument past
[ -r "$1" ] || return 1   ## validate argument is readable file

fn="${1##*/}"             ## remove path components

## decompress to stdout, preserving original, redirect to new name
bzip2 -cdk "$1" > "${fn%.bz2}.ppm"

(确保您使 helper.sh 可执行,例如 chmod + x/path/to/helper.sh)

(make sure you make helper.sh executable, e.g. chmod +x /path/to/helper.sh)

以包含以下内容的文件夹开头的简短示例:

A short example beginning with a folder containing:

$ l1 ../folder
file1.bz2
file2.bz2
file3.bz2
file4.bz2

,当前目录 newfolder 为空,在父目录中为 helper.sh ,您将:

and the present directory newfolder empty with helper.sh in the parent directory, you would:

$ find ../folder -type f -name "*bz2" -exec ../helper.sh '{}' \;

现在,当前目录 newfolder 包含原始 .bz2 文件的解压缩内容:

Now the present directory newfolder contains the decompressed contents of the original .bz2 files:

$ l1
file1.ppm
file2.ppm
file3.ppm
file4.ppm

,其中原始 .bz2 文件保留在原始目录文件夹中.

with the original .bz2 files preserved in the original directory folder.

仔细研究一下,如果您有任何疑问,请告诉我.

Look things over and let me know if you have questions.

这篇关于将子文件夹中的所有.bz2提取到另一个文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆