Example scenario:

I've multiple files ending with .torrent and I would like to move them into folders which has the same name.

For example given the following files:

abc.torrent
def.torrent
some files has spaces.torrent
or [special] (characters).torrent
or . (dots).torrent
xyz.torrent

I would like to move them into its corresponding folders (if exists) which has the exactly same name, for example:

  • abc.torrent goes into abc/
  • def.torrent goes into def/
  • and so on

If the folder doesn't exist, file should stay untouched (as it may relate to incomplete download).

I believe BitTorrent doesn't have such options. If so, I would like to find some command line to achieve that.

Optionally I would like to specify the source folder of my files, and destination folder with expected directories.

Any ideas?

  • @terdon If directory doesn't exist, ignore the file and move on, since files could be related to incomplete downloads. – kenorb Dec 21 '15 at 23:30
  • Oh well, I gave both options. – terdon Dec 21 '15 at 23:42
up vote 3 down vote accepted
  • Non-recursively:

    for t in *torrent; do [ -d "${t%.*}" ] && mv "$t" "${t%.*}"; done 
    

    The ${var%pattern} syntax removes the shortest match of pattern from the end of a variable. In this case, it simply removes the extension.

  • Recursively (using bash):

    shopt -s globstar
    for t in **/*torrent; do [ -d "${t%.*}" ] && mv "$t" "${t%.*}"; done
    

    The globstar option makes ** match any files and 0 or more directories. Note that the above expects the target directory to be a subdirectory of the torrent file's parent directory. So, if a file is at foo/bar/baz.torrent, it will be moved to foo/bar/baz/baz.torrent.

  • Non-recursively, creating the directory if absent:

    for t in *torrent; do mkdir -p "${t%.*}"; mv "$t" "${t%.*}"; done 
    

    The -p option tells mkdir to print no error if the directory already exists and to create parent directories as needed. So, mkdir -p foo/bar/baz will also create the foo and bar directories if they don't exist.

  • Recursively, creating the directory if absent:

    shopt -s globstar
    for t in **/*torrent; do mkdir -p "${t%.*}"; mv "$t" "${t%.*}"; done
    
  • For the 'creating directory if absent' part, I think you can use $_ in the mv command as mkdir seems to update it regardless if the directory exists or not. – h.j.k. Dec 22 '15 at 1:59
  • **/*torrent version generates paths, so -d check agains them (not name it-self), here is the version which using base names and move into /foo folder: for t in **/*torrent; do [ -d /foo/"$(basename "${t%.*}")" ] && mv -v "$t" /foo/"$(basename "${t%.*}")/"; done – kenorb Jan 19 '16 at 23:22

Since I couldn't find such option on OS X version of BitTorrent, I've wrote the following one-liner:

find . -name "*.torrent" -exec sh -c 'DST=$(find . -type d -name "$(basename "{}" .torrent)" -print -quit); [ -d "$DST" ] && mv -v "{}" "$DST/"' ';'

This will look for the .torrent files in the current directory (.) and find the folders (DST) with the same name recursively found in the same folder (.). To check for different directories, change the dots (.) to appropriate destinations. See complete script: mv_torrents.sh.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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