1

I'd like to create a new directory that looks like /dir1/dir2/dir3/dir4 but I can't.

If I create the directories one by one from the parent and new parent directories, everything works:

lm@debian:~$ sudo mkdir -v ./Pictures/Photos1/onbike2023/
mkdir: cannot create directory ‘./Pictures/Photos1/onbike2023/’: No such file or directory
lm@debian:~$ sudo mkdir -v ./Pictures/Photos1/
mkdir: created directory './Pictures/Photos1/'
lm@debian:~$ sudo mkdir -v ./Pictures/Photos1/onbike2023/
mkdir: created directory './Pictures/Photos1/onbike2023/'
lm@debian:~$ 

What am I doing wrong?

0

2 Answers 2

5

mkdir by default won't create intermediate directories. That is, if you want mkdir foo/bar/baz to succeed, foo and foo/bar had better already exist.

However, you can change this behavior by mkdir -p foo/bar/baz. This option will cause any intermediate directories to be created if necessary. It has the added benefit of not causing mkdir to exit with an error code if foo/bar/baz already exists.

1

extract from man mkdir

-p, --parents
              no  error if existing, make parent directories as needed, with their file modes unaffected by
              any -m option.

Not the answer you're looking for? Browse other questions tagged .