Linux Commands – Working with Directories

Here I will be writing about the basics of working with directories in Linux from the command line. I was quite confused about this when I first started using Linux about 14 years ago, coming from a windows GUI world. After learning about linux , I now know that it is much easier and faster to work from the linux command line than in windows GUI.

Here we will be Discussing how to perform the following actions 

  • Make /Create a directory
  • Rename a directory
  • Change to a specific linux directory  — linux home directory
  • Delete/remove a directory
  • Find the current directory / working directory

mkdir command – Make / Create a directory in Linux

Syntax  : 

mkdir command is used to create /make new directory in linux .

Example 1 To create a single directory under your current directory. 

mkdir example

The above command will create a directory called example under the current working directory

Example 2 To create a single directory in a different location

mkdir /etc/example

The above command will create a new directory under the /etc  directrory. Like this example, you need to provide the full directory location if the directory you want to create is not under your current directory

Example 3 Create multiple directories at one 

mkdir example1 example2 example3

The above command will create 3 directories named directory1,directory2,directory3 under your current working directory

Example 4 Create a directory structure

mkdir -p /dir/sub-dir/sub-sub-dir/

This command will create a directory structure. The -p flag will create the parent directories if they don’t exist. For example , if you use the above command without the -p flag you will get an error message saying that the parent directory doesn’t exist as below

mv command – rename or move a directory in linux

 

Unlike the name suggests-move- mv command can be used to rename a directory.

Example 1  rename a directory under the current directory in linux

mv alice bob

The above command will rename a directory called alice to bob. 

[message title=”Note” title_color=”#db3d32″ title_bg=”#dddddd” title_icon=”fa-exclamation” content_color=”#000000″ content_bg=”#ffffff” id=””]if a directory called bob already exists, instead of renaming , it will move the directory alice in to bob – /bob/alice. To prevent this , use -T flag(–no-target-directory – treat DEST as a normal file) [/message]

  

cd command – Change directory in linux

Syntax

cd <name of dir>

Note : if the directory name contains any space you should use quotes to make sure that you moved in to the correct directory. Also you can use TAB key to complete the name of the directrory , bash will try to autocomplete.

Example 1 : change to direct sub directory(use relative path)

cd dirname

Example 2 : change to a directory that is not a current sub directrory( absolute path)

Cd /var/log/

Example 3 change to current current users home directory

Cd

Cd ~

You can go directly to your home directory using this command – doesn’t matter where your current directory location is . ( ~  symbol is called a tilde )

Example 4 change to the previous directory 

Cd -

This command will take you to the directory you were previously before the last cd command

Example 5 change to parent directory

Cd ..

This will change your working directory to the parent directory of the current directory

Cd /

This will change your working directory to the root directory – doesn’t matter where your current locations is just like like cd ~ command for home directory. Please see the table below to see what symbols are used with cd command.

. Current directory
.. Parent directory
~ Home directory of user
/ Root directory
  Previous directory before the last cd command

How to find your current working directory ?

you can use pwd command to find the current working directory. 

pwd- Print working directory.

 

 

Leave a Comment