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
lsIn 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 -lIn 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/homeworkIn 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 schoolIn 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
lsIn 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 filecreates an empty file,mkdir dircreates a directory,mkdir -p a/b/ccreates a whole path.