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

Forum rules
Before you post please read how to get help
Post Reply
Brutus
Level 1
Level 1
Posts: 1
Joined: Mon Jan 22, 2018 1:55 pm

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

Post by Brutus » Sat Feb 03, 2018 5:52 am

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

User avatar
karlchen
Level 19
Level 19
Posts: 9311
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 » Sat Feb 03, 2018 6:29 am

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
Linux Mint 18.1 64-bit Cinnamon Desktop, Total Commander 9.21a 64-bit
Ubuntu 18.04.1 32-bit Mate Desktop, Total Commander 9.21a 32-bit
Windows? - 1 window in every room

rene
Level 8
Level 8
Posts: 2170
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 » Sat Feb 03, 2018 4:39 pm

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 » Sun Feb 04, 2018 7:03 pm

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 5
Level 5
Posts: 762
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 » Sun Feb 04, 2018 8:29 pm

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
Here to help.

I'm LearnLinux (LL) on YouTube: https://www.youtube.com/channel/UCfp-lN ... naEE6NtDSg
I'm also terminalforlife (TFL) on GitHub: https://github.com/terminalforlife

Post Reply

Return to “Scripts & Bash”