-2

I have a directory "~/files" which contains files a through z with varying contents. I have a file "test" whose contents are "text"

I want to create directory "~/new" containing files a through z with contents "text"

Example bash script:

for filename in "~/files"; do
    cp "~/test" "~/new/filename"
done
oguz ismail
  • 1
  • 16
  • 47
  • 69
Luke Dunn
  • 39
  • 5
  • *I want to create directory "~/new" containing files a through z with contents "text"* `mkdir ~/new && for f in ~/new/{a..z}; do echo text > "$f"; done` – Shawn Aug 22 '21 at 00:54
  • 1
    `~` won't be expanded when it's in quotes (see [this question](https://stackoverflow.com/questions/41871596/why-isnt-tilde-expanding-inside-double-quotes)) -- use e.g. `~/files`, `~/"files"`, or `"$HOME/files"` instead. – Gordon Davisson Aug 22 '21 at 03:39
  • Thanks for your responses! @Shawn, I don't actually want them to be named a-z, they are placeholder names. The real file names reside in the original directory. Also, curious, why am I getting downvoted? Should I have placed my question somewhere else? I would like some guidance, thanks! – Luke Dunn Aug 23 '21 at 01:23

1 Answers1

1

If you don't want the contents of the original file, don't copy it. Just use the filename as the name of a new file.

for f in ~/files/*
do
    echo text > ~/new/"${f##*/}"
done
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks! I tweaked it a little bit though; I don't want the file to literally contain "text," I should have clarified that it was a placeholder for some obscure contents that aren't actually text. It works for what I want when I change the echo line to: `cp test ~/new/"${f##*/}"` – Luke Dunn Aug 23 '21 at 01:41
  • As far as I can tell, the only real question you have is how to make the destination files have the same names as the source files, but in a different directory, and then you just want to repeat the same actions for all of them. – Barmar Aug 23 '21 at 18:24