Move files to directories based on some part of file name?

与世无争的帅哥 提交于 2019-12-25 01:47:14

问题


I have several thousand eBooks named like AuthorFirstName AuthorLastName Title XX.pdf, where xx are number from 1-99 (volume number).

Author tilte name can be of multiple word so here i want to move copy the files to folder with the name AuthorFirstName AuthorLastName title. Everything except the number should be the folder name, so that all volumes of the eBook come in same folder.

For example

root.....>AuthorFirstName AuthorLastName Title>AuthorFirstName AuthorLastName Title XX.pdf

回答1:


You can use a mix of find, sed and bash script for the task. You have to write it on your own though and ask for help if you fail.

You can also try some ready tools for mass moving/renaming like these: http://tldp.org/LDP/GNU-Linux-Tools-Summary/html/mass-rename.html Never used one of these though.




回答2:


I would try with this:

for folder in $(ls | sed -r "s/(.*) ([0-9]{1,2})/\1/" | uniq)
do 
    mkdir $folder
    mv $(find . -name "$folder*") $folder
done    

I don't know if this is correct, but it may give you some hints.

edit: added uniq to the pipe.




回答3:


Use a loop as shown below:

find . -type f -name "*pdf" -print0 | while IFS= read -d '' file
do
    # extract the name of the directory to create
    dirName="${file% *}"

    # create the directory if it doesn't exist
    [[ ! -d "$dirName" ]] && mkdir "$dirName"

    mv "$file" "$dirName"
done


来源:https://stackoverflow.com/questions/15782766/move-files-to-directories-based-on-some-part-of-file-name

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