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.
- Part 1 - pwd & cd
- Part 2 - echo & ls
- Part 3 - touch & mkdir
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 Documentstest_doc.txt# This creates file inside Documents directory$ touch Documents/another_file.txt$ ls Documentsanother_file.txttest_doc.txttouch [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$ 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
mkdir [optional flags] [directories]Usage
$ pwd/home/subin$ lsDesktopDocumentsDownloadsMusicPictures# This creates an empty directory$ mkdir test_dir$ lsDesktopDocumentsDownloadsMusicPicturestest_dirmkdir [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$ lsanother_dirDesktopdir1dir2DocumentsDownloadsMusicPicturestest_dirmkdir -v [directories]--vor--verboseflag is used to display a status message for each directory passed as an argument.$ pwd/home/subin$ lsanother_dirDesktopdir1dir2DocumentsDownloadsMusicPicturestest_dir$ mkdir -v new_dir dir1mkdir: created directory 'new_dir'mkdir: dir1: File existsmkdir -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/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.
.├── home│ └── subin│ ├── .bashrc│ ├── .config│ ├── Downloads│ │ └── movie│ │ └── some_movie.mp4│ ├── Desktop│ ├── Documents│ │ └── test_doc.txt│ ├── Music│ └── Pictures├── lib├── opt└── tmp