简体   繁体   English

Bash 脚本到 7za 目录中的所有文件然后重命名文件

[英]Bash script to 7za all files in a directory then rename files

I am working on using 7za to compress and password protect all files in a directory then rename the files.我正在使用 7za 压缩和密码保护目录中的所有文件,然后重命名文件。 Here is my code:这是我的代码:

#!/bin/bash

rename 's/ /_/g' *
rename 's/_-_/_/g' *

Password_string=My_Password_String
a=1

for i in *;
do 7za a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=off -p$Password_string -mhe=on $i.7z $i
    new=$(printf "Summer_Vacat_%04d.7z" "$a") 
    mv -i -- "$i" "$new"
    let a=a+1 #increment counter
done

I am in the directory with all the folders I want to 7za and password protect, the mv command renames the folders before they are compressed.我在包含我想要 7za 和密码保护的所有文件夹的目录中, mv命令在压缩文件夹之前重命名它们。 I want to rename $i.7z to Summer_Vacat_0000.7z, what am I missing here?我想将 $i.7z 重命名为 Summer_Vacat_0000.7z,我在这里缺少什么? This is only the first phase of this program...这只是该计划的第一阶段...

Try this:尝试这个:

#!/bin/bash

Password_string=My_Password_String
a=1

for i in *;
do
  7za a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=off -p$Password_string -mhe=on $i.7z $i
  if [ -f "${i}.7z" ];then
    new=$(printf "Summer_Vacat_%04d.7z" "$a")
    mv -i "${i}.7z" "$new"
    rm -f "$i" # this will remove the unencrypted or original file
    let a=a+1 #increment counter
  fi
done

Note that if your script is in this folder, it will also be encrypted and renamed.. so you need to check for the name and exclude it from the loop请注意,如果您的脚本在此文件夹中,它也将被加密并重命名..因此您需要检查名称并将其从循环中排除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM