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
how do I create a file within a directory at the same time?
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
how do I create a file within a directory at the same time?
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.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Re: how do I create a file within a directory at the same time?
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
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
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
Re: how do I create a file within a directory at the same time?
If by "create" and "put" you mean "copy" you can use
install -D
; see man install
.Re: how do I create a file within a directory at the same time?
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.
Re: how do I create a file within a directory at the same time?
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:
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:
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
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.