-1
mainFile=$(find /home/INVENT/custREAD -name '*.txt.gz' -type f -mmin 300)

I m using the above line in my shell script to fetch the file from the location /home/INVENT/custREAD and put it in the variable mainFile

But when I echo this variable, I see:

/home/INVENT/custREAD/filename.txt

But I want that only the file name to be in the variable, because then I want to unzip this file with

  gunzip "$mainFile"

but this is causing an error

gzip : No such file or directory

Can someone please correct where am I going wrong?

Aviator
  • 543
  • 1
  • 4
  • 10

1 Answers1

0

If you have GNU find, you can use -printf to drop the path:

mainFile=$(find /home/INVENT/custREAD -name '*.txt.gz' -type f -printf '%f')

This assumes that you'll find just one file, or else all filenames will be in a single string.

You could also use parameter expansion to remove everything up to the last slash:

mainFile=$(find /home/INVENT/custREAD -name '*.txt.gz' -type f)
echo "${mainFile##*/}"

It looks like you don't even need find, since you seem to expect just one .txt.gz file, so you could use a glob instead:

mainFile=/home/INVENT/custREAD/*.txt.gz
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116