Say hi
all posts

Part 3 [touch & mkdir] - Know your commands

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

bash
touch [optional flags] [filenames]

Usage

bash
$ pwd/home/subin$ ls Documentstest_doc.txt# This creates file inside Documents directory$ touch Documents/another_file.txt$ ls Documentsanother_file.txttest_doc.txt
  • touch [multiple files] - touch command can be also used to create multiple files at once.
    bash
    $ pwd/home/subin$ cd Documents$ pwd /home/subin/Documents# Creating multiple files at once$ touch file1 file2 yet_another_file$ lsanother_file.txtfile1file2test_doc.txtyet_another_file

mkdir

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

Syntax

bash
mkdir [optional flags] [directories]

Usage

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

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.

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

That’s the whole Know your commands series.