Bash Script to sort video files to correct folders
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Bash Script to sort video files to correct folders
I have a little Bash script set up to find video files then move them to a certain folder depending on size. It works fine. An extract of the script I used is below:
I then have a similar script to sort video files smaller than 2gig. I thought it would be more elegant, especially as more conditions are added, to break it down using boolean logic as outlined below
If file ends in .mkv or .mp4 or .avi then
If file larger than 2 gig then move to long
If file smaller than 2 gig then move to short
However I'm unsure how this would look in a script. Can I do something like:
Code:
if /home/tim/readynas/Tim/Download/Downloaded/ -type f \( -iname \*.mkv -o -iname \*.avi -o -iname \*.wmv -o -iname \*.mp4 -o -iname \*.m4v \) then
if -size +2G -exec mv -f {} /home/tim/readynas/Videos/Longform/ \;
if -size -2G -exec mv -f {} /home/tim/readynas/Videos/Shortform/ \;
else
endif
for i in $( /home/tim/readynas/Tim/Download/Downloaded/ -type f \( -iname \*.mkv -o -iname \*.avi -o -iname \*.wmv -o -iname \*.mp4 -o -iname \*.m4v \); do
Many thanks for the code. I'm attempting to wrap my head around it.
Does the first line create a string called 'i' that contains all files ending with desired extension?
The next line then lists 'i' then pipes to list again?
Whats the print $5 do?
It could be more elegant visually (?) but not efficiently wise than using 2 lines of find command maybe with xargs to move all found filenames in one go (for each line)
This should print the name of all the file you intend do find.
Code:
for i in $(find /home/tim/readynas/Tim/Download/Downloaded/ -type f \( -iname \*.mkv -o -iname \*.avi -o -iname \*.wmv -o -iname \*.mp4 -o -iname \*.m4v \); do
echo $i
done
do this
Code:
ls -l <some dir eg /tmp>
to find file size in bytes do. Size is # five in the out put.
So we print it out.
Code:
ls -l <some dir eg /tmp> | awk '{print $5}'
but when we do this
Code:
$(ls -l <some FILENAME> | awk '{print $5}')
we take that size, position # five, in that variable , so we can use it.
for example do this
Code:
date
echo today is $(date)
That's great information. I'm slowly figuring it all out.
When I attempt to run the script I get:
/home/tim/bin/download_clean_sort.sh: line 30: unexpected EOF while looking for matching `)'
/home/tim/bin/download_clean_sort.sh: line 64: syntax error: unexpected end of file
I thought that by adding a second ) just before the ; would fix it but I then get more errors. Where should that second ) go?
linux-6e72:~ # ls -l /etc/init.d/*\.sh
-rwxr--r-- 1 root root 7774 Sep 1 10:34 /etc/init.d/mw_APCH.sh
-rwxr--r-- 1 root root 551 Sep 1 10:12 /etc/init.d/test.sh
[/QUOTE]
[QUOTE]
linux-6e72:~ # for i in $(find /etc/init.d -type f \( -iname \*.mkv -o -iname \*.avi -o -iname \*.sh \) ) ; do if [[ $( ls -l $i | awk '{print $5}') -gt 4000 ]]; then echo $i large ; elif [[ $( ls -l $i | awk '{print $5}') -lt 4000 ]]; then echo $i small ; fi ; done
/etc/init.d/mw_APCH.sh large
/etc/init.d/test.sh small
linux-6e72:~ #
it works. now lets break it down carefully.
Code:
#!/bin/bash
for i in $(find /etc/init.d -type f \( -iname \*.mkv -o -iname \*.avi -o -iname \*.sh \) )
do
if [[ $( ls -l $i | awk '{print $5}') -gt 4000 ]]; then
echo $i large
elif [[ $( ls -l $i | awk '{print $5}') -lt 4000 ]]; then
echo $i small
fi
done
please mind the space between "4000 ]]" or "[[ $("
linux-6e72:~ # for i in $(find /etc/init.d -type f \( -iname \*.mkv -o -iname \*.avi -o -iname \*.sh \) ) ; do if [[ $( ls -l $i | awk '{print $5}') -gt 4000 ]]; then echo $i large ; elif [[ $( ls -l $i | awk '{print $5}') -lt 4000 ]]; then echo $i small ; fi ; done
/etc/init.d/mw_APCH.sh large
/etc/init.d/test.sh small
linux-6e72:~ #
Quote:
it works. now lets break it down carefully.
Code:
#!/bin/bash
for i in $(find /etc/init.d -type f \( -iname \*.mkv -o -iname \*.avi -o -iname \*.sh \) )
do
if [[ $( ls -l $i | awk '{print $5}') -gt 4000 ]]; then
echo $i large
elif [[ $( ls -l $i | awk '{print $5}') -lt 4000 ]]; then
echo $i small
fi
done
please mind the space between "4000 ]]" or "[[ $("
Loving your code. Many thanks indeed for all the help, learned a lot.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.