preferences
Linux

Working with Linux directories

0 277

In this tutorial we will learn how to work with Linux directories.

We will learn how to know current working directory, switching or moving between directories, listing contents of directories(files/folders) making and removing directories.

We will also discuss about paths. We will talk about absolute and relative paths and path completion in bash.

The commands which we will discuss in this tutorial are available in all Linux versions/distributions.

pwd (present working directory

To know current working in directory use pwd command

[box] [ali@rhel71 ~]$ pwd

/home/ali

[ali@rhel71 ~]$

As I am logged in as ali it is showing that I am in home directory of ali. You can also see ~  sign which means home directory of logged in user

In above command ali is user rehl71 is host name and ~ is home directory of ali.

cd

If you want to change directory you will type cd (change directory)

[box] [ali@rhel71 ~]$ pwd

/home/ali

[ali@rhel71 ~]$ cd /home

[ali@rhel71 home]$ pwd

/home

[ali@rhel71 home]$ cd /bin

[ali@rhel71 bin]$ pwd

/bin

[ali@rhel71 bin]$ cd ~

for example you are in directory /etc/fonts/conf.d and you want to change directory back to home directory you can type cd /home/ali but there is trick just type cd ~ and you will be in your home directory

[box] [ali@rhel71 conf.d]$ pwd

/etc/fonts/conf.d

[ali@rhel71 conf.d]$ cd ~

[ali@rhel71 ~]$ pwd /home/ali

[ali@rhel71 ~]$

cd ..

to move back to parent directory of current working directory or going one level up type cd ..

see example below

[box] [ali@rhel71 ~]$ pwd

/home/ali

[ali@rhel71 ~]$ cd ..

[ali@rhel71 home]$ pwd

/home

[ali@rhel71 home]$

cd –

to go back to previous directory type cd – see example below we first changed directory to home directory  ~. Then we typed cd – to go back to original directory /etc/fonts/conf.d

[box][ali@rhel71 conf.d]$ pwd

/etc/fonts/conf.d

[ali@rhel71 conf.d]$ cd ~

[ali@rhel71 ~]$ pwd

/home/ali

[ali@rhel71 ~]$ cd –

/etc/fonts/conf.d

[ali@rhel71 conf.d]$ pwd

/etc/fonts/conf.d

[ali@rhel71 conf.d]$

Absolute and relative path

In operating system a unique location to a file or folder in a file system is called a path.

A path to file or folder is combination of slash(s) / and alphanumeric folder(s) name and file(s) name

[box]E,g /etc/fonts/conf.d

/var/log/cups

/home/ali/Pictures

So what is difference between absolute and relative path?

Absolute Path

Absolute path is full path starting from / (root) (base) directory. In other words we can say absolute path is a complete path from start of actual file system from / directory.

When you type / then root of file system is considered as starting point. Full path is like absolute path  e.g

[box]/home/ali/Pictures

/var/log/cups

Relative Path

If you do not start your path with / then current directory is assumed as starting point and it is related to current working directory. Suppose I am in /home/ali  folder and want to change to directory /home/ali/Pictures I can type absolute (full) path by typing

[box]Cd /home/ali/Pictures

or by absolute path typing cd Pictures

[ali@rhel71 ~]$ pwd

/home/ali

[ali@rhel71 ~]$ ls

Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

[box][ali@rhel71 ~]$ cd Pictures

[ali@rhel71 Pictures]$ pwd

/home/ali/Pictures

[ali@rhel71 Pictures]$
Now you can move (navigate) between directories.

Lets start leanring about listing contents in directories

ls

you can list files/directories  in a directory by ls command

[box][ali@rhel71 Pictures]$ ls
Screenshot from 2015-08-09 12:05:52.png

Screenshot from 2015-08-09 12:06:00.png

Screenshot from 2015-08-13 19:18:17.png

Screenshot from 2015-08-13 19:25:31.png

Screenshot from 2015-08-13 19:31:41.png

Screenshot from 2015-08-22 12:38:44.png

ls –a

to list hidden files or directories that start with . type ls –a. in Linux (unix) files or directories starting with . means they are of hidden type
[ali@rhel71 Pictures]$ ls -a
.

..

Screenshot from 2015-08-09 12:05:52.png

Screenshot from 2015-08-09 12:06:00.png

Screenshot from 2015-08-13 19:18:17.png

Screenshot from 2015-08-13 19:25:31.png

Screenshot from 2015-08-13 19:31:41.png

Screenshot from 2015-08-22 12:38:44.png

.shahid

in above listing .shahid is hidden file

ls –l
To display listing of contents in directory in long format type ls –l
[ali@rhel71 Pictures]$ ls -l
total 2224

-rw-rw-r–. 1 ali ali 333905 Aug 9 12:05 Screenshot from 2015-08-09 12:05:52.png

-rw-rw-r–. 1 ali ali 333951 Aug 9 12:06 Screenshot from 2015-08-09 12:06:00.png

-rw-rw-r–. 1 ali ali 56925 Aug 13 19:18 Screenshot from 2015-08-13 19:18:17.png

-rw-rw-r–. 1 ali ali 63885 Aug 13 19:25 Screenshot from 2015-08-13 19:25:31.png

-rw-rw-r–. 1 ali ali 573280 Aug 13 19:31 Screenshot from 2015-08-13 19:31:41.png

-rw-rw-r–. 1 ali ali 27802 Aug 22 12:38 Screenshot from 2015-08-22 12:38:44.png

[ali@rhel71 Pictures]$

above output shows permissions on file group and owner (user ali group ali) size creation date and name of file

we will talk about permissions, user, groups and ownership in future tutorial
ls –lh

To display long listing but display size in human readable format type ls –lh

total 2.2M
-rw-rw-r–. 1 ali ali 327K Aug 9 12:05 Screenshot from 2015-08-09 12:05:52.png

-rw-rw-r–. 1 ali ali 327K Aug 9 12:06 Screenshot from 2015-08-09 12:06:00.png

-rw-rw-r–. 1 ali ali 56K Aug 13 19:18 Screenshot from 2015-08-13 19:18:17.png

-rw-rw-r–. 1 ali ali 63K Aug 13 19:25 Screenshot from 2015-08-13 19:25:31.png

-rw-rw-r–. 1 ali ali 560K Aug 13 19:31 Screenshot from 2015-08-13 19:31:41.png

-rw-rw-r–. 1 ali ali 28K Aug 22 12:38 Screenshot from 2015-08-22 12:38:44.png

[ali@rhel71 Pictures]$

Now lets starting learning creating and removing directories

mkdir

To make new directory type mkdir name of directory

[ali@rhel71 Pictures]$ ls
[ali@rhel71 Pictures]$ ls
[ali@rhel71 Pictures]$ mkdir mydir
[ali@rhel71 Pictures]$ ls
mydir
[ali@rhel71 Pictures]$

mkdir –p

To make directory with subdirectories use switch –p

mkdir –p  directory/subdirectory/subdirectory
[ali@rhel71 Pictures]$ ls
mydir
[ali@rhel71 Pictures]$ mkdir parentDir/childDir/childDir
mkdir: cannot create directory âparentDir/childDir/childDirâ: No such file or directorywithout switch p you can not create child directories. we will now use switch p
[ali@rhel71 Pictures]$ mkdir -p parentDir/childDir/childDir
[ali@rhel71 Pictures]$ ls
mydir parentDir
[ali@rhel71 Pictures]$ cd parentDir
[ali@rhel71 parentDir]$ cd childDir/
[ali@rhel71 childDir]$ cd childDir/
[ali@rhel71 childDir]$ pwd
/home/ali/Pictures/parentDir/childDir/childDir
[ali@rhel71 childDir]$
rmdir
To remove directory type rmdir name of directory

[ali@rhel71 childDir]$ ls
childDir
[ali@rhel71 childDir]$ rmdir childDir/
[ali@rhel71 childDir]$ ls
[ali@rhel71 childDir]$ pwd
/home/ali/Pictures/parentDir/childDir
[ali@rhel71 childDir]$
rmdir –p
To delete entire directory with subdirectories use rmdir –p  directory/subdirectories

[ali@rhel71 childDir]$ pwd
/home/ali/Pictures/parentDir/childDir
[ali@rhel71 childDir]$ cd ../..
[ali@rhel71 Pictures]$ ls
mydir Screenshot from 2015-10-15 10:01:03.png
parentDir Screenshot from 2015-10-15 10:01:09.png
[ali@rhel71 Pictures]$ rmdir -p parentDir/childDir
[ali@rhel71 Pictures]$ ls
mydir

Screenshot from 2015-10-15 10:01:03.png

Screenshot from 2015-10-15 10:01:09.png
[ali@rhel71 Pictures]$

SUBSCRIBE OUR NEWSLETTER
Join us by subscribing to our newsletter and learn IT subjects for free
We hate spam. Your email address will not be sold or shared with anyone else.

Leave a reply

You must be logged in to post a comment.