2

I am trying to launch a script to echo (as a test) the file name of files in a different directory but get the "No such file or directory" error. It looks like it is appending the whole directory of where I'm echoing from to the file name I'm trying to redirect to but how do I fix that. Thank you.

for filename in /data/logs/2017/jan/201701*
do
echo $filename > /home/bishopm/${filename%.gz}
done

Getting the following errors for each file trying to echo:

./data_collector.sh: line 5: /home/bishopm//data/logs/2017/jan/20170131: No such file or directory
martinbshp
  • 1,073
  • 4
  • 22
  • 36

3 Answers3

1

You actually need to tell sh that you want to iterate through the list of files in that directory, so you call the ls command

for filename in $(ls /data/logs/2017/jan/201701*);
do
file=$(basename $filename)
echo $file > /home/bishopm/${file%.gz}
done

Edit: now it should work, provided that the bishopm directory exists Edit2: substituted the rev | cut | rev chain with basename, thanks to Sild

PMonti
  • 456
  • 3
  • 9
0

You've got 2 / in your path: /home/bishopm//.... Try:

...
echo $filename > "/home/bishopm${filename%.gz}"
...
Kind Stranger
  • 1,736
  • 13
  • 18
0

More elegant POSIX solution:

for filpath in /data/logs/2017/jan/201701*; do
    filename="$(basename "${filepath}")"
    echo "${filename}" > /home/bishopm/${filename%.gz}
done
Sild
  • 3,077
  • 3
  • 15
  • 21