0

I looked for similar questions, but it was hard to get a clear answer in my case.

I use my synology DS1515+ and DSM 6.2.2.

First of all, I have been made daily folder containing a date using script as below;

mkdir /volume1/video/$(date +%Y-%m-%d\(%a\))

This script is executed at daily midnight. So every midnight, these folders are made.

(It means destination folder is always already existed before copying.)

And some video files are downloaded. Those filenames contain date.

For example, ABCDABCD.200328.avi or EFGHIJKH.200327.1080p.mp4

Filenames don't have certain rules but date like YYMMDD type is included in every filenames.

I'd like to copy these files to folders which is including same date.

(folder what is made automatically as above I explain)


[Location of directory]

Path of files what I wanna copy : /volume1/video/

Example of Destination directory : /volume1/video/daily/2020-04-06(Mon)


Could you help or explain how to do it?

If you explain to me, please include path of directory in my case as above.

(because I can't apply the code what you recommend to me for lack of my understanding. I apologize)

Thank you very much again. Have a good day.

  • There is always 6 digits in a row only once per filename? – pLumo Apr 7 at 12:46
1

Try this,

for f in /volume1/video/*; do
  # skip over directories
  [ -f "$f" ] || continue
  # grep the date in YYMMDD format
  date=$(printf '%s' "$f" | grep -Eo '[0-9]{6}')
  # set target path using date to convert YYMMDD to YYYY-MM-DD(%a)
  target="/volume1/video/daily/$(date -d "$date" +%Y-%m-%d\(%a\))/"
  # mv the file
  echo mv "$f" "$target"
done
  • Remove the echo when it's working.
  • Maybe it would be good to add a check to see if $date is not empty.
  • You could add mkdir -p "$target" before the mv to avoid errors on missing directories.
  • Note: this code has a millenium bug ;-)
| improve this answer | |
  • Wow! I really appreciate!! Thank you very much!!! – FBMan Apr 7 at 14:10
1

Just created a script also that I tested:

#!/bin/bash

SOURCE_FOLDER="video/"
TARGET_FOLDER="video/daily" # Not used as it is under the source folder.

doMove() {
    # Year loop assuming AFTER 2000 up to 2022
    cd "$SOURCE_FOLDER"
    for i in 20{19..20} ; do
        #echo -e "/nWorking on 20$1"
        # Month Loop
        for j in 0{1..9} {10..12} ; do
            # Day Loop
            for k in 0{1..9} {10..31} ; do
                # Find files matching YYMMDD
                file=$(find . -maxdepth 1 -type f -print | grep $i$j$k)
                # For each found file:
                for sf in $file ; do
                    # Find the folder with a different format
                    folder=$(find * -type d -print | grep $i-$j-$k)
                    echo -e "\nFile: $sf"
                    echo -e "Folder: $folder"
                    mv "$sf" "$folder/" # 2> /dev/null
                    echo -e "Moved $sf to $folder"
                done
            done
        done
    done
    cd .. # Change to your needs.
}
doMove
| improve this answer | |
  • Of course the script from pLumo is much more slick ... ;-) – MrVirtualT Apr 7 at 14:57
  • Thank you for your effort! – FBMan Apr 7 at 15:37

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.