Getting touch with Linux commands (part 2)

From a previous article “Getting touch with Linux commands (part 1)“, we could learn about some basic commands for listing files, showing the current directory, changing directories, viewing the contents of files such things. From this article we will go abroad of that.

When you are working in a Linux operating system, you must know the way of creating a new directory, how to copy files to that directory or remove files in that directory and how to move a file from one place to another place.

Here, we will learn how to work with directories in details.

1) mkdir : Create a new directory ( similarly a new folder in windows )

syntax : mkdir <directory name >

What is mkdir ? In linux, it allows users to create a single or multiple directories with mkdir command.

eg : mkdir myFiles

If you want to check with more details, you can go with ls -ld myFiles as shown below.

What is the difference in between ls -ld myFiles and ls -l myFiles. It will be shown below.

There you can see the difference. We use ls -ld myFiles to view details of the directory itself. So in this case it will show details of the directory “myFiles”. But if we use ls -l myFiles, there you are asking “Show me details about the files in the directory myFiles”. Simply we use ls command for listing files, with -l option to view details of the files and with -d option it shows details of the directory.

How to create multiple directories in a single command

syntax : mkdir <directory name1> <directory name2> <directory name3>

Same as creating a single directory, we can create multiple directories giving their names along with the command.

In the 1st images, it shows 6 directories has created. In the 2nd images, it shows two new directories is created with names subjects and marks in “foo1” directory.  So that is done with a single command.

What is a parent directory ? How to create a parent directory ?

Parent directory is the parent of the child directories. Similarly as in the above image, “foo1” is the parent directory of marks and subject directories.

syntax : mkdir -p <directory name1> <directory name2> <directory name3>

What’s the easiness with above command. Check the below scenario.

In foo2 directory, there were zero files. Simply its blank. mkdir foo2/grade1/students/names command tries to create a directory set inside of foo2 as a tree. But to create names, there should be the students directory. To create students directory there should be grade1 directory.
So, here is the place we meet parent directories. In order to create a child directory, it should be have it’s parent directory.
mkdir -p command let you create all the parent directories and child directories with a single command. It is shown in the above command.

How to remove a directory ? 

syntax : rm -r <directory name>

eg : rm -r foo5

* You can’t remove a directory with just rm command. Because it doesn’t allow to remove it as it is a directory.

As well you can remove a directory tree in a single command.

eg : rm -r foo2/grade1

Above will remove all the directories and sub-directories of grade1 with itself.

** And if you need to remove all directories in the present directory, you can use rm -rf <foo1> <foo2> <foo3>

How to remove a file ?

syntax : rm <file1>

How to remove multiple files ?

syntax : rm -f <file1> <file2> <file3>

*** If you want to check for in-built help for creating directories and removing Use man mkdir, man rm. 

This article covers mostly useful options for working with directories. Please refer man pages. Learn more.

Other Linux Commands resources


Leave a Reply