user2175309
user2175309

Reputation: 1

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

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

Upvotes: 0

Views: 3747

Answers (3)

darxsys
darxsys

Reputation: 1570

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.

Upvotes: 1

dogbane
dogbane

Reputation: 274838

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

Upvotes: 0

akostadinov
akostadinov

Reputation: 18654

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.

Upvotes: 1

Related Questions