Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: help script in bash with find

  1. #1

    Default help script in bash with find

    Hy, on my leap 15.0 the problem is:
    I have many files that have .avi extension but they are matroska files and should have .mkv extension, I never noted this becouse using smplayer it seems that extension is not important, it opens and play flawlessly any file, when I transfere these files in other apparatuses it doesn't works, the matroska files files must have the .mkv extension.
    I would have a script that change the extension of all matroska files in a folder with .avi extension to .mkv extension.
    I reached this script that works but on all the avi files:
    Code:
    #!/bin/bash#here under /where/the/files/are
    cd /home/procuste/Downloads/prove/
    find . -name '*.avi' \
    -exec mmv {} {}.mkv \;
    I found that using mediainfo I can get if the file is matroska in this way:
    Code:
    mediainfo "name-of-the-file" | grep "Matroska"
    I tried to add this option to the working script but I didn't succeed, the less error version is this:
    Code:
    #!/bin/bash#here under /where/the/files/are
    cd /home/procuste/Downloads/prove/
    find . -name '*.avi' | 'mediainfo | grep "Matroska"' \
    -exec mmv {} {}.mkv \;
    could some saint guy gives me a suggestion to solve this problem correcting this script or in another way??
    manythanks, ciao :-)

  2. #2
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,288

    Default Re: help script in bash with find

    Well, sorry to say, but your last script isn't rea;y smething.

    But going back, you forgot to tell what
    Code:
    mediainfo "name-of-the-file" | grep "Matroska"
    produces as output. And you try to use that output in your script. Bit difficult to give comment.

    I can understand what
    Code:
    find . -name '*.avi' -exec mmv {} {}.mkv \;
    does. In short it finds, starting from the working directory, looking for filenames that end in .avi. When it finds one, let us say e.g. tune.avi, it then executes the command
    Code:
    mmv tune.avi tune.avi.mkv
    Is that would you also think it does?

    Now to the second command line
    Code:
    find . -name '*.avi' | 'mediainfo | grep "Matroska"' -exec mmv {} {}.mkv \;
    These are two commands connected through a pipe. The first one is
    Code:
    find . -name '*.avi'
    This wil find the same files as earlier, but because there is no further thing to do for find, it will output the file names found.
    Those file names will then go into the pipe and be fed as input to the next command:
    Code:
    'mediainfo | grep "Matroska"' -exec mmv {} {}.mkv \;
    Mind the quotes there '...'. They wil make
    Code:
    'mediainfo | grep "Matroska"'
    one "word" (the spaces being included because of the quoting) and that, in the end is NOT a command that can be found.

    Then if we remove the quoting, we will have again two statements with a pipe in between.

    First the simple
    Code:
    mediainfo
    will then get fed the file names from the pipe, which it then should read from stdin. And that is different from your earlier usage of mediainfo, when mediainfo does not read from stdin, but gets one file name fed as an argument. Thus I doubt this way of using mediainfo will do what you hope.

    The next command after the second pipe
    Code:
    grep "Matroska"' -exec mmv {} {}.mkv \;
    Where the grep "Matroska" part can be understood, but the rest is not something that will be handled by grep in a way I (and you) will understand. Syntacticaly it looks like it is part of a find statement, but there is no find statement anymore.

    Again, helping you with creating something that is based on the use of mediainfo, we need to know what mediainfo does.
    Henk van Velden

  3. #3
    Join Date
    Aug 2010
    Location
    Chicago suburbs
    Posts
    12,815
    Blog Entries
    3

    Default Re: help script in bash with find

    I suggest you start by designing a script that works for a single file, with name given on the command line. That way you can use "grep" in a test to determine whether to rename this file.

    Once you have a script that works for a single file, you can then come up with a "find" command that calls your script on each file found.
    openSUSE Leap 15.1; KDE Plasma 5;
    testing Leap 15.2Alpha

  4. #4

    Default Re: help script in bash with find

    manythanks hcvv :-)
    Quote Originally Posted by hcvv View Post
    Well, sorry to say, but your last script isn't rea;y smething.

    But going back, you forgot to tell what
    Code:
    mediainfo "name-of-the-file" | grep "Matroska"
    produces as output. And you try to use that output in your script. Bit difficult to give comment.
    the output of mediainfo if the file is a matroska file is:
    Code:
    io@server3TW:~/Downloads/prove> mediainfo mmm.avi | grep "Matroska"
    Format                                   : Matroska
    io@server3TW:~/Downloads/prove> 
    
    when the file is not a matroska file is:
    Code:
    procuste@server3TW:~/Downloads/prove> mediainfo aaa.avi | grep "Matroska"    
    procuste@server3TW:~/Downloads/prove> 
    
    so I would like to use this difference in output to get only the matroska files that ends with .avi

  5. #5

    Default Re: help script in bash with find

    Quote Originally Posted by nrickert View Post
    I suggest you start by designing a script that works for a single file, with name given on the command line. That way you can use "grep" in a test to determine whether to rename this file.

    Once you have a script that works for a single file, you can then come up with a "find" command that calls your script on each file found.
    manythanks nick, I will try also this way :-)

  6. #6
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,288

    Default Re: help script in bash with find

    I would use the feature that grep gives a return code on found/not found.
    Code:
    grep -q "Matroska" tune.avi && mmv tune.avi tune.avi.mkv
    So try this first on a file that is Matroska and one that isn't to see if it works as intended (method nrickert).

    Then to call this from a find, there are two ways.

    The most simple to uderstand what you are doing is making a script from the above statement, call it e.g. convert:
    Code:
    #!/bin/bash
    grep -q "Matroska" ${1} && mmv ${1} ${1}.mkv
    And then do the
    Code:
    find . -name '*.avi" -exec convert {} \;
    I have not tested this because I lack the environment you have!
    Henk van Velden

  7. #7

    Default Re: help script in bash with find

    Quote Originally Posted by nrickert View Post
    I suggest you start by designing a script that works for a single file, with name given on the command line. That way you can use "grep" in a test to determine whether to rename this file.

    Once you have a script that works for a single file, you can then come up with a "find" command that calls your script on each file found.
    well, this script works:
    Code:
    #!/bin/bash#qui sotto /dir/da/dove/prelevi/i/files
    cd /home/procuste/Downloads/prove/
    matro=$(mediainfo "mmm.avi" | grep "Matroska")
    echo "$matro"
    if [ -n "$matro" ]; then
    mmv 'mmm.avi' 'mmm.mkv'
    fi
    if mmm.avi is a matroska file it is renamed as .mkv file if it is not a matroska file it remain .avi.
    but I don't now know how to transfere the filename that comes from the "find" command to this script

  8. #8
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,288

    Default Re: help script in bash with find

    You will by now have found out that my post was not complete.

    The second possibility is to incorporate the statmenet in the fibnd command, but that is more complicated. And it ha one big disadvantage, you will get the tune.avi.mlv file where yoy probably want tune.mkv.

    For that you cab adapt the script to
    Code:
    #!/bin/bash
    grep -q "Matroska" ${1} && mmv ${1} ${1%.avi}.mkv
    Henk van Velden

  9. #9
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,288

    Default Re: help script in bash with find

    Quote Originally Posted by pier_andreit View Post
    well, this script works:
    Code:
    #!/bin/bash#qui sotto /dir/da/dove/prelevi/i/files
    cd /home/procuste/Downloads/prove/
    matro=$(mediainfo "mmm.avi" | grep "Matroska")
    echo "$matro"
    if [ -n "$matro" ]; then
    mmv 'mmm.avi' 'mmm.mkv'
    fi
    Yes, in this way you convert the grep reult into a logical, but I think my way is shorter and better understandable.
    Henk van Velden

  10. #10
    Join Date
    Aug 2010
    Location
    Chicago suburbs
    Posts
    12,815
    Blog Entries
    3

    Default Re: help script in bash with find

    Quote Originally Posted by pier_andreit View Post
    if mmm.avi is a matroska file it is renamed as .mkv file if it is not a matroska file it remain .avi.
    but I don't now know how to transfere the filename that comes from the "find" command to this script
    I don't know what your programs "mediainfo" and "mmv" do. So I'm guessing here.

    Try changing your script to:
    Code:
    #! /bin/bash
    
    matro=$(mediainfo "$1" | grep "Matroska")
    
    if [ -n "$matro" ]; then
        dir=`dirname "$1"`
        file=`basename "$1" .avi`
        mmv "$dir/$file.avi" "$dir/$file.mkv"
    fi
    That should allow you to put the filename/path as the an argument to your script. But this depends on your programs working properly with full paths.

    If that all works, then you can call your script from a find command -- so that it is called for each file.
    openSUSE Leap 15.1; KDE Plasma 5;
    testing Leap 15.2Alpha

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •