Lets say I have this variable
FILE=/data/tenant/dummy/TEST/logs/2020-02-03_16.20-LSTRM.log
I'm trying to get the name of the 4th sub directory, in this example TEST
job=${FILE%/*/*} # This gives me /data/tenant/dummy/TEST
Then
name=${job##*/}
This give me exactly what I want - Test
However when I try to use this in for loop like this:
for FILE in "/data/tenant/dummy/*/logs/*$year-*"; do
job=${FILE%/*/*}
echo $job # /data/tenant/dummy/TEST(and few other directories, so far so good)
name=${job##*/}
echo $name
done
The result of echo $name
shows the list of files in my current directory I'm in instead of TEST
Main issue is the double quotes groups all files into a single long string:
You can see this if you do something like:
This should print
++++++++++++++
once followed by a single line with a long list of file names.To process the files individually remove the double quotes, eg:
It's also good practice to wrap individual variable references in double quotes, eg: