-4

I am trying to create a new folder, which has a subfolder inside it. The new folder is being created in a folder that already exists

I am running the following command:

mkdir /mobiledata/Google/Tests/NEW_FOLDER/NEW_SUBFOLDER/

I get an error

cannot create directory ’/mobiledata/Google/Tests/NEW_FOLDER/NEW_SUBFOLDER/: No such file or directory

I think what this means is that when I create NEW_SUBFOLDER/, the command looks for its parent NEW_FOLDER and rejects the command because NEW_FOLDER doesn't exist yet.

Is there anyway for me to run a command that creates the new folder first, then the subfolder? Or another solution?

Thanks.

ZakS
  • 1,073
  • 3
  • 15
  • 27
  • [How to create nonexistent subdirectories recursively using Bash?](https://stackoverflow.com/q/1731767/608639), [Bash mkdir and subfolders](https://stackoverflow.com/q/9242163/608639), and friends. I don't think you should ask this question on other sites. It is equally poor on all sites in the Stack Exchange network. – jww Mar 21 '19 at 08:17

2 Answers2

3
mkdir -p new_folder/with_a_subfolder
hager
  • 313
  • 1
  • 7
1

mkdir will create as many directories as it can with -p switch.

$ mkdir -p mobiledata/Google/Tests/NEW_FOLDER/NEW_SUBFOLDER/
$ find mobiledata/
mobiledata/
mobiledata/Google
mobiledata/Google/Tests
mobiledata/Google/Tests/NEW_FOLDER
mobiledata/Google/Tests/NEW_FOLDER/NEW_SUBFOLDER

From the manual:

   -p, --parents
          no error if existing, make parent directories as needed
sanmai
  • 29,083
  • 12
  • 64
  • 76