[英]Bash Script to move files based on their Prefix
I take pictures for my job on three different cameras.我用三台不同的相机为我的工作拍照。 I am looking to automate the process of organising these to folders based on the first 3 letters and then the filetype.
我希望根据前 3 个字母然后是文件类型自动执行将这些组织到文件夹的过程。
Unfortunately my bash script syntax knowledge is non existent and so I can't figure out more than creating the directories..不幸的是,我的 bash 脚本语法知识是不存在的,所以我除了创建目录之外无法弄清楚..
An eg of the incoming files:传入文件的示例:
HAM1234.JPG
HAM1234.RAW
HDR1234.JPG
HDR1234.RAW
STL1234.JPG
STL1234.RAW
These would go into 3 folders这些将 go 放入 3 个文件夹
HAM - REF/{RAW,JPG}
HDR - HDRI/{RAW,JPG}
STL - STILLS/{RAW,JPG}
With the filetypes being aligned.随着文件类型的对齐。
Any help would be much appreciated!任何帮助将非常感激!
Jason杰森
Please refer a very basic bash script based on your requirements and information you provided:请根据您的要求和您提供的信息参考一个非常基本的 bash 脚本:
#!/bin/bash
for files in *
do
if [ -f "$files" ]; then
fileprefix=${files%%[0-9]*.*}
case $fileprefix in
HAM)
if [ ! -d REF ]; then
mkdir REF;
fi
mv "$fileprefix"*.* REF
;;
HDR)
if [ ! -d HDRI ]; then
mkdir HDRI;
fi
mv "$fileprefix"*.* HDRI
;;
STL)
if [ ! -d STILLS ]; then
mkdir STILLS;
fi
mv "$fileprefix"*.* STILLS
;;
esac
fi
done
The first logic for files in *
assumes the script itself and all the image files resides in the same directory ie PWD. for files in *
的第一个逻辑假定脚本本身和所有图像文件都位于同一目录中,即 PWD。
if [ -f "$files" ]; then
Only run the logic if the matching item in the iteration is a file仅当迭代中的匹配项是文件时才运行逻辑
fileprefix=${files%%[0-9]*.*}
Extract the letters (first 3) and store it in variable fileprefix
提取字母(前 3 个)并将其存储在变量
fileprefix
中
After that a simple switch-case
follows.之后是一个简单的
switch-case
。
You can modify the script as per your needs.您可以根据需要修改脚本。
Hope this answers your question.希望这能回答您的问题。
Thanks Gaurav for the script.感谢 Gaurav 提供的脚本。 Editing it slightly I came out with this that seems to work.
稍微编辑一下,我想出了这个似乎可行的方法。 I wanted to be able to run it on a batch of folders at once for I added and extra loop and I ended up tweaking the folder structure.
我希望能够一次在一批文件夹上运行它,因为我添加了额外的循环,最后我调整了文件夹结构。 2 questions, 1)Am I losing anything by using the mkdir -p command and removing the if statement?
2 个问题,1) 使用 mkdir -p 命令并删除 if 语句是否会丢失任何内容? 2)What is the syntax behind ${files%%[0-9] .
2) ${files%%[0-9] 背后的语法是什么。 } I just can't see how that pulls the first three chars?
} 我只是看不出那是如何提取前三个字符的? :D
:D
#!/bin/bash
for f in "$@"; do
cd "$f"
for files in *
do
if [ -f "$files" ]; then
fileprefix=${files%%[0-9]*.*}
echo $fileprefix
case $fileprefix in
DSC)
mkdir -p "SetRef"
mv "$fileprefix"*.* SetRef
;;
HDR)
mkdir -p "HDRI/JPG" "HDRI/ARW";
mv "$fileprefix"*.JPG HDRI/JPG
mv "$fileprefix"*.ARW HDRI/ARW
;;
HAM)
mkdir -p "STILLS/JPG" "STILLS/ARW";
mv "$fileprefix"*.JPG STILLS/JPG
mv "$fileprefix"*.ARW STILLS/ARW
;;
esac
fi
done
done
With your shown examples:使用您显示的示例:
#!/bin/bash
mkdir -p {HAM\ -\ REF,HDR\ -\ HDRI,STL\ -\ STILLS}/{RAW,JPG}
shopt -s failglob
mv HAM*.JPG "HAM - REF/JPG"
mv HAM*.RAW "HAM - REF/RAW"
mv HDR*.JPG "HDR - HDRI/JPG"
mv HDR*.RAW "HDR - HDRI/RAW"
mv STL*.JPG "STL - STILLS/JPG"
mv STL*.RAW "STL - STILLS/RAW"
From man bash
:来自
man bash
:
failglob
: If set, patterns which fail to match filenames during pathname expansion result in an expansion error.failglob
:如果设置,则在路径名扩展期间无法匹配文件名的模式会导致扩展错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.