Home > Software engineering >  Getting the filename from a directory only using a string from the filename? and setting that found
Getting the filename from a directory only using a string from the filename? and setting that found

Time:07-29

Could someone steer me towards a resource to do the following in unix: I want to set a variable equal to a filename so that I can input that variable/filename into a command line tool. I am trying to automate the process of running this command line tool by doing so. My input files will always have the same string at the end their unique names. How can I get this filename by searching the directory for a string AND successfully input that variable into command line tool?

so the unix code would look something like:

file1="find . -maxdepth 1 -name "string""

my command line tool --input $file1

thanks for your patience!

P.S only one file with that string will be in a directory at a time.

CodePudding user response:

Instead of work with variables you can directly use the output of find command as parameter in your command line:

my_command_line_tool --input "$(find . -maxdepth 1 -name "*string*")"

If you expect more than one file you may remove the outer quotation marks. But this may broke the command if you have files which match the string, but have special characters like space, new line, etc in filename.

  •  Tags:  
  • unix
  • Related