0

Hello again Ubuntu users

I'm trying some new things with mkdir and I can't quite get the hang of things

I want to learn how to make a directory inside of a specified path, then a touch.txt text document with words inside or a story

So far I've got

 mkdir ~/Desktop/new_file touch ~/Desktop/new_file/adas.txt

But that doesn't work, and not sure how to get words in it all from one command line

Seth
  • 58,122

2 Answers2

1

This could be done this way:

mkdir ~/Desktop/new_file && touch ~/Desktop/new_file/adas.txt && echo "Hello World" > ~/Desktop/new_file/adas.txt

Information:

  1. && This ensures that the command preceding it executes correctly before the next command, hence if the preceding command fails the next won't execute.

  2. > Directs the output of the echo command into the adas.txt file.

George Udosen
  • 36,677
1

You might consider using mkdir --parents (or mkdir -p for short) to avoid an error if the directory already exists (you can also create nested directories this way, e.g mkdir -p dir1/dir2/dir3 will create the requested directories and not throw an error if they already exist).

You may also want to consider omitting the touch command entirely as > will create the file for you if it doesn't already exist (FYI: >> will append to the end of an existing file, wheras > will overwrite an existing file.

So,

mkdir -p ~/Desktop/new_file && echo "Hello World" > ~/Desktop/new_file/adas.txt

Should do what you want, then you could later do things like:

echo "More text to add" >> ~/Desktop/new_file

More info can be found in the bash manpage (man bash under SHELL GRAMMAR (particularly the Lists section.

Good Luck