BASH find Question

Mark Phillips mark at phillipsmarketing.biz
Sun Oct 15 09:29:51 MST 2017


Alex,

I think I am too lazy to find the patterns...;)

Anyway, I managed to solve the original problem a different way. The issue
that started me on this journey is that some of the .mkv files had been
converted, and my original one liner was failing:

find /media/plex/ -name "*.mkv" -exec ffmpeg -i {}  -vcodec copy -acodec
copy {}.mp4 > fix-mkv.txt 2>&1 \;

That is why I went to a multi-line script to find mkv files that had a
corresponding mp4 file, and not convert those. Which brought me to the
limitation on find looking for files with special characters. It seems find
will 'find' them if the special characters are included in a wildcard, but
won't find them unless they are escaped.

I looked through the ffmpeg man page and found that a "-y" option, which
tells ffmpeg to always overwrite an existing file. My plex server has
nothing better to do than consume electricity, so it can convert mkv files
all day and night and overwrite already converted files without disturbing
anyone. My new one liner:

find /media/plex/ -name "*.mkv" -exec ffmpeg -i {} -y -vcodec copy -acodec
copy {}.mp4 > fix-mkv.txt 2>&1 \;

Once it finishes and there are no errors in the fix-mkv.txt file, I will

find /media/plex/ -name "*.mkv" -exec rm {} \;

and all the obnoxious mkv files will be gone with shiny new mp4 files in
their place!

Thanks for the offer to help! I know I can spend hours crafting regex
search patterns, and this issue really did not need that level of
sophistication. In this case, a blunt hammer worked just as well as a sharp
knife!

Mark

On Sun, Oct 15, 2017 at 9:01 AM, Snyder, Alexander J <
alex at misteralexander.com> wrote:

> Sure there is! There is never a pattern *until we want a pattern*, then
> you'd be surprised at what pops up!
>
> You might find that you need to process in groups (*ie: movies that have
> "(YYYY)" in the name and others that don't*).
>
> Try this:
>
> find /media/plex/ -type f -name "*.mkv" -exec bash -c "if [[ \"{}\" =~
> (.*)(\[x264\])(.*) ]]; then echo \"{}\"; fi" \;
>
> That should list all of your "x264" encoded movies, including your Disney
> movies used as an example above.
>
> Feel free to ping me directly, so we can walk this out, step-by-step.
>
> Interested to know the results. Let me know.
>
> -Alex.
>
>
>
> --
> Thanks,
> Alex.
>
>
>
> On Sun, Oct 15, 2017 at 8:25 AM, Mark Phillips <mark at phillipsmarketing.biz
> > wrote:
>
>> Alex,
>>
>> Thanks for the idea. However, there really aren't any common patterns in
>> the 120 targets that I can match a regex to.
>>
>> Mark
>>
>> On Sun, Oct 15, 2017 at 8:20 AM, Snyder, Alexander J <
>> alex at misteralexander.com> wrote:
>>
>>> I did something very similar (BASH/find/ Plex) just the other day! The
>>> solution Aaron gave will fix it:
>>>
>>> "{}" instead of '{}'.
>>>
>>> May I also suggest matching the output to a regex pattern. That helps me
>>> sort with great precision:
>>>
>>> ..... -exec bash -c "if [[ "{}" =~ (my)([pattern]{3}) ]]; then doThis;
>>> fi;" \;
>>>
>>> Let me know how it goes!
>>>
>>> Thanks,
>>> Alex.
>>>
>>> Sent from my Samsung Galaxy S8+
>>>
>>> On Oct 15, 2017 04:08, "Aaron Jones" <retro64xyz at gmail.com> wrote:
>>>
>>>> Im just laying in bed, but try double quotes instead of singles and
>>>> then paste me the exact error if you get one.
>>>>
>>>> On Oct 14, 2017, at 10:15 PM, Mark Phillips <mark at phillipsmarketing.biz>
>>>> wrote:
>>>>
>>>> I found a few (~120) .mkv files crept into my plex movie drive. Plex on
>>>> my limited plex server has trouble transcoding these files in real time, so
>>>> I need to transcode them into mp4s ahead of time.
>>>>
>>>> I tried this script, but I have an problem with the second find command.
>>>>
>>>> find /media/plex/ -name '*.mkv' | while read line; do
>>>>     echo "Processing file '$line'"
>>>>     f=`basename "$line" .mkv`
>>>>     echo "basename = $f"
>>>>     if [ -n "$(find /media/plex/ -name '$f.mp4' )" ]; then
>>>>         #ffmpeg -i {} -vcodec copy -acodec copy $f.mp4 > fix2-mkv.txt
>>>> 2>&1
>>>>         #echo {} > files_fixed.txt 2>$1
>>>>         echo "**************need to convert $line"
>>>>     else
>>>>         echo "$f.mp4 already exists"
>>>>     fi
>>>> done
>>>>
>>>> All of the file names contain special characters, eg '[', and others.
>>>>
>>>> For example,
>>>> /media/plex/Movies/Disney (Classic)/Robin Hood (1973 Movie)
>>>> [x264-AAC][DVD][C-W].mkv
>>>>
>>>> I get a false negative on this file when I search for the corresponding
>>>> mp4 file because of the '[' in the file name.
>>>>
>>>> This works:
>>>> find /media/plex -name 'Robin Hood (1973 Movie)*'
>>>> /media/plex/Movies/Disney (Classic)/Robin Hood (1973 Movie)
>>>> [x264-AAC][DVD][C-W].mkv
>>>>
>>>> but this gives a false negative
>>>> find /media/plex -name 'Robin Hood (1973 Movie) [x264-AAC]*'
>>>> root at orca:/home/mark#
>>>>
>>>> I searched the find man page for ways to handle the special characters,
>>>> but no luck on finding a solution. Anyone have a suggestion?
>>>>
>>>> Thanks!
>>>>
>>>> Mark
>>>>
>>>> ---------------------------------------------------
>>>> PLUG-discuss mailing list - PLUG-discuss at lists.phxlinux.org
>>>> To subscribe, unsubscribe, or to change your mail settings:
>>>> http://lists.phxlinux.org/mailman/listinfo/plug-discuss
>>>>
>>>>
>>>> ---------------------------------------------------
>>>> PLUG-discuss mailing list - PLUG-discuss at lists.phxlinux.org
>>>> To subscribe, unsubscribe, or to change your mail settings:
>>>> http://lists.phxlinux.org/mailman/listinfo/plug-discuss
>>>>
>>>
>>> ---------------------------------------------------
>>> PLUG-discuss mailing list - PLUG-discuss at lists.phxlinux.org
>>> To subscribe, unsubscribe, or to change your mail settings:
>>> http://lists.phxlinux.org/mailman/listinfo/plug-discuss
>>>
>>
>>
>> ---------------------------------------------------
>> PLUG-discuss mailing list - PLUG-discuss at lists.phxlinux.org
>> To subscribe, unsubscribe, or to change your mail settings:
>> http://lists.phxlinux.org/mailman/listinfo/plug-discuss
>>
>
>
> ---------------------------------------------------
> PLUG-discuss mailing list - PLUG-discuss at lists.phxlinux.org
> To subscribe, unsubscribe, or to change your mail settings:
> http://lists.phxlinux.org/mailman/listinfo/plug-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.phxlinux.org/pipermail/plug-discuss/attachments/20171015/8657e87d/attachment.html>


More information about the PLUG-discuss mailing list