简体   繁体   English

按名称递归查找所有文件,使用shell脚本解密并重命名它们

[英]Find all files by name recursively, decrypt and rename them using shell script

I'm trying to write a script to find all of the files with .production in names, decrypt those files and save copies of them without .production .我正在尝试编写一个脚本来查找名称中带有.production所有文件,解密这些文件并保存它们的副本,而无需.production

Example files:示例文件:

./functions/key.production.json
./src/config.production.js

Here is my code:这是我的代码:

decrypt() {
  echo $1

  for file in $(find . -name "*.$1.*")
  do
      echo "some $file"
      openssl enc -aes-128-cbc -a -d -salt -pass pass:asdffdsa -in $file -out $(sed -e "p;s/.$1//")
  done
}

$(sed -e "p;s/.$1//") is the part that hangs. $(sed -e "p;s/.$1//")是挂起的部分。 You can check that out by adding set -x and executing your script.您可以通过添加set -x并执行脚本来检查它。 This is because sed expectes an input file/stream, and there is none given to it.这是因为sed需要输入文件/流,而没有给它。

You could rather use bash substring replacement "${file//.$1}"您宁愿使用 bash 子字符串替换"${file//.$1}"

${string//$substring_to_remove/} ${string//$substring_to_remove/}

All occurrences of the content after // is replaced in the main string, with the content after the last / //之后所有出现的内容在主字符串中被替换,最后一个/之后的内容

So, the working function would be所以,工作函数将是

decrypt() {
  echo $1

  for file in $(find . -name "*.$1.*")
  do
      echo "some $file"
      openssl enc -aes-128-cbc -a -d -salt -pass pass:asdffdsa -in $file -out "${file//.$1}"
  done
}

You can avoid the subshell $(find . -name " .$1. ") by using a while loop.您可以通过使用 while 循环来避免子 shell $(find . -name " .$1. ")

decrypt() {
  echo "$1"
  local file
  while read -r file; do
    echo "some $file"
    PROCESS-YOUR-FILE-AND-DO-YOUR-STUFF_HERE
  done < <(find . -name "*.$1.*")
}

see

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

相关问题 如何使用 linux 中的 bash shell 脚本递归地重命名文件中的所有目录、文件和文本 - How to rename all directories , files and text in the files recursively using bash shell script in linux 递归查找目录并在Shell脚本中重命名 - Recursively find a directory and rename it in Shell Script 使用 find 以随机字符递归重命名文件 - Using find to rename files recursively with random chars 使用 find 和 sed 递归重命名文件 - Recursively rename files using find and sed Shell脚本在文件列表中查找单词,所有这些单词均作为参数给出 - shell script to find a word in a list of files, all of them given as parameters 在目录中递归重命名某些文件-Shell / Bash脚本 - Rename certain files recursively in a directory - Shell/Bash script 使用Linux Shell脚本重命名多个文件 - Multiple files rename using linux shell script 查找所有扩展名为.sh .cpp .c的文件,并将其复制到我的桌面目录中,如果存在同名文件,则将其重命名 - find all files with .sh .cpp .c extension and copy them to a directory in my desktop, if there is a file with the same name, rename it 以递归方式查找今天的所有文件并复制它们 - Find all files from today recursively and copy them 查找所有文件,并将它们复制到一个文件夹(递归展平) - Find all files, and copy them to a folder (Flatten recursively)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM