我正在编写一个.sh
文件,我/parent_folder/
在 .sh的父文件夹中运行它我运行一个命令,该命令创建了一个名为的子文件夹My_Sub_Folder
,其中每个文件开头有数百个文件,Myfile_
然后是一个数字,然后是_
另一个数字,然后是许多其他字母,作为一个较小的例子,这些是一些文件名:
Myfile_1_1_3423423_MY.my
Myfile_1_2_3242343_MY.my
Myfile_1_3_5645654_MY.my
Myfile_2_1_3242354_MY.my
Myfile_2_2_4534535_MY.my
Myfile_2_3_2365464_MY.my
Myfile_3_1_5464567_MY.my
Myfile_3_2_4364766_MY.my
Myfile_3_3_4564564_MY.my
因此,如图所示,所有文件都Myfile_
以数字递增计数开始,然后是_
另一个计数递增数字,所以我想要做的是在里面创建子文件夹,My_sub_folder
因此在上面的示例中,它下面将有三个子文件夹Myfile_1
,Myfile_2
并且所有文件都从Myfile_3
哪里Myfile_1
开始与Myfile_1_*
等等。关于如何做到这一点的任何建议?
这是我的脚本:
#!/bin/sh
myscript2.sh
DIR=$(pwd)
${DIR}/My_sub_folder
###I am not sure how I can then loop on the files and create the needed folders depending on their names as explained above then move them to the corresponding folder
像这样的东西,
#!/bin/sh
#
for file in My_Sub_Folder/*.my # Files end with ".my" suffix
do
dir="${file%_*_*_*.my}" # Strip the pattern off the end of the filename
mkdir -p "$dir" # Create the directory unless it already exists
mv "$file" "$dir/" # Move the file to the directory
done
然后,您可以使文件可执行(在这种情况下,我称之为thescript
)
chmod a+x thescript
然后根据需要多次运行它
./thescript
要查看发生了什么,请插入以 开头的额外行echo
。例如,
echo "Creating directory: mkdir '$dir'"
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句