Creating Files and Directories

touch: create an empty file

touch creates an empty file. Give it several names to create several files at once:

touch notes.txt
touch a.txt b.txt c.txt
ls

In the terminal

student@vulntech:~$ touch notes.txt
student@vulntech:~$ touch a.txt b.txt c.txt
student@vulntech:~$ ls
a.txt  b.txt  c.txt  notes.txt

If the file already exists, touch doesn't change its contents; it only updates its modified time. That's where the name comes from: it "touches" the file.

mkdir: make a directory

mkdir projects
mkdir music videos
ls -l

In the terminal

student@vulntech:~$ mkdir projects
student@vulntech:~$ mkdir music videos
student@vulntech:~$ ls -l
total 2
drwxr-sr-x 2 student student 0 Oct 16 19:11 music
drwxr-sr-x 2 student student 0 Oct 16 19:11 projects
drwxr-sr-x 2 student student 0 Oct 16 19:11 videos

Building a whole tree with -p

mkdir can't create a directory inside one that doesn't exist yet:

mkdir school/maths/homework

In the terminal

student@vulntech:~$ mkdir school/maths/homework
mkdir: cannot create directory ‘school/maths/homework’: No such file or directory

The -p option (parents) creates every missing directory along the way. It also doesn't complain if the directory already exists:

mkdir -p school/maths/homework
tree school

In the terminal

student@vulntech:~$ mkdir -p school/maths/homework
student@vulntech:~$ tree school
school
└── maths
    └── homework

3 directories, 0 files

Names with spaces

The shell splits a command at spaces, so mkdir my files creates two directories, my and files. To use a space inside a name, put the name in quotes or put a backslash before the space:

mkdir "my files"
mkdir holiday\ photos
ls

In the terminal

student@vulntech:~$ mkdir "my files"
student@vulntech:~$ mkdir holiday\ photos
student@vulntech:~$ ls
'holiday photos'  'my files'

Tip: Tab completion adds the backslashes for you.

Remember: touch file creates an empty file, mkdir dir creates a directory, mkdir -p a/b/c creates a whole path.

HOME LEARN COMMUNITY DASHBOARD
Practice terminal Starting…