다수의 tar.gz 아카이브를 사용하기 위한 bash 작업 흐름

다수의 tar.gz 아카이브를 사용하기 위한 bash 작업 흐름

4개의 하위 디렉터리로 구성된 디렉터리로 작업하고 있습니다.

ls -t
pnmrnp40_to_69  pnmrnp9028_to_9100  pnmrnp00_to_39  pnmrnp70_to_9028

각 prmnp* 하위 디렉터리 내부에는 *.tar.gz 아카이브 또는 *.md5sub(무엇인지 모르므로 제거해야 함)에 속하는 많은 파일이 있습니다.

charlie@Precision-7920-Tower:~/Documents/script/mega_data/pnmrnp/pnmrnp40_to_69$ ls -t
ligands57_dir_results.tar.gz.md5sum  ligands40_dir_results.tar.gz.md5sum
ligands57_dir_results.tar.gz         ligands69_dir_results.tar.gz
ligands69_dir_results.tar.gz.md5sum  ligands68_dir_results.tar.gz
ligands68_dir_results.tar.gz.md5sum  ligands67_dir_results.tar.gz
ligands67_dir_results.tar.gz.md5sum  ligands66_dir_results.tar.gz
ligands66_dir_results.tar.gz.md5sum  ligands65_dir_results.tar.gz

각 하위 디렉터리로 이동하는 간단한 bash 작업 흐름이 필요합니다.

  1. 모두 제거 *.md5sub
  2. 모든 *.tar.gz를 동일한 하위 폴더에 압축 해제합니다(원본 아카이브의 이름 유지).

Bash의 작업 흐름은 다음과 같습니다.

#!/bin/bash
# assuming that the script is in the folder contained all subdirectories
dir="$PWD"

# loop each subdirectory
for subdir in ${dir}
cd ${subdir}
# unzip each archive to the same place
for tar in *.tar.gz; do
tar xzvf $tar
done
# return to initial dir
cd ..
done

매우 많은 수의 아카이브에 적용할 수 있도록 이 스크립트를 보다 효율적으로 만들 수 있는 가능성이 있습니까?

답변1

find(1):

...
       -execdir command ;

       -execdir command {} +
              Like -exec, but the specified command is run from the
              subdirectory containing the matched file, which is not normally
              the directory in which you started find.  As with -exec, the {}
              should be quoted if find is being invoked from a shell.  This a
              much more secure method for invoking commands, as it avoids race
              conditions during resolution of the paths to the matched files.
              As with the -exec action, the `+' form of -execdir will build a
              command line to process more than one matched file, but any
              given invocation of command will only list files that exist in
              the same subdirectory.  If you use this option, you must ensure
              that your $PATH environment variable does not reference `.';
              otherwise, an attacker can run any commands they like by leaving
              an appropriately-named file in a directory in which you will run
              -execdir.  The same applies to having entries in $PATH which are
              empty or which are not absolute directory names.  If any
              invocation with the `+' form returns a non-zero value as exit
              status, then find returns a non-zero exit status.  If find
              encounters an error, this can sometimes cause an immediate exit,
              so some pending commands may not be run at all.  The result of
              the action depends on whether the + or the ; variant is being
              used; -execdir command {} + always returns true, while
              -execdir command {} ; returns true only if command returns 0.
...
find -type f -name '*.tar.gz' -execdir tar xvf {} \;

관련 정보