how do I create a file within a directory at the same time?

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
Brutus

how do I create a file within a directory at the same time?

Post by Brutus »

Hi guys
quick question
I know how to make multiple directories at the same time, for example...

mkdir -p tree/{dir1,dir2,dir3}

but how do I do that and put a file in those directories at the same time?

thanks
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
karlchen
Level 23
Level 23
Posts: 18206
Joined: Sat Dec 31, 2011 7:21 am
Location: Germany

Re: how do I create a file within a directory at the same time?

Post by karlchen »

Hello, Brutus.

Creating directories is one thing. After all a directory is a directory.
Creating files is a different thing, considering the countless different file formats which exist.
To the best of my knowledge there is no single command which will do what you have in mind.

Yet, very likely there is a way of achieving your goal.
Create a small script which
+ creates the directories which you want to create (command mkdir)
+ creates an empty file in each of the newly created directories (command touch)

HTH,
Karl
Image
The people of Alderaan have been bravely fighting back the clone warriors sent out by the unscrupulous Sith Lord Palpatine for 771 days now.
Lifeline
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: how do I create a file within a directory at the same time?

Post by rene »

If by "create" and "put" you mean "copy" you can use install -D; see man install.
lmuserx4849

Re: how do I create a file within a directory at the same time?

Post by lmuserx4849 »

The answer has already been provided (combination of mkdir and touch). But... if you are looking for additional information, the basic file manipulation commands are all part of the GNU Core Utilities. They have a really nice manual. Every linux system has this package.
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: how do I create a file within a directory at the same time?

Post by Termy »

Need specifics in order for me to be more useful here, but it sounds like what you're wanting is a for loop; however, this is more where shell becomes 'programming', so I would do some research if you're wanting to go that into shell. Since you're already using brace expansion, I expect you'd be up for this. My inbox is open if you have any questions about that.

A for loop is set in many ways, but here's a very "proper", traditional approach:

Code: Select all

for WORD in LIST
do
	COMMANDS
done
Where WORD is a word (the shell's idea of a word; a field) to act as the placeholder (variable) representing the current LIST (series of words on which to process, which in this case can be directories) item which COMMANDS are running on.

Actually, I'll just make up a scenario, as it might help. Let's say you want to create a directory for 2018, 2019, 2020, 2021, and 2022, but you also want to create one file for each month in each directory. Obviously doing this manually would be as useless as a chocolate kettle. So let's use a for loop:

Code: Select all

for DIR in 20{18..22}
do
	mkdir "$DIR"
	
	if cd "$DIR"
	then
		touch {Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec}.txt
	fi
	
	cd -
done
I'm also Terminalforlife on GitHub.
Locked

Return to “Scripts & Bash”