0

I want to get the file name from different server and store it in a variable.

example: from 1 server I want to get the file name using below command

FILE_NAME=$(ssh user@host ls /acd/file/zipped/processing/trigger/Daily_trigger_$DATE*.csv*)

but FILE_NAME stores complete path and filename:

/acd/file/zipped/processing/trigger/Daily_trigger_19072021_220305.csv

I only want the filename.

Toto
  • 89,455
  • 62
  • 89
  • 125
  • if you are receiving the whole path, you can try using regex in order to get only the characters at the end for the filename, like in this example: https://stackoverflow.com/questions/9363145/regex-for-extracting-filename-from-path – yeeshue99 Jul 21 '21 at 19:04
  • 1
    Assuming your glob always returns exactly one file: `FILE_NAME=$(ssh user@host basename /acd/file/zipped/processing/trigger/Daily_trigger_$DATE*.csv*)` – jhnc Jul 21 '21 at 20:12
  • thanks jhnc for the suggestion, the basename worked perfectly. – Sudhanshu pandey Jul 23 '21 at 19:56

1 Answers1

0

If your system has basename, you can use it, as in jhnc's comment, above.

If you want a pure bash solution, you can use

${FILE_NAME##*/}

to get only the filename part. See Shell Parameter expansion in the bash manual for more.

MassPikeMike
  • 672
  • 3
  • 12