linux - matching files to folders

07
2014-07
  • Ali

    Can linux scan two directories? Once scanned match the two directories based on similarities in the file name and transfer files.

    I'm looking to transfer file "directory"/seq0001.pdf to folder "different directory"/001 and "directory"/seq0002.pdf to folder "different directory"/002 e.t.c

    When newly incremented file number is created I would like to move that new file to the new directory.

    The new folder doesn't need to be made. As when there is a new file some other job is putting information in this folder and creates the new folder.

    I know how to do a straight move function but that would only work for the specified files. Can you move the files by a match rather than having to manually move the files every time?

  • Answers
  • Jason C

    There might be a way to shorten this (actually, there are a ton of good alternatives for parsing the file name; check out this question - I will update here as soon as I get a chance), but one way is to first create a script that handles a single file. For example, save the following as move.sh:

    #!/bin/sh
    subdir=`echo -n "${1%.*}" | tail -c3`
    mv -v "$1" "/path/to/$subdir"
    

    Adjust to taste. That will take take the part of the filename before the last "." and use the rightmost three characters as the target directory (e.g. "seq0002.pdf" goes to "/path/to/002").

    The chmod 755 move.sh to make it executable.

    I recommend putting that script in a different directory so it doesn't interfere. Then you can use e.g. find to execute it on all files, or just run it on one file at a time, e.g.:

    find . -maxdepth 1 -type f -exec /wherever/move.sh {} \;
    

    Or for one file (and assuming move.sh is in current directory):

    ./move.sh seq0001.pdf
    

    If you want to choose more complex paths you can extend the script, e.g.:

    #!/bin/sh
    subdir=`echo -n "${1%.*}" | tail -c3`
    
    if [ $subdir = "001" ] ; then
        rootdir="/directory";
    elif [ $subdir = "002" ] ; then
        rootdir="/another/place";
    else
        rootdir="/default/root";
    fi
    
    mv -v "$1" "$rootdir/$subdir"
    

    Again adjusting to taste. The possibilities are fairly limitless.


  • Related Question

    linux - Remove duplicate files across separate directory trees
  • P-Nuts

    I have two directories: old/ and new/. Some of the files in old/ are duplicates of those in new/. Some merely have the same name, and a few may even have identical content but different names. I'm not interested in the subdirectory path to the files, so only an unqualified filename needs to be considered in the matching.

    I want to remove the duplicate files from old/, so that it only contains files not also found in new/. For cases where the filenames match, but not the contents, or vice-versa, I want to have this listed in some log.

    Has anyone encountered a similar problem, or have a better solution than hacking together a (perl or similar) script from scratch? A runtime dominated by calling md5sum on each file is acceptable.


  • Related Answers
  • 8088

    Of course! Check out FSlint:

    FSlint is a utility to find and clean various forms of lint on a filesystem. I.E. unwanted or problematic cruft in your files or file names. For example, one form of lint it finds is duplicate files. It has both GUI and command line modes.

    alt text

    FSlint is free and open source software.

  • Cristian Ciupitu

    fdupes - finds duplicate files in a given set of directories

    fdupes new/ old/
    

    -d for delete

    -N for don't ask

    fdupes -N -d new/ old/