简体   繁体   English

如何使用名称中的计数将不同文件夹中的所有文件重命名为另一个文件夹?

[英]How to rename all the files across different folders into another folder using count in the name?

I am under Ubuntu and I would like to write a bash script such that the following configuration is changed from:我在 Ubuntu 下,我想编写一个 bash 脚本,以便将以下配置更改为:

--images
  |--video1
  |  |--frame1.jpg
  |  |--frame2.jpg
  |  |-- ...
  |  |--frame32.jpg
  |
  |--video2
  |  |--frame1.jpg
  |  |--frame2.jpg
  |  |-- ...
  |  |--frame32.jpg
  |
  |-- ...
  |
  |--video900
  |  |--frame1.jpg
  |  |--frame2.jpg
  |  |-- ...
  |  |--frame32.jpg

to

|--images
|--final_images
|  |--frame1.jpg
|  |--frames2.jpg
|  |-- ...
|  |--frame28800.jpg

My goal is to move all the frames for each video in the same directory final_images .我的目标是将每个视频的所有帧移动到同一目录final_images中。 So first I would like to go into every folder and rename every frame such that to frame1, frame2,..., frame28800 (every video has 32 frames and I have 900 videos so the total number of frames I have is 28800).所以首先我想将 go 放入每个文件夹并将每个帧重命名为frame1, frame2,..., frame28800 (每个视频有 32 帧,我有 900 个视频,所以我的帧总数为 28800)。 Then I would like to move all the renamed frames into final_images .然后我想将所有重命名的帧移动到final_images中。 Any help would be appreciated, I am not very proficient with bash.任何帮助将不胜感激,我不是很精通 bash。

Edit: This is what I tried, being in images folder:编辑:这是我尝试过的,位于图像文件夹中:

A=0; 
for i in $(ls -d */); do 
    for file in $(ls ${i%%} */); do 
       mv "$(pwd)/${i%%}${file%%}" "$(pwd)/${i%%}${file%%}_$A" ;  
       ((A++)); 
       echo $A ;  
    done ; 
done

These could be the algorithm steps:这些可能是算法步骤:

  • initialize a count variable初始化一个计数变量
  • get all files from source dir从源目录获取所有文件
  • exclude directories.排除目录。 We need just files我们只需要文件
  • copy the source file to the target folder, renaming it using the count variable将源文件复制到目标文件夹,使用计数变量重命名
  • increase the count variable增加计数变量
source_folder=$1
target_folder=$2

mkdir -p $target_folder

echo "procesing..."
count=1
for file in $source_folder/* $source_folder/**/* ; do

  if [[ -f $file ]]; then

    cp  $file $target_folder/frame$count.jpg
    echo "source:"$file "target:"$target_folder/frame$count.jpg
    count=$((count + 1))

  else
      echo "$file is not valid or is folder"
  fi
done;

echo ""
echo "target_folder: $target_folder"
find  $target_folder | sort

Save this and execute with this parameters:保存并使用以下参数执行:

bash merge_frames.sh /tmp/workspace/source/ /tmp/workspace/target

result:结果:

结果

暂无
暂无

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

相关问题 将文件夹中的所有文件复制到另一个以 . 到文件名并将其重命名回原始文件名 - Copy all files in folder to another folder prepending a . to the file name and rename it back to original file name 如何根据文件夹名称更改所有文件夹和文件权限? - How to change all folders and files permessions base on folder name? 如何使用模式重命名文件夹中的所有文件 - How to rename all files in folders with pattern 如何从多个文件夹中提取文件并根据文件夹名称 (bash) 重命名? - How can I extract files from multiple folders and rename according to folder name (bash)? 如何重命名(通过添加前缀)从当前文件夹开始的所有文件和文件夹? - How to rename(by adding a prefix) all the files and folders starting from the current folder? linux如何将不同文件夹下的同名文件批量复制重命名到单独的文件夹,文件重命名为父文件夹? - How do batch copy and rename files with same names under different folders to a separate folder with files renamed to their parent folder in linux? 重命名文件夹中的所有文件 - Rename all files in a folder 如何使用find重命名具有相同名称的不同目录中的文件 - How to rename files in different directories with the same name using find 如何重命名文件夹 google colab 中的所有文件 - How to rename all files in a folder google colab 重命名文件夹及其子文件夹的文件 - rename files of a folder and its folders children
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM