[SOLVED] How to put a file into multiple directories

Hapxier

New Member
Joined
Dec 17, 2020
Messages
4
Reaction score
2
Credits
35
Hello.
I'm new to linux and forums alike.
I've been doing linux basics.

I have a question
I'm creating 12 folder with
Mkdir dir{1..12}
Is it possible, on linux, to create a bunch of files that will be inserted into all those folders simultaneously?
Touch file{1..12} dir{1..12} doesn't seem to work.

I've searched online but probably don't search for the right terming, and can't find much on it.
I will give a shot at more searching another day

Thank you in advance.
 
Last edited:


G'day @Hapxier and welcome to linux.org :)

I'll move this to Command Line where it will get better attention

Good luck

Chris Turner
wizardfromoz
 
find * -maxdepth 0 -type d -exec cp -vi testfile{a..z} {} \;

This work, but caused a problem.
It will put it in all directories in the same level (maxdepth 0).
I will keep searching for another way.
 
Hmm. Found two more ways and both are good enough.
Considering the directories and files already created in a home folder, this is the first way:

echo "/home/dir1/ /home/dir2/ /home/dir3/ " | sudo xargs -n 1 cp -v /home/file{a..z}


Considering directories were already made, this is the second way:

touch dir{1..3}/file{a..z}


This seems the best way if its all in the same dir.
But the first way will be better as you can put different locations.

I don't know if this is one of those forums you close the thread, but consider it closed :)
 
Try this:
Bash:
mkdir dir{1..12}
touch dir{1..12}/file{1..12}
The mkdir command should create 12 subdirectories called dir1 to dir12.
And the touch command should populate all of the subdirectories with 12 files.

EDIT: sorry, I didn’t read the entire thread before responding..... you’ve already got it!!
 
G'day @Hapxier - we leave 'em open around here, in case you come back with more to offer, and also for Members who may have a similar problem.

But I have added a "Solved" at the top, which is likely something you can add yourself.

Good work and congratulations.

Chris Turner
wizardfromoz
 

Members online


Latest posts

Top