0

I'm a first time poster & new-ish to coding:

I need to copy a file ING_30.tif into 100s of folders, so I'm hoping to use a terminal command to do this quickly.

  • Both ING_30.tif and the folders I need to copy it into are in the same parent folder, ING/.
  • I don't want to copy ING_30.tif into every folder in ING/, just the folders in ING/ that start with AST (e.g. AST_1113054).
  • These AST* folders already have other files in them which I don't want to remove.

Help would be appreciated. I found posts online that copied a file into multiple folders, but not code that let me specify that I only want to copy into the AST* folders. I also saw that some commands copied over the file but removed existing files already in the folder, which I need to avoid. Thanks!

AdminBee
  • 22,803
  • Do any of the folders already have a file in them with the same name? Have you tried anything so far? Add this information to your question. – Nasir Riley Sep 12 '21 at 21:04
  • Yes, some of the folders I've already manually copied ING_30.tif into them. It would be okay for this file to be copied over.. just not the other contents of the folder. – speitzer Sep 12 '21 at 21:24
  • I've attempted, when in the folder in terminal: "cp ING_30.tif AST*", but it returns an error that the folder is a directory (not copied). – speitzer Sep 12 '21 at 21:25
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 22 '21 at 12:16

2 Answers2

1

Another option:

for d in AST*/; do cp ING_30.tif "$d"; done

This will loop over all directories (enforced by the trailing /) that match the glob pattern AST* and copy the file there.

This is safer than using xargs in case your directory names can contain spaces or other funny characters (see answers to this question for more insight), unless you have the GNU version which accepts the -0 option, and in addition have a means to feed the directory names as NULL-terminated strings to xargs.

AdminBee
  • 22,803
0

Found a solution:

echo AST* | xargs -n 1 cp ING_30.tif

This copied my .tif file into all my AST_ folders, without removing any of the other files that were in the folders.

AdminBee
  • 22,803
  • There is no reason that it would remove any other files in the folder unless they had the same name. If they did, it would prompt beforehand unless one used cp -f. – Nasir Riley Sep 12 '21 at 22:14