2015-09-28 38 views
1

我需要將一個zip文件中的所有文件從AAAAA-filename.txt重命名爲BBBBB-filename.txt,並且我想知道如果我可以自動執行此任務而不必提取所有文件,請重命名並重新壓縮。一次解壓一個,重命名和再次壓縮是可以接受的。如何在不提取和重新壓縮的情況下重命名zip歸檔文件中的文件?

我現在擁有的是:

for file in *.zip 
do 
    unzip $file 
    rename_txt_files.sh 
    zip *.txt $file 
done; 

但我不知道是否有這個地方我沒有使用所有額外的磁盤空間票友版本。

+0

使用zipnote的重命名功能? http://www.computerhope.com/unix/zipnote.htm – bishop

+0

我沒有權限在需要執行此@bishop的機器上安裝軟件。 – Topo

+0

好吧,在舊名稱和新名稱之間創建映射,然後遍歷該映射。如果存檔中存在舊名稱,請將其從存檔提取到磁盤,在磁盤上重命名,將新重命名添加到存檔。如果成功,請從存檔中刪除舊名稱。可以在bash中完成,但是我發現bash數組很麻煩,所以我可能會使用P *語言。 – bishop

回答

1

計劃

  • 用繩子
  • 使用DD文件名查找偏移覆蓋新名稱(注意只使用相同的文件名長度的工作)。否則還必須找到並覆蓋 的filenamelength場..

備份您的壓縮文件嘗試這一

zip_rename.sh

#!/bin/bash 

strings -t d test.zip | \ 
grep '^\s\+[[:digit:]]\+\sAAAAA-\w\+\.txt' | \ 
sed 's/^\s\+\([[:digit:]]\+\)\s\(AAAAA\)\(-\w\+\.txt\).*$/\1 \2\3 BBBBB\3/g' | \ 
while read -a line; do 
    line_nbr=${line[0]}; 
    fname=${line[1]}; 
    new_name=${line[2]}; 
    len=${#fname}; 
# printf "line: "$line_nbr"\nfile: "$fname"\nnew_name: "$new_name"\nlen: "$len"\n"; 
    dd if=<(printf $new_name"\n") of=test.zip bs=1 seek=$line_nbr count=$len conv=notrunc 
done; 

輸出

$ ls 
AAAAA-apple.txt AAAAA-orange.txt zip_rename.sh 
$ zip test.zip AAAAA-apple.txt AAAAA-orange.txt 
    adding: AAAAA-apple.txt (stored 0%) 
    adding: AAAAA-orange.txt (stored 0%) 
$ ls 
AAAAA-apple.txt AAAAA-orange.txt test.zip zip_rename.sh 
$ ./zip_rename.sh 
15+0 records in 
15+0 records out 
15 bytes (15 B) copied, 0.000107971 s, 139 kB/s 
16+0 records in 
16+0 records out 
16 bytes (16 B) copied, 0.000109581 s, 146 kB/s 
15+0 records in 
15+0 records out 
15 bytes (15 B) copied, 0.000150529 s, 99.6 kB/s 
16+0 records in 
16+0 records out 
16 bytes (16 B) copied, 0.000101685 s, 157 kB/s 
$ unzip test.zip 
Archive: test.zip 
extracting: BBBBB-apple.txt   
extracting: BBBBB-orange.txt   
$ ls 
AAAAA-apple.txt BBBBB-apple.txt test.zip 
AAAAA-orange.txt BBBBB-orange.txt zip_rename.sh 
$ diff -qs AAAAA-apple.txt BBBBB-apple.txt 
Files AAAAA-apple.txt and BBBBB-apple.txt are identical 
$ diff -qs AAAAA-orange.txt BBBBB-orange.txt 
Files AAAAA-orange.txt and BBBBB-orange.txt are identical 
相關問題