Find if the file is compressed or not











up vote
0
down vote

favorite












I need to find whether the file is compressed or not in script.If it is compressed I need to uncompress and send as attachement. My find command results in two files sum12.pdf.Z and sum23.pdf.Z My script is



dir=/home/as1234/bills
cd $dir
for file in `find . -ctime -1 -type f -name "Sum*pdf*"`
do
if [ ${file: -1} == "Z" ]; then
echo "$file is Zipped"
uncompress $file
uuencode $file
fi
done
uuencode $file $file | mailx -s "subject" abc@gmail.com


when I ran this script I got error like



 ${file: -1}: 0403-011 The specified substitution is not valid for this command.


I am using ksh.










share|improve this question




















  • 7




    Why don't you use the file command to check if it is compressed instead? For example file -ib <filename.Z> will give you the MIME type of the file which you can then use in your tests - applicationx-compress; charset=binary. You could then check for that exact string or grep for compress maybe? File extensions aren't used in the Unix/Linux world as they are in Windows.
    – garethTheRed
    Jun 25 '14 at 8:34






  • 1




    The ${file:offset} syntax was introduced in ksh93, you probably have ksh88. Use case $file in *.Z) ... (and don't forget to quote your variables!) (and don't use command substitution on the output of find, use -exec instead)
    – Stéphane Chazelas
    Jun 25 '14 at 8:37












  • @garethTheRed - in my machine case is not installed. Is there any way to find .Z file?
    – Aravind
    Jun 25 '14 at 15:51










  • From the man page for file: There has been a file command in every UNIX since at least Research Version 4 (man page dated November, 1973). What is your machine?
    – garethTheRed
    Jun 25 '14 at 16:52

















up vote
0
down vote

favorite












I need to find whether the file is compressed or not in script.If it is compressed I need to uncompress and send as attachement. My find command results in two files sum12.pdf.Z and sum23.pdf.Z My script is



dir=/home/as1234/bills
cd $dir
for file in `find . -ctime -1 -type f -name "Sum*pdf*"`
do
if [ ${file: -1} == "Z" ]; then
echo "$file is Zipped"
uncompress $file
uuencode $file
fi
done
uuencode $file $file | mailx -s "subject" abc@gmail.com


when I ran this script I got error like



 ${file: -1}: 0403-011 The specified substitution is not valid for this command.


I am using ksh.










share|improve this question




















  • 7




    Why don't you use the file command to check if it is compressed instead? For example file -ib <filename.Z> will give you the MIME type of the file which you can then use in your tests - applicationx-compress; charset=binary. You could then check for that exact string or grep for compress maybe? File extensions aren't used in the Unix/Linux world as they are in Windows.
    – garethTheRed
    Jun 25 '14 at 8:34






  • 1




    The ${file:offset} syntax was introduced in ksh93, you probably have ksh88. Use case $file in *.Z) ... (and don't forget to quote your variables!) (and don't use command substitution on the output of find, use -exec instead)
    – Stéphane Chazelas
    Jun 25 '14 at 8:37












  • @garethTheRed - in my machine case is not installed. Is there any way to find .Z file?
    – Aravind
    Jun 25 '14 at 15:51










  • From the man page for file: There has been a file command in every UNIX since at least Research Version 4 (man page dated November, 1973). What is your machine?
    – garethTheRed
    Jun 25 '14 at 16:52















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I need to find whether the file is compressed or not in script.If it is compressed I need to uncompress and send as attachement. My find command results in two files sum12.pdf.Z and sum23.pdf.Z My script is



dir=/home/as1234/bills
cd $dir
for file in `find . -ctime -1 -type f -name "Sum*pdf*"`
do
if [ ${file: -1} == "Z" ]; then
echo "$file is Zipped"
uncompress $file
uuencode $file
fi
done
uuencode $file $file | mailx -s "subject" abc@gmail.com


when I ran this script I got error like



 ${file: -1}: 0403-011 The specified substitution is not valid for this command.


I am using ksh.










share|improve this question















I need to find whether the file is compressed or not in script.If it is compressed I need to uncompress and send as attachement. My find command results in two files sum12.pdf.Z and sum23.pdf.Z My script is



dir=/home/as1234/bills
cd $dir
for file in `find . -ctime -1 -type f -name "Sum*pdf*"`
do
if [ ${file: -1} == "Z" ]; then
echo "$file is Zipped"
uncompress $file
uuencode $file
fi
done
uuencode $file $file | mailx -s "subject" abc@gmail.com


when I ran this script I got error like



 ${file: -1}: 0403-011 The specified substitution is not valid for this command.


I am using ksh.







shell-script find mailx






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









Rui F Ribeiro

38.2k1475123




38.2k1475123










asked Jun 25 '14 at 8:02









Aravind

52071432




52071432








  • 7




    Why don't you use the file command to check if it is compressed instead? For example file -ib <filename.Z> will give you the MIME type of the file which you can then use in your tests - applicationx-compress; charset=binary. You could then check for that exact string or grep for compress maybe? File extensions aren't used in the Unix/Linux world as they are in Windows.
    – garethTheRed
    Jun 25 '14 at 8:34






  • 1




    The ${file:offset} syntax was introduced in ksh93, you probably have ksh88. Use case $file in *.Z) ... (and don't forget to quote your variables!) (and don't use command substitution on the output of find, use -exec instead)
    – Stéphane Chazelas
    Jun 25 '14 at 8:37












  • @garethTheRed - in my machine case is not installed. Is there any way to find .Z file?
    – Aravind
    Jun 25 '14 at 15:51










  • From the man page for file: There has been a file command in every UNIX since at least Research Version 4 (man page dated November, 1973). What is your machine?
    – garethTheRed
    Jun 25 '14 at 16:52
















  • 7




    Why don't you use the file command to check if it is compressed instead? For example file -ib <filename.Z> will give you the MIME type of the file which you can then use in your tests - applicationx-compress; charset=binary. You could then check for that exact string or grep for compress maybe? File extensions aren't used in the Unix/Linux world as they are in Windows.
    – garethTheRed
    Jun 25 '14 at 8:34






  • 1




    The ${file:offset} syntax was introduced in ksh93, you probably have ksh88. Use case $file in *.Z) ... (and don't forget to quote your variables!) (and don't use command substitution on the output of find, use -exec instead)
    – Stéphane Chazelas
    Jun 25 '14 at 8:37












  • @garethTheRed - in my machine case is not installed. Is there any way to find .Z file?
    – Aravind
    Jun 25 '14 at 15:51










  • From the man page for file: There has been a file command in every UNIX since at least Research Version 4 (man page dated November, 1973). What is your machine?
    – garethTheRed
    Jun 25 '14 at 16:52










7




7




Why don't you use the file command to check if it is compressed instead? For example file -ib <filename.Z> will give you the MIME type of the file which you can then use in your tests - applicationx-compress; charset=binary. You could then check for that exact string or grep for compress maybe? File extensions aren't used in the Unix/Linux world as they are in Windows.
– garethTheRed
Jun 25 '14 at 8:34




Why don't you use the file command to check if it is compressed instead? For example file -ib <filename.Z> will give you the MIME type of the file which you can then use in your tests - applicationx-compress; charset=binary. You could then check for that exact string or grep for compress maybe? File extensions aren't used in the Unix/Linux world as they are in Windows.
– garethTheRed
Jun 25 '14 at 8:34




1




1




The ${file:offset} syntax was introduced in ksh93, you probably have ksh88. Use case $file in *.Z) ... (and don't forget to quote your variables!) (and don't use command substitution on the output of find, use -exec instead)
– Stéphane Chazelas
Jun 25 '14 at 8:37






The ${file:offset} syntax was introduced in ksh93, you probably have ksh88. Use case $file in *.Z) ... (and don't forget to quote your variables!) (and don't use command substitution on the output of find, use -exec instead)
– Stéphane Chazelas
Jun 25 '14 at 8:37














@garethTheRed - in my machine case is not installed. Is there any way to find .Z file?
– Aravind
Jun 25 '14 at 15:51




@garethTheRed - in my machine case is not installed. Is there any way to find .Z file?
– Aravind
Jun 25 '14 at 15:51












From the man page for file: There has been a file command in every UNIX since at least Research Version 4 (man page dated November, 1973). What is your machine?
– garethTheRed
Jun 25 '14 at 16:52






From the man page for file: There has been a file command in every UNIX since at least Research Version 4 (man page dated November, 1973). What is your machine?
– garethTheRed
Jun 25 '14 at 16:52












1 Answer
1






active

oldest

votes

















up vote
0
down vote













The filename suffix after the final dot may be had with ${file##*.}.



In this case though, I would consider doing the uncompressing and the uuencoding with find -exec directly like this:



#!/bin/sh

dir=/home/as1234/bills

find "$dir" -type f -ctime -1 -name "Sum*.pdf*" -exec sh -c '
for pathname do
filename=$( basename "${pathname%.pdf*}.pdf" )
if [ "${pathname##*.}" = "Z" ]; then
uncompress -c "$pathname"
elif [ "${pathname##*.}" = "gz" ]; then
gzip -cd "$pathname"
else
cat "$pathname"
fi |
uuencode "$filename" |
mailx -s "subject ($filename)" abc@gmail.com
done' sh {} +


This way, you would support pathnames with spaces and other troublesome characters. The sh -c script also does not store the uncompressed files but uncompresses them, uuencodes them and sends them in one go.



I also added handling of gzip-compressed files.



Related:




  • Understanding the -exec option of `find`

  • Why is looping over find's output bad practice?




Alternative implementation of the sh -c script using case ... esac instead of multiple if and elif statements.



find "$dir" -type f -ctime -1 -name "Sum*.pdf*" -exec sh -c '
for pathname do
filename=$( basename "${pathname%.pdf*}.pdf" )
case $pathname in
*.Z) uncompress -c "$pathname" ;;
*.gz) gzip -cd "$pathname" ;;
*) cat "$pathname"
esac |
uuencode "$filename" |
mailx -s "subject ($filename)" abc@gmail.com
done' sh {} +





share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f139068%2ffind-if-the-file-is-compressed-or-not%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    The filename suffix after the final dot may be had with ${file##*.}.



    In this case though, I would consider doing the uncompressing and the uuencoding with find -exec directly like this:



    #!/bin/sh

    dir=/home/as1234/bills

    find "$dir" -type f -ctime -1 -name "Sum*.pdf*" -exec sh -c '
    for pathname do
    filename=$( basename "${pathname%.pdf*}.pdf" )
    if [ "${pathname##*.}" = "Z" ]; then
    uncompress -c "$pathname"
    elif [ "${pathname##*.}" = "gz" ]; then
    gzip -cd "$pathname"
    else
    cat "$pathname"
    fi |
    uuencode "$filename" |
    mailx -s "subject ($filename)" abc@gmail.com
    done' sh {} +


    This way, you would support pathnames with spaces and other troublesome characters. The sh -c script also does not store the uncompressed files but uncompresses them, uuencodes them and sends them in one go.



    I also added handling of gzip-compressed files.



    Related:




    • Understanding the -exec option of `find`

    • Why is looping over find's output bad practice?




    Alternative implementation of the sh -c script using case ... esac instead of multiple if and elif statements.



    find "$dir" -type f -ctime -1 -name "Sum*.pdf*" -exec sh -c '
    for pathname do
    filename=$( basename "${pathname%.pdf*}.pdf" )
    case $pathname in
    *.Z) uncompress -c "$pathname" ;;
    *.gz) gzip -cd "$pathname" ;;
    *) cat "$pathname"
    esac |
    uuencode "$filename" |
    mailx -s "subject ($filename)" abc@gmail.com
    done' sh {} +





    share|improve this answer



























      up vote
      0
      down vote













      The filename suffix after the final dot may be had with ${file##*.}.



      In this case though, I would consider doing the uncompressing and the uuencoding with find -exec directly like this:



      #!/bin/sh

      dir=/home/as1234/bills

      find "$dir" -type f -ctime -1 -name "Sum*.pdf*" -exec sh -c '
      for pathname do
      filename=$( basename "${pathname%.pdf*}.pdf" )
      if [ "${pathname##*.}" = "Z" ]; then
      uncompress -c "$pathname"
      elif [ "${pathname##*.}" = "gz" ]; then
      gzip -cd "$pathname"
      else
      cat "$pathname"
      fi |
      uuencode "$filename" |
      mailx -s "subject ($filename)" abc@gmail.com
      done' sh {} +


      This way, you would support pathnames with spaces and other troublesome characters. The sh -c script also does not store the uncompressed files but uncompresses them, uuencodes them and sends them in one go.



      I also added handling of gzip-compressed files.



      Related:




      • Understanding the -exec option of `find`

      • Why is looping over find's output bad practice?




      Alternative implementation of the sh -c script using case ... esac instead of multiple if and elif statements.



      find "$dir" -type f -ctime -1 -name "Sum*.pdf*" -exec sh -c '
      for pathname do
      filename=$( basename "${pathname%.pdf*}.pdf" )
      case $pathname in
      *.Z) uncompress -c "$pathname" ;;
      *.gz) gzip -cd "$pathname" ;;
      *) cat "$pathname"
      esac |
      uuencode "$filename" |
      mailx -s "subject ($filename)" abc@gmail.com
      done' sh {} +





      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        The filename suffix after the final dot may be had with ${file##*.}.



        In this case though, I would consider doing the uncompressing and the uuencoding with find -exec directly like this:



        #!/bin/sh

        dir=/home/as1234/bills

        find "$dir" -type f -ctime -1 -name "Sum*.pdf*" -exec sh -c '
        for pathname do
        filename=$( basename "${pathname%.pdf*}.pdf" )
        if [ "${pathname##*.}" = "Z" ]; then
        uncompress -c "$pathname"
        elif [ "${pathname##*.}" = "gz" ]; then
        gzip -cd "$pathname"
        else
        cat "$pathname"
        fi |
        uuencode "$filename" |
        mailx -s "subject ($filename)" abc@gmail.com
        done' sh {} +


        This way, you would support pathnames with spaces and other troublesome characters. The sh -c script also does not store the uncompressed files but uncompresses them, uuencodes them and sends them in one go.



        I also added handling of gzip-compressed files.



        Related:




        • Understanding the -exec option of `find`

        • Why is looping over find's output bad practice?




        Alternative implementation of the sh -c script using case ... esac instead of multiple if and elif statements.



        find "$dir" -type f -ctime -1 -name "Sum*.pdf*" -exec sh -c '
        for pathname do
        filename=$( basename "${pathname%.pdf*}.pdf" )
        case $pathname in
        *.Z) uncompress -c "$pathname" ;;
        *.gz) gzip -cd "$pathname" ;;
        *) cat "$pathname"
        esac |
        uuencode "$filename" |
        mailx -s "subject ($filename)" abc@gmail.com
        done' sh {} +





        share|improve this answer














        The filename suffix after the final dot may be had with ${file##*.}.



        In this case though, I would consider doing the uncompressing and the uuencoding with find -exec directly like this:



        #!/bin/sh

        dir=/home/as1234/bills

        find "$dir" -type f -ctime -1 -name "Sum*.pdf*" -exec sh -c '
        for pathname do
        filename=$( basename "${pathname%.pdf*}.pdf" )
        if [ "${pathname##*.}" = "Z" ]; then
        uncompress -c "$pathname"
        elif [ "${pathname##*.}" = "gz" ]; then
        gzip -cd "$pathname"
        else
        cat "$pathname"
        fi |
        uuencode "$filename" |
        mailx -s "subject ($filename)" abc@gmail.com
        done' sh {} +


        This way, you would support pathnames with spaces and other troublesome characters. The sh -c script also does not store the uncompressed files but uncompresses them, uuencodes them and sends them in one go.



        I also added handling of gzip-compressed files.



        Related:




        • Understanding the -exec option of `find`

        • Why is looping over find's output bad practice?




        Alternative implementation of the sh -c script using case ... esac instead of multiple if and elif statements.



        find "$dir" -type f -ctime -1 -name "Sum*.pdf*" -exec sh -c '
        for pathname do
        filename=$( basename "${pathname%.pdf*}.pdf" )
        case $pathname in
        *.Z) uncompress -c "$pathname" ;;
        *.gz) gzip -cd "$pathname" ;;
        *) cat "$pathname"
        esac |
        uuencode "$filename" |
        mailx -s "subject ($filename)" abc@gmail.com
        done' sh {} +






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Oct 10 at 6:40

























        answered Jun 22 at 12:10









        Kusalananda

        116k15218351




        116k15218351






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f139068%2ffind-if-the-file-is-compressed-or-not%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Morgemoulin

            Scott Moir

            Souastre