Split Filename Up to Define Variables

感情迁移 提交于 2019-12-13 04:47:13

问题


I have a script I created to help with converting a video then uploading it to our website. Our videos all have a standard format for their filename to help with setting them up correctly (day, month, year; i.e. 09OCT2013.m4v). They get filed into directories from year to month to day (i.e. 2013/oct/09OCT2013/09OCT2013.m4v). Right now, my script opens by asking for user input for the year then month then the actual file name for the folder. What I want to do is take the file that has already been created, drop it into the script, then have the script take it apart and put it in the appropriate file (i.e. drop the file 12JUN2012.m4v into the script and the script automatically puts it into 2012/jun/12JUN2012/). Is there any possible way to do this in terminal? Please let me know if any part of my question is unclear.


回答1:


Assuming that you're using bash:

for file in "$@"
do
     dd=${file:0:2}
     mm=${file:2:3}
     yy=${file:5:4}
     mv "$file" "$yy/$mm/$file"
done

If the file needs to be moved further, or is supplied with more pathname, you can adjust the script, but the basic idea of splitting the last component of the file name up using the substring notation is good.



来源:https://stackoverflow.com/questions/19340794/split-filename-up-to-define-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!