working with directories

Working with directories in Linux

WORKING WITH DIRECTORIES IN LINUX

Files in a Linux systems are arranged in hierarchical directory structure. The root directory consists of files and subdirectories, which contain more files and subdirectories
A tree like pattern of directories we call it as folder in windows operating system. Which may contain files and other directories.

Current working directory is the directory in which the user is currently working in shell.when you log into the Linux system, your current working directory will be home directory.

In this article we will learn some basic Linux commands to work with directories

Working with directory
Working with directory in linux system
  1. PWD(Print working directory)

PWD command is used to print the name of the current or working directory in Linux and Unix systems.PWD command will print full path of the current working directory

pwd [OPTION]…
[pavan@master ~]$ pwd
/home/pavan

We can use -L(Logical) and -P(physical) attributes of PWD command

-L is for showing symlinks inside the directories

-P is for avoid all symlinks.

2.CD (Change directory)

CD command we used to change the current or working directory.

[pavan@master ~]$ cd /opt/
[pavan@master opt]$ pwd
/opt
[pavan@master opt]$ cd /sbin/
[pavan@master sbin]$ pwd
/sbin
[pavan@master sbin]$ cd /home/pavan/
[pavan@master ~]$ pwd
/home/pavan

2.1 We can use cd as shortcut to get back into the home directory. Just type cd Togo into your home directory

[pavan@master ~]$ cd /sbin/
[pavan@master sbin]$ pwd
/sbin
[pavan@master sbin]$ cd
[pavan@master ~]$ ls
[pavan@master ~]$ cd /sbin/
Alternate you can use cd ~ for home directory
[pavan@master sbin]$ cd ~
[pavan@master ~]$

2.2 (cd -) go to last path of your directory

[pavan@localhost share]$ cd /
[pavan@localhost /]$ cd -
/usr/share
[pavan@localhost share]$

2.3 (cd ..) command is used to go, just above your current directory in the directory tree.

[pavan@localhost share]$ pwd
/usr/share
[pavan@localhost share]$ cd ..
[pavan@localhost usr]$ pwd
/usr

3.ls :- List information about the Files (the current directory by default).

[pavan@localhost init.d]$ ls
functions netconsole network README

3.1 ls -l or ll :- This command will give you output in long listing.

[pavan@localhost init.d]$ ls -l
total 40
-rw-r--r--. 1 root root 18104 Jan 2 2018 functions
-rwxr-xr-x. 1 root root 4334 Jan 2 2018 netconsole
-rwxr-xr-x. 1 root root 7293 Jan 2 2018 network
-rw-r--r--. 1 root root 1160 Apr 11 2018 README
[pavan@localhost init.d]$ ll
total 40
-rw-r--r--. 1 root root 18104 Jan 2 2018 functions
-rwxr-xr-x. 1 root root 4334 Jan 2 2018 netconsole
-rwxr-xr-x. 1 root root 7293 Jan 2 2018 network
-rw-r--r--. 1 root root 1160 Apr 11 2018 README
[pavan@localhost init.d]$

3.2 ls -lh :- It will show the output in human-readable format.

[pavan@localhost init.d]$ ls -lh
total 40K
-rw-r--r--. 1 root root 18K Jan 2 2018 functions
-rwxr-xr-x. 1 root root 4.3K Jan 2 2018 netconsole
-rwxr-xr-x. 1 root root 7.2K Jan 2 2018 network
-rw-r--r--. 1 root root 1.2K Apr 11 2018 README
[pavan@localhost init.d]$

3.3 ls -a :- This command will show all files in a directory including hidden, symlinks.

[pavan@localhost init.d]$ ls
functions netconsole network README
[pavan@localhost init.d]$ ls -a
. .. functions netconsole network README
[pavan@localhost init.d]$

4. mkdir :- This command is work on all operating system including windows, Unix, Linux, mac.

[pavan@localhost ~]$ mkdir folder
[pavan@localhost ~]$ ls
Desktop Documents Downloads folder Music Pictures Public Templates Videos
[pavan@localhost ~]$ cd folder/
[pavan@localhost folder]$ ls
[pavan@localhost folder]$

4.1 mkdir -p :- This command is used to create sub-directories of a directory. It will create parent directory first, if it doesn’t exist.

The following command will fail, because the parent directory of test1 does not exit.

[pavan@localhost ~]$ mkdir folder/test1/test2
mkdir: cannot create directory ‘folder/test1/test2’: No such file or directory
[pavan@localhost ~]$

If we use same command with option -P then mkdir will create parent directories as needed.

[pavan@localhost ~]$ mkdir -p folder/test1/test2
[pavan@localhost ~]$ ls -l folder/test1/
total 0
drwxrwxr-x. 2 pavan pavan 6 Aug 19 12:23 test2
[pavan@localhost ~]$

5.rmdir :- This command is used to remove empty directory

[pavan@localhost ~]$ cd folder/test1/
[pavan@localhost test1]$ ls
test2
[pavan@localhost test1]$ rmdir test2/
[pavan@localhost test1]$ ls
[pavan@localhost test1]$

5.1 rmdir -p :- This command is used to remove directories recursively.

[pavan@localhost ~]$ rmdir -p folder/test1/
[pavan@localhost ~]$

What Is A Path in linux?

The location of a file or directory from root (/) directory is known as the path of that file or directory.

Difference between absolute path and relative path in Linux

Absolute Path

An absolute path is a complete path from start of actual file system from root (/) directory in command prompt.

[pavan@localhost ~]$ ls /usr/share/nano/asm.nanorc
[pavan@localhost ~]$ ls /etc/sys/sysctl.conf
[pavan@localhost ~]$ ls /etc/system-release

Relative Path

Relative path is defined as path related to the present working directory.This is same as the path where we are running the commands without accessing actual location of the file or scripts.

pavan@localhost bin]$ ./script

Also, See :- Useful Linux Commands

Linux1

Linux2

Follow my blog with Bloglovin

Leave a Reply