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

2

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.