Sorting script

Need help with script i have about 40TB of a files *.mov
an need to sort them to folders
i did managed to create some kind of a script to sort new files according to date.

#! /bin/bash
cd /Volumes/xsan/ARHIVA/SNIMANJA/
mkdir $(date +%Y) #godina
cd $(date +%Y)
mkdir $(date +%B)
cd $(date +%B)
mkdir $(date +%d)
mv /Volumes/xsan/ARHIVA/SNIMANJA/XDCAM_IN/[^"InProgres"]* /Volumes/xsan/ARHIVA/SNIMANJA/$(date +%Y)/$(date +%B)/$(date +%d)

Now i need to do that with all files from /Volumes/xsan/old
to /Volumes/xsan/new
so if i have file 85000.mov in folder "old" and that file is created 2013.05.13. i want to to be moved to /Volumes/xsan/new/2013/05/13

so from this
/Volumes/xsan/old/85000.mov
mv
/Volumes/xsan/new/2013/05/13/85000.mov

find /Volumes/xsan/ARHIVA/SNIMANJA/XDCAM_IN/ -type f -printf "%CY/%Cm/%Cd\t%p\n"  |
while read -r DATE FILE
do
        mkdir -p /path/to/"$DATE"
        echo mv "$FILE" /path/to/"$DATE"
done

Remove the echo once you've tested it and are sure it does what you want.

I'm not quite sure what you're doing with that "[^"InProgres"]*", explain and I can add the filter for that.

1 Like

Not working says:

find: -printf: unknown primary or operator

and [^"InProgres"] is to skip files that have "InProgres" in name.
Thanks

---------- Post updated at 08:23 AM ---------- Previous update was at 03:32 AM ----------

i forgot to say that i am using OS X 10.9.
I have found that find those not work on osx and that i should use awk.
Thanks in advance

does the file names have YYYY-MM-DD on them?? Or the directory has only files for one day?

Ok, i have installed macports and findutils and there is no error, but all files are moved in /2014/07/09
and they are not created on that day.

script need to create folder from creation day of a specific file

created filename new location
6/28/14 85729.mov -> /2014/06/28/85729.mov
7/1/14 85764.mov -> /2014/07/01/85764.mov
7/2/14 85797.mov -> /2014/07/02/85797.mov

---------- Post updated at 09:05 AM ---------- Previous update was at 09:04 AM ----------

Files does have YYY-MM-DD

---------- Post updated at 10:14 AM ---------- Previous update was at 09:05 AM ----------

I have managed to do it myself thank you all
here is if someone will need it

#!/bin/bash
srcdir="/source/dir"
destdir="/destination/dir"

cd $srcdir

touch /tmp/sort.txt
ls -Tl *.mp4 > /tmp/sort.txt

while read line
do
    filename=$(echo $line | awk '{print $10}')
    fileyear=$(echo $line | awk '{print $9}')
	filemont=$(echo $line | awk '{print $6}')
    filedate=$(echo $line | awk '{print $7}')
	filedir=${filedate:0:7}
	filedirm=${filemont:0:7}
	filediry=${fileyear:0:7}
    if [ ! -d $destdir/$fileyear/$filedirm/$filedir ]; then
        mkdir -p $destdir/$fileyear/$filedirm/$filedir
    fi
    # move files 
     if [ ! -f $destdir/$fileyear/$filedirm/$filedir/$fiename ]; then
         mv $filename $destdir/$fileyear/$filedirm/$filedir
     fi


done < /tmp/sort.txt
rm /tmp/sort.txt

---------- Post updated at 10:31 AM ---------- Previous update was at 10:14 AM ----------

And now i need a little help with file_name
if file name have space "file Name.mov" than it is not '{print $10}' than "file is 10" and "Name is 11" how can I combine that?

use $NF --> Last field of the processing line

1 Like

But this is only for last word in line
so for example if the file is called
brown lazy fox jumped_over.mov
$NF

mv: rename jumped_over.mov to /Volumes/xsan/ARHIVA/SNIMANJA/2014/test/2/2014/Apr/14/jumped_over.mov: No such file or directory

I have tried and $10 $NF
but in that case

mv: rename brownjumped_over.mov to /Volumes/xsan/ARHIVA/SNIMANJA/2014/test/2/2014/Apr/14/brownjumped_over.mov: No such file or directory

Tried {print $10,$11,$12,$13,$14,$15}') and this when use echo is "working"

mv brown lazy fox jumped_over.mov /Volumes/xsan/ARHIVA/SNIMANJA/2014/test/2/2014/Apr/14
mv jumped_over.mov /Volumes/xsan/ARHIVA/SNIMANJA/2014/test/2/2014/Jul/11

but when trie without echo it looks like this

mv: rename brown to /Volumes/xsan/ARHIVA/SNIMANJA/2014/test/2/2014/Apr/14/brown: No such file or directory
mv: rename lazy to /Volumes/xsan/ARHIVA/SNIMANJA/2014/test/2/2014/Apr/14/lazy: No such file or directory
mv: rename fox to /Volumes/xsan/ARHIVA/SNIMANJA/2014/test/2/2014/Apr/14/fox: No such file or directory
mv: rename jumped_over.mov to /Volumes/xsan/ARHIVA/SNIMANJA/2014/test/2/2014/Jul/11/jumped_over.mov: No such file or directory

I don't know how you end up trying to move files with names ending in .mov when you're listing files with names ending in .mp4 ???

But, I'm guessing that this much simpler (and faster) version of your script will be much closer to what you're trying to do (working on both .mp4 and .mov files):

#!/bin/bash
srcdir="/source/dir"
destdir="/destination/dir"

cd "$srcdir"
ls -Tl *.mp4 *.mov | while read -r x x x x x filedirm filedir x fileyear filename
do
	if [ ! -d "$destdir/$fileyear/$filedirm/$filedir" ]
	then	mkdir -p "$destdir/$fileyear/$filedirm/$filedir"
	fi
	# move files 
	if [ ! -f "$destdir/$fileyear/$filedirm/$filedir/$fiename" ]
	then	mv "$filename" "$destdir/$fileyear/$filedirm/$filedir"
	fi
done

The key point is that when you have filenames containing whitespace characters, you have to quote them appropriately. Getting rid of all of the calls to awk and the problems created by trying to use awk to gather an unknown number of fields into the filename is handled by letting the read shell built-in do most of the work for you.

1 Like

I have end up with mp4 because i have created test folder and copied mp4 files. mp4 is smaller few MB and mov are few GB and for testing is ok.
I will try your script and let you know if that is ok.
Thank you very much for your effort.

---------- Post updated at 05:29 PM ---------- Previous update was at 04:36 PM ----------

That is exactly what i needed Thanks again. :b:
I just adapted to my needs because this script will use people that are not familiar with shell.

so if anyone need it here it is

#!/bin/bash

#source
echo -n "drag&drop source folder:"
read -e srcdir
#srcdir="/src/dir"

#destination
echo -n "drag&drop destination folder:"
read -e destdir
#destdir="/dst/dir

#File type
echo -n "file type:"
read -e ftype

cd "$srcdir"
ls -Tl *.$ftype | while read -r x x x x x filedirm filedir x fileyear filename
do
	if [ ! -d "$destdir/$fileyear/$filedirm/$filedir" ]
	then	mkdir -p "$destdir/$fileyear/$filedirm/$filedir"
	fi
	# move files 
	if [ ! -f "$destdir/$fileyear/$filedirm/$filedir/$fiename" ]
	then	mv "$filename" "$destdir/$fileyear/$filedirm/$filedir"
	fi
done