Hey guys, this is part 3 of the series Know your commands.

If you are new here, please also checkout the other parts of this series.

In this part I will be covering the use of touch and mkdir commands.


touch

touch command is used to create an empty file if not exists.

Syntax

touch [optional flags] [filenames]

Usage

$ pwd
/home/subin

$ ls Documents
test_doc.txt

# This creates file inside Documents directory
$ touch Documents/another_file.txt

$ ls Documents
another_file.txt
test_doc.txt
  • touch [multiple files] - touch command can be also used to create multiple files at once.
    $ pwd
    /home/subin
    
    $ cd Documents
    
    $ pwd 
    /home/subin/Documents
    
    # Creating multiple files at once
    $ touch file1 file2 yet_another_file
    
    $ ls
    another_file.txt
    file1
    file2
    test_doc.txt
    yet_another_file
    

mkdir

mkdir stands for Make Directory. As the name suggests, it is used to create an directory.

Syntax

mkdir [optional flags] [directories]

Usage

$ pwd
/home/subin

$ ls
Desktop
Documents
Downloads
Music
Pictures

# This creates an empty directory
$ mkdir test_dir

$ ls
Desktop
Documents
Downloads
Music
Pictures
test_dir
  • mkdir [multiple files] - mkdir command can be also used to create multiple directories at once.
    $ pwd
    /home/subin
    
    # Creating multiple directories at once
    $ mkdir dir1 dir2 another_dir
    
    $ ls
    another_dir
    Desktop
    dir1
    dir2
    Documents
    Downloads
    Music
    Pictures
    test_dir
    
  • mkdir -v [directories] - -v or --verbose flag is used to display a status message for each directory passed as an argument.
    $ pwd
    /home/subin
    
    $ ls
    another_dir
    Desktop
    dir1
    dir2
    Documents
    Downloads
    Music
    Pictures
    test_dir
    
    $ mkdir -v new_dir dir1
    mkdir: created directory 'new_dir'
    mkdir: dir1: File exists
    
  • mkdir -p [directories] - This command is used to create parent directories if not exists.
    $ pwd
    /home/subin
    
    # This command will fail since Folder1 doesn't exist
    $ mkdir Documents/Folder1/Folder2
    mkdir: Documents/Folder1: No such file or directory
    
    $ mkdir -p Documents/Folder1/Folder2
    
    $ ls Documents/Folder1
    Folder2
    

What’s next

I hope this article gave you an insight on creating files and directory. In next part of this series, we will see basic file operations.


Note: All examples above are based on below imaginary directory structure.

.
├── home
│   └── subin
│       ├── .bashrc
│       ├── .config
│       ├── Downloads
│       │   └── movie
│       │       └── some_movie.mp4
│       ├── Desktop
│       ├── Documents
│       │   └── test_doc.txt
│       ├── Music
│       └── Pictures
├── lib
├── opt
└── tmp