Log inSign up

Making new directories and empty files from the shell

Run mkdir -p /srv/app/logs to create a directory together with any missing parents, and touch notes.txt to create an empty file or refresh the timestamp of an existing one. Add -m 750 to mkdir to set the mode at creation, or use sudo install -d -o deploy -g deploy -m 750 /srv/app when owner and mode must be right from the first second.

Topic: Linux command line · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

How do I create a single directory?

Type mkdir followed by the name. A relative name is created inside your current directory, an absolute name starting with / anywhere you have write access. Both tools ship with coreutils on every Linux image, so you can use them right after your first login.

mkdir projects
mkdir /home/deploy/projects/site
mkdir --version | head -n 1

Several names on one line create several directories: mkdir images css js. Names may contain spaces if you quote them, but on a server it is easier to stick to lower case letters, digits, dashes and underscores, which never need quoting in scripts or config files.

How do I create nested directories in one step?

Add -p (parents). It creates every missing level along the path and stays quiet when the directory already exists, which makes it safe in scripts that run more than once.

mkdir -p /home/deploy/sites/example.net/public
mkdir -pv /srv/app/shared/uploads

With -v mkdir prints each directory it actually creates. Bash brace expansion lets you lay out a whole structure at once, because the shell expands the braces into separate paths before mkdir sees them:

mkdir -p /srv/app/{releases,shared/{logs,uploads,cache}}
ls -R /srv/app

The result is releases and shared under /srv/app, with three folders inside shared. Do not put spaces after the commas inside braces, or the expansion stops working.

How can I set permissions while creating a directory?

Use -m with an octal or symbolic mode. Without it, the new directory gets 777 minus your umask, which on most servers ends up as 755 (owner writes, everyone reads).

umask
mkdir -m 700 ~/private
mkdir -m 2775 /srv/shared-team
ls -ld ~/private /srv/shared-team

One detail surprises people: with mkdir -p -m 700 a/b/c only the last directory, c, receives mode 700; the parents created on the way get the umask default. If every level needs a strict mode, create them one at a time or fix them afterwards with chmod. The 2 in 2775 sets the setgid bit, so files created inside inherit the directory group, a common setup for folders shared by a team. The file permissions guide explains these numbers.

How do I create a directory owned by another user?

mkdir always creates directories owned by whoever runs it, so a directory made with sudo belongs to root. The install command, also part of coreutils, creates the directory with the owner, group and mode you specify in one atomic step:

sudo install -d -o deploy -g deploy -m 750 /srv/app
sudo install -d -o www-data -g www-data -m 755 /var/www/example.net
ls -ld /srv/app /var/www/example.net

On Rocky and Alma the web server user is nginx or apache rather than www-data. The alternative is two commands, sudo mkdir then sudo chown deploy:deploy, which is fine as long as nothing writes into the folder between them. Ownership in more detail is covered in the chown guide. On Rocky and Alma, also remember that SELinux labels new folders from their parent; a web root created outside /var/www may need restorecon or a new rule, which the SELinux basics guide describes.

How do I create an empty file with touch?

Give touch one or more names. Missing files are created with zero bytes, and existing files keep their content while their access and modification times move to now.

touch index.html
touch app.log error.log access.log
touch /var/www/example.net/{index.html,robots.txt}
ls -l index.html

Typical uses on a server include placeholder files, a .gitkeep so Git tracks an empty folder, marker files that scripts test for, and pre-creating a log file before a service starts. When the file must have a specific owner and mode, install again does it in one go: sudo install -m 640 -o deploy -g adm /dev/null /var/log/app.log creates an empty log owned by deploy and readable by the adm group.

How do I change a file timestamp without editing it?

touch sets timestamps to the current time by default, and you can supply any other time with -d in a readable format or -t in the compact [[CC]YY]MMDDhhmm[.ss] form.

touch -d "2026-11-01 08:30" report.pdf
touch -t 202611010830 report.pdf
touch -r original.conf copy.conf
stat report.pdf
  • -a changes only the access time, -m only the modification time.
  • -r file copies the timestamps of another file.
  • -c (no create) updates existing files and silently skips missing ones.

Adjusting dates is useful for testing cleanup jobs: create a file dated 40 days ago and confirm that your find -mtime +30 -delete rule removes it, as shown in the find guide. The change time (ctime) cannot be set by touch; the kernel updates it whenever metadata changes.

What other ways are there to create or empty a file?

Shell redirection creates a file just as well, and it is the usual way to empty one without deleting it, for example a log that a service still holds open.

: > empty.txt
printf 'line one\n' > one-line.txt
truncate -s 0 /var/log/app/debug.log
fallocate -l 2G /srv/placeholder.img

The colon form truncates or creates empty.txt. truncate -s 0 empties a file in place, so a writer keeps writing to the same file. fallocate reserves real disk space instantly, which is the method used to build a swap file in the swap file guide. Writing into root owned paths needs sudo tee or sudo truncate, because a plain sudo echo x > file performs the redirection as your own user.

Why does mkdir or touch fail?

  • File exists: mkdir without -p refuses an existing name. Add -p or check with ls -ld.
  • No such file or directory: a parent is missing. Use mkdir -p.
  • Permission denied: you lack write access to the parent folder. Create it with sudo or under your home directory.
  • No space left on device with free space showing: the filesystem ran out of inodes, often from millions of tiny cache files. Check with df -i.
  • Read-only file system: the disk was remounted read only after an error. Look at sudo dmesg | tail and open a support ticket if the cause is unclear.

Questions about Create directories and empty files

Does mkdir -p complain if the directory already exists?

No. With -p an existing directory counts as success, which is why it is the safe choice in setup scripts.

Will touch overwrite the content of an existing file?

Never. touch only changes timestamps on an existing file. Use truncate -s 0 or : > file when you really want it empty.

How do I create a hidden directory?

Start the name with a dot, for example mkdir ~/.config/myapp. Plain ls hides it; ls -a shows it.

What mode should a web root have?

Directories usually 755 and files 644, owned by your deploy user with the web server only reading them. Upload or cache folders need write access for the web server user.

Can I create files of a given size for testing?

Yes. fallocate -l 500M test.img reserves space instantly, and truncate -s 500M sparse.img makes a sparse file that uses almost no real disk until data is written.