0

code

I am new in bash, and using online bash compiler, I am trying to go to access every directory and the file in it in a loop. But every time it is showing the directory does not exist. What is the correct syntax to create and access the directory in online bash compiler like replit?

#!/bin/bash
for I in new, new1
  do
    cp /fileName $1
  done

Output:

cp: cannot stat 'fileName': No such file or directory
cp: cannot stat 'fileName': No such file or directory
rovr138
  • 695
  • 6
  • 9
Nur
  • 19
  • 4
  • 1
    you should add a sample example of what you are trying to do – Cyrille MODIANO Jul 23 '21 at 12:47
  • 2
    Show the code you've tried so far? – Jiří Baum Jul 23 '21 at 12:48
  • ok just a minute – Nur Jul 23 '21 at 12:54
  • I have attached the picture to the question.. please have a look at that. There I have tried to copy a file named "fileName" to the the directories. – Nur Jul 23 '21 at 12:58
  • 1
    `for i in new new1; do echo cp filename "$i"; done` No need for the `,` to separate the elements that you're looping through, Also the `/` means the parent directory on a Unix/Linux directory tree, The `echo` is there to show you what might have been... – Jetchisel Jul 23 '21 at 13:04
  • 2
    [Please do not upload images of code/errors when asking a question.](//meta.stackoverflow.com/q/285551) – 0stone0 Jul 23 '21 at 13:07
  • How I will write now if I want to loop through all the files present in all the directories?@Jetchisel – Nur Jul 23 '21 at 13:39
  • Either use `globstar`s shell option from `bash` or use `find`. I can't do it right now, hopefully someone can answer you. Mean while search this forum , there are a lot of entries/question like yours. – Jetchisel Jul 23 '21 at 13:46

1 Answers1

0

The problem is that you prefix fileName with /, so it's expecting one in the root directory (of the operating system), not a file relative to where the program is being run, but literally a file at /fileName

You then also use $I as the loop variable, but later try to refer to $1 when they should be the same

you almost-certainly want a form like

for path in new new1; do
   cp fileName "$path/"
done
ti7
  • 16,375
  • 6
  • 40
  • 68