Using ls to obtain filename in a directory

Discussion in 'Getting Started' started by physicalgrafitti75, Oct 25, 2013.

  1. physicalgrafitti75

    physicalgrafitti75 New Member

    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    Hello.
    I tried searching the site to find an example of this but could not locate.

    I am trying to use the ls command to assign a filename to a parameter that I will use later in my script.

    I want the name of a file in that directory that ends with the extension .trg

    the command I have coded in the script is:

    param = `ls input/${file_name}*.trg`


    it says that param: not found [no such file or directory]

    The file is there....

    Ideas?
  2. grim76

    grim76 Active Member Staff Writer

    Messages:
    177
    Likes Received:
    48
    Trophy Points:
    28
    When you are scripting make sure do use the full path to the location. So if your files are in /opt/input then make sure to put /opt/input.

    Why wouldn't you do a find vs. an ls? With find you can look for the file and then execute things against the file.
  3. physicalgrafitti75

    physicalgrafitti75 New Member

    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    OK, yes, I guess that would make sense.

    The ultimate end is to have param get the value of the actual filename. This will be used later in the script to refer to other filenames for this specific application.

    I am trying the find command now.

    Thanks so much for the quick reply. I will reply back when complete.
  4. physicalgrafitti75

    physicalgrafitti75 New Member

    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    OK, I used the find and that worked.

    So, now I have
    param=`find input/${file_name}*.trg`

    And this gives me the param set to the name of the file that has .trg as an extension.

    The name of that file is

    input1.trg

    Now, how can I take that param and only use the file name without the trigger?

    So, I want to basically take the left 6 bytes of that string.
  5. grim76

    grim76 Active Member Staff Writer

    Messages:
    177
    Likes Received:
    48
    Trophy Points:
    28
    You could write a sed statement to pull off the .trg
  6. physicalgrafitti75

    physicalgrafitti75 New Member

    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    OK, but the SED command seems to want to work only when I am using a file and it reads each record. I only want to manipulate the current string.

    Can SED be used for that? Or should I be using something different. I tried using CUT, but it seems to want to read the file instead of the actual sting.
  7. physicalgrafitti75

    physicalgrafitti75 New Member

    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    Just to follow up:
    What I ended up doing was this:
    param=`find *${file_name}*.trg

    This gave me the filename ending in .trg

    Then, I took that and used a simple string matching parameter:

    appname=${param%.*}

    This took everything to the left of the .

    Which is what I wanted.

    Thanks for the guidance.
  8. grim76

    grim76 Active Member Staff Writer

    Messages:
    177
    Likes Received:
    48
    Trophy Points:
    28
    Happy to help.

Share This Page