Using ls to obtain filename in a directory

P

physicalgrafitti75

Guest
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?
 


OP
G

grim76

Guest
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.
 
OP
P

physicalgrafitti75

Guest
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.
 
OP
P

physicalgrafitti75

Guest
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.
 
OP
P

physicalgrafitti75

Guest
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.
 
OP
P

physicalgrafitti75

Guest
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.
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Linux.org Hosting Donations
Consider making a donation

Members online


Top