finding file type

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • trs204
    New Member
    • Feb 2007
    • 12

    finding file type

    I am newbie... this may be a basic question...

    In korn shell script, how to find the type of the file.... here is the pseudo

    if (filename is of type ".eml" or ".jpg") then
    --- do something

    thx in advance.
  • Motoma
    Recognized Expert Specialist
    • Jan 2007
    • 3236

    #2
    Originally posted by trs204
    I am newbie... this may be a basic question...

    In korn shell script, how to find the type of the file.... here is the pseudo

    if (filename is of type ".eml" or ".jpg") then
    --- do something

    thx in advance.
    Do you have a filename to begin with? Could you post your shell script thus far?

    Comment

    • trs204
      New Member
      • Feb 2007
      • 12

      #3
      The directory contains the following files
      .html
      .jpg
      .css
      .js

      I want only the .jpg files checked based on the extension or some file type logic.

      1) for x in ${directory}/*.*
      2) if (x endswith .jpg) then /// finding a particular type of file.
      // do something

      I am looking for the exact condition (FOR ENDS WITH) how to keep on the second line.

      thx in advance.

      Comment

      • Motoma
        Recognized Expert Specialist
        • Jan 2007
        • 3236

        #4
        Originally posted by trs204
        The directory contains the following files
        .html
        .jpg
        .css
        .js

        I want only the .jpg files checked based on the extension or some file type logic.

        1) for x in ${directory}/*.*
        2) if (x endswith .jpg) then /// finding a particular type of file.
        // do something

        I am looking for the exact condition (FOR ENDS WITH) how to keep on the second line.

        thx in advance.
        I can think of ways to do this with filters, but off hand I am not familiar with ksh or shell scripting.

        Code:
        $ head `ls | grep ".txt"`
        This will perform "head" on all files in the current directory that have the .txt extension.

        Code:
        $ grep -i toasty `find . | grep ".txt"`
        This will perform "grep -i toasty" on all files in the current directory and subdirectories that have the .txt extension.

        In these examples, there is no iteration needed. Simple command line filters can do the necessary tasks.

        I hope this helps.

        Comment

        • trs204
          New Member
          • Feb 2007
          • 12

          #5
          As I have to modify the existing script, is there any way in korn shell script to check... if the filename contains or ends with.

          Any references on string functions (like finding word in a string, finding character in a string) is helpful. thanx in advance.

          Comment

          • Motoma
            Recognized Expert Specialist
            • Jan 2007
            • 3236

            #6
            Originally posted by trs204
            As I have to modify the existing script, is there any way in korn shell script to check... if the filename contains or ends with.
            Maybe it would help for you to post the ksh script then.

            Comment

            • cybervegan
              New Member
              • Jan 2007
              • 36

              #7
              Originally posted by trs204
              The directory contains the following files
              .html
              .jpg
              .css
              .js

              I want only the .jpg files checked based on the extension or some file type logic.

              1) for x in ${directory}/*.*
              2) if (x endswith .jpg) then /// finding a particular type of file.
              // do something

              I am looking for the exact condition (FOR ENDS WITH) how to keep on the second line.

              thx in advance.
              Maybe you can just change the filespec of the for statement to "/*.jpg"?

              That would be fine if it's only jpg's you want to process. If it's complicated, I'd personally use Python (but that's just me).

              hth
              -cybervegan

              Comment

              • ghostdog74
                Recognized Expert Contributor
                • Apr 2006
                • 511

                #8
                Originally posted by trs204
                The directory contains the following files
                .html
                .jpg
                .css
                .js

                I want only the .jpg files checked based on the extension or some file type logic.

                1) for x in ${directory}/*.*
                2) if (x endswith .jpg) then /// finding a particular type of file.
                // do something

                I am looking for the exact condition (FOR ENDS WITH) how to keep on the second line.

                thx in advance.
                #eg only, find *.jpg files and remove them
                find /dir -type f -name "*.jpg" -exec rm {} \;

                Comment

                • trs204
                  New Member
                  • Feb 2007
                  • 12

                  #9
                  sorry motoma... its a very big script to post and has lot of env variables. I tried to post lot of references... will not be clear.

                  Thanks for the replies.

                  I had thought of using this
                  if [[ "`echo ${fname} | cut -d'.' -f2`" = "jpg" ]]; then
                  -- do some thing.

                  Comment

                  • michaelb
                    Recognized Expert Contributor
                    • Nov 2006
                    • 534

                    #10
                    find /dir -type f -name "*.jpg" -exec rm {} \;
                    Note that this command will do a recursive search...
                    I'm not sure the intention here was to delete all jpg files in the dir hierarchy.

                    Here's a working example in Bourne shell, I believe it can be easily converted to any other standard shell.

                    Code:
                    #!/bin/sh
                    
                    for fname in `ls`
                    do
                       if echo $fname | egrep -i '.+\.jpg$' >/dev/null 2>&1 ; then 
                           # perform the desired action on the file; rm, etc
                           echo got $fname
                       fi
                    done
                    
                    exit 0
                    I use case-insensitive match, assuming pic.JPG is the same file-type as pic.jpg
                    If this is not a desired behavior remove the "-i" option
                    I make sure that hidden file ".jpg" will not be captured.

                    Comment

                    • michaelb
                      Recognized Expert Contributor
                      • Nov 2006
                      • 534

                      #11
                      Sorry, my last point is moot - the `ls` command won't return hidden files anyway, so pattern matching can be made even simpler:

                      Code:
                      if echo $fname | grep -i '\.jpg$' >/dev/null 2>&1 ; then

                      Comment

                      • sqlxpert
                        New Member
                        • Mar 2008
                        • 2

                        #12
                        Thank you for your advise!
                        My company always used if [ ... ]; then ... fi.
                        Please notice the [ ]. If I want to use your code, it can only work without the [ ].
                        Why?
                        Thank you again.

                        Comment

                        • sqlxpert
                          New Member
                          • Mar 2008
                          • 2

                          #13
                          Never mind!
                          It will work if you put ` ` around your command inside [ ].


                          Originally posted by sqlxpert
                          Thank you for your advise!
                          My company always used if [ ... ]; then ... fi.
                          Please notice the [ ]. If I want to use your code, it can only work without the [ ].
                          Why?
                          Thank you again.

                          Comment

                          Working...