3

So I have a directory that has 77 subfolders in it on my Ubuntu machine, each with various amounts of torrent files in them. Total of 427 torrents are contained in the subfolders. I want to move all of them from the subfolders into the parent directory.

The catch? Some of the files are named the exact same. When moved, it doesn't really matter what they're renamed to but the easiest way to fix it would be abc.torrent ---> abc1.torrent, abc2.torrent, and so on if there are more.

I found the following code to extract all the files to the parent directory:

find . -mindepth 2 -type f -print -exec mv {} . \;

and I understand I can use --backup to fix the files that have the same name. So I added this to the code:

find . -mindepth 2 -type f -print -exec mv --backup=numbered {} . \;

But when I run it, I only end up getting 167 files moved instead of the 427.

I also know I can use -i to ask me individually about each one, but that would take quite a long time. Is there anyway to automate this?

  • 1
    Is there anything special about the files (their names) that were moved or the ones that weren't? – Kusalananda Jan 29 '18 at 10:47
-1

Your second find should work as far as my understanding goes - if you pipe that find into wc -l does it return 427 as expected?

Here's a quick bash script that should do the same however:

#!/bin/bash

for torrent in $(find /path/to/target/dir -mindepth 2 -type f); do
    count=0
    # get filename and remove any spaces
    filename=$(echo $torrent | tr -d ' ' | awk -F '/' '{print $NF}')
    # if it exists in the current dir, name it $count-$filename
    while [ -f /path/to/target/dir/$filename ]; do
        ((count++))
        filename=$count-$filename
    done
    # do the move
    mv "$torrent" /path/to/target/dir/$filename
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.