2

I'm newbie in stack overflow.

I need your help. Thank you for your reply in advance.

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.

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 is included in every filenames of YYMMDD type.

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

(folder what is made automatically as above I explain)

Could you help or explain how to do it?

Thank you very much again. Have a good day.

4
  • 1
    Your title says "move" but your body text says "copy" - which do you want to do? I can think of a way to do the former by (ab)using the perl-based rename command... Mar 29, 2020 at 14:34
  • Sorry. I want to copy. I modified the title.
    – FBMan
    Mar 29, 2020 at 16:57
  • Making folder is always former work than copying files.
    – FBMan
    Mar 29, 2020 at 16:59
  • I got help through another my article. unix.stackexchange.com/questions/578468/…
    – FBMan
    Apr 7, 2020 at 16:05

3 Answers 3

1

If I understand you correctly, you can try this with GNU grep an date:

shopt -s nullglob
for file in *.avi *.mp4; do 
  dat=$(echo "$file" | grep -Po "(?<=\.)[0-9]{6}(?=\.)")
  if [[ $(date "+%y%m%d" -d "$dat") ]]; then
    dir=$(date -d "$dat" "+%Y-%m-%d(%a)")
    cp "$file" "$dir"
  fi
done
1
  • I really appreciate for your response. Before I try to your suggestion, I have additional question. How can I write the path over there? the path means... 1. Path that folder containing video files (For example, /volume1/video/temp/) 2. Path that date folders (For example, /volume1/video/2020-03-31(Tue)/)
    – FBMan
    Mar 31, 2020 at 12:31
1

One possibility would be to use GNU Awk (gawk) which has built-in time manipulation functions. For example (assuming that if you have gawk, you also have GNU implementations of cp and mv)

printf '%s\n' *.mp4 *.avi | gawk -F. '
  {
    datespec = sprintf("20%02d %02d %02d 00 00 00", substr($2,1,2), substr($2,3,2), substr($2,5,2)); 
    cmd = sprintf("echo cp -t %s/ -- %s", strftime("%Y-%m-%d\\(%a\\)",mktime(datespec)), $0);
    system(cmd)
  }
'
cp -t 2020-03-29(Sun)/ -- EFGHIJKH.200329.1080p.mp4
cp -t 2020-03-28(Sat)/ -- ABCDABCD.200328.avi

Remove the echo once you are happy that it's doing the right thing. Change cp to mv if you want to move rather than copy.

Another option for the move case might be to (ab)use the Perl-based rename utility:

$ rename -n -e 'BEGIN{use Time::Piece}' -e 's@^[^.]*[.]([0-9]{6})[.].*$@Time::Piece->strptime($1,"%y%m%d")->strftime("%Y-%m-%d(%a)/") . $_@e' *.mp4 *.avi
rename(EFGHIJKH.200329.1080p.mp4, 2020-03-29(Sun)/EFGHIJKH.200329.1080p.mp4)
rename(ABCDABCD.200328.avi, 2020-03-28(Sat)/ABCDABCD.200328.avi)

Here, remove the -n after testing.

-1

if you cd to the folder where the video files are stored, then you should be able to move the files using the following command:

mv -v *YYMMDD* /path-to-new-folder

This uses a pattern match to move only the files that have that date format in the filename. I usually use the -v option, so the command prints out verbosely, so you know it has done something.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .