Reputation: 1173
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
Upvotes: 2
Views: 6151
Reputation: 460
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
Upvotes: 1
Reputation: 3443
More elegant POSIX solution:
for filpath in /data/logs/2017/jan/201701*; do
filename="$(basename "${filepath}")"
echo "${filename}" > /home/bishopm/${filename%.gz}
done
Upvotes: 0
Reputation: 1761
You've got 2 /
in your path: /home/bishopm//...
. Try:
...
echo $filename > "/home/bishopm${filename%.gz}"
...
Upvotes: 0