Code Help for copying files by (YEAR) to drive


Recommended Posts

I have all my files stored like such on my server for Media.

 

Transformers(2007)(1080p)

2012(2009)(720p)

Bolt(2008)(720p)

 

Every once in a while the misses will ask me to load up a portable USB drive so when we travel we can have a copy of our ripped movies for on the go. I literally have pre-ripped every movie we have using HandBrake down to 2Channel 720pish in quality

 Normally I'll erase the portable and whatever the year is I'll simply via windows type in the year for this example it would be (2017) and it will show all the movies from 2017 which are the newest. The problem is I'm getting lazier and somebody will always say why don't we have this or why don't we have that from last year or the year before that. I did this up out of laziness that sorta suits my needs.  

 

To explain the below. First Off I set up a specific location on a Drive so I didn't have to retype if over and over. 

Then I established the current year so when the script sorts through my files its currently looking for all (2018) then it moves to all the (2017) and then on to (2016) on and on for 5 years. I'd like to go farther and farther back, but its a lot of typing. 

 

Then when it finishes the 5 years it erases whatever didn't fit on the drive or any empty folders.  

 

#Grab newest movies based on year and pack drive

#Using Unassigned Devices fill in location
SOURCE=/mnt/disks/General_UDisk
DESTINATION=$SOURCE/Movies/

#Setup Destination Drive
mkdir $SOURCE/Movies/

#This Year
TODAY=$(date +"%Y")
cp -Rn $(ls -dt /mnt/user/Travel/*$TODAY*) $DESTINATION

#Previous Year
LAST1=$(date -d "-1 year" +"%Y")
cp -Rn $(ls -dt /mnt/user/Travel/*$LAST1*) $DESTINATION

#Previous Year
LAST2=$(date -d "-2 year" +"%Y")
cp -Rn $(ls -dt /mnt/user/Travel/*$LAST2*) $DESTINATION

#Previous Year
LAST3=$(date -d "-3 year" +"%Y")
cp -Rn $(ls -dt /mnt/user/Travel/*$LAST3*) $DESTINATION

#Previous Year
LAST4=$(date -d "-4 year" +"%Y")
cp -Rn $(ls -dt /mnt/user/Travel/*$LAST4*) $DESTINATION

#Previous Year
LAST5=$(date -d "-5 year" +"%Y")
cp -Rn $(ls -dt /mnt/user/Travel/*$LAST5*) $DESTINATION

#Find files that appear to not be complete and erase them
find $DESTINATION -type f -size -1000k -delete

#Find empty folders that didn't make it and delete them
find $DESTINATION -type d -empty -delete

 

The reason I'm sorting by year is When the misses says she wants to bring along the newest Movies she doesn't mean the newest to us, but the newest released movies. Meaning just because I Ripped a copy of Star Wars from 1977 yesterday, technically that movie is from 1977 not 2017. 

 

Anyways if anybody has a way to simply this please let me know. My goal is to pack a drive starting at (2018) then working all the way down until its full.  My way is crude, but it only covers 2018-2014 without repeating the same few lines of code over and over all the way down to 1970 or whatever. Of course I don't have enough space to go back that way, but......... :D

 

Link to comment
23 hours ago, bonienl said:

Those individual copy statements you can simply replace by a loop.


for YEAR in {2018..2014}; do
  cp -Rn $(ls -dt /mnt/user/Travel/*$YEAR*) $DESTINATION
done

 

 

Lol

 

I'll take a look at that. Seems way easier than my nightmare. 

 

So 2018..2014 basically is going to look through everything that is 2018-2104?

 

I found a site talking about. Thanks for the direction. ;-)

I knew I should of used a loop, but hey I was up late playing with this.

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.