parent_directory/
다음 하위 디렉터리 및 을 포함하는 디렉터리가 child1/
있다고 child2/
가정 해 보겠습니다 child3/
.
zip
모든 *.txt
파일을 해당 디렉터리 에 쓰기 위한 스크립트를 작성 childx/
하려면 어떻게 해야 합니까?childx.zip
childx.zip
childx/
답변1
견본
$ mkdir -p parent_directory/child{1..3}
$ touch parent_directory/child{1..3}/file1.txt
$ touch parent_directory/child{1..3}/file2.txt
$ tree parent_directory/
parent_directory/
├── child1
│ ├── file1.txt
│ └── file2.txt
├── child2
│ ├── file1.txt
│ └── file2.txt
└── child3
├── file1.txt
└── file2.txt
3 directories, 6 files
해결책
이제 zip 파일을 스크립트로 작성하십시오.
$ cd parent_directory/; for i in *; do find ${i} -name "*.txt" -print | zip ${i}.zip -@; mv ${i}.zip ${i}; done; cd -
adding: child1/file2.txt (stored 0%)
adding: child1/file1.txt (stored 0%)
adding: child2/file2.txt (stored 0%)
adding: child2/file1.txt (stored 0%)
adding: child3/file2.txt (stored 0%)
adding: child3/file1.txt (stored 0%)
결과
결과:
$ tree parent_directory/
parent_directory/
├── child1
│ ├── child1.zip
│ ├── file1.txt
│ └── file2.txt
├── child2
│ ├── child2.zip
│ ├── file1.txt
│ └── file2.txt
└── child3
├── child3.zip
├── file1.txt
└── file2.txt
3 directories, 9 files
$ unzip -l parent_directory/child1/child1.zip
Archive: parent_directory/child1/child1.zip
Length Date Time Name
--------- ---------- ----- ----
0 07-05-2018 10:08 child1/file2.txt
0 07-05-2018 10:08 child1/file1.txt
--------- -------
0 2 files
$ unzip -l parent_directory/child2/child2.zip
Archive: parent_directory/child2/child2.zip
Length Date Time Name
--------- ---------- ----- ----
0 07-05-2018 10:08 child2/file2.txt
0 07-05-2018 10:08 child2/file1.txt
--------- -------
0 2 files
$ unzip -l parent_directory/child3/child3.zip
Archive: parent_directory/child3/child3.zip
Length Date Time Name
--------- ---------- ----- ----
0 07-05-2018 10:08 child3/file2.txt
0 07-05-2018 10:08 child3/file1.txt
--------- -------
0 2 files
인용하다
답변2
노력하다,
$ zip -R parent_directory '*.txt'