Directories: Change Directory, Print Working Directory, Make Directory
Linux Directories: cd (Change Directory), pwd (Print Working Directory), mkdir (Make Directory) are a fundamental skill for any user. Whether you’re a beginner or someone looking to brush up on the basics, understanding how to move between directories, check your current location, and create new directories is crucial.
Introduction
In the world of Linux, navigating the filesystem and managing directories is very imporant skill. In this tutorial, we’ll cover three essential Linux commands: cd
, pwd
, and mkdir
. These commands form the foundation of directory navigation and organization in a Linux environment, and mastering them will help you become more comfortable working with the terminal. Let’s dive into how each command works and how you can use them effectively.
Navigate directories: pwd / cd
To print current directory:
pwd
To navigate directories and change the current working directory:
cd directory_name
List of Files and Directories: ls
List content fo directories:
ls
Output:
appl bin boot dev etc home install lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
Long listing:
ls -l
Output:
root@nginx:/# ls -l
total 2097236
lrwxrwxrwx 1 root root 7 Apr 22 2024 bin -> usr/bin
drwxr-xr-x 2 root root 4096 Feb 26 2024 bin.usr-is-merged
drwxr-xr-x 4 root root 4096 Jan 15 13:21 boot
dr-xr-xr-x 2 root root 4096 Apr 23 2024 cdrom
drwxr-xr-x 20 root root 4100 Jan 16 13:19 dev
drwxr-xr-x 113 root root 4096 Jan 15 13:20 etc
drwxr-xr-x 3 root root 4096 Aug 8 09:05 home
lrwxrwxrwx 1 root root 7 Apr 22 2024 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Apr 22 2024 lib64 -> usr/lib64
drwxr-xr-x 2 root root 4096 Feb 26 2024 lib.usr-is-merged
drwx------ 2 root root 16384 Aug 8 07:14 lost+found
drwxr-xr-x 2 root root 4096 Apr 23 2024 media
drwxr-xr-x 2 root root 4096 Apr 23 2024 mnt
drwxr-xr-x 2 root root 4096 Apr 23 2024 opt
dr-xr-xr-x 220 root root 0 Jan 16 13:19 proc
drwx------ 4 root root 4096 Sep 13 12:37 root
drwxr-xr-x 27 root root 860 Jan 16 13:20 run
lrwxrwxrwx 1 root root 8 Apr 22 2024 sbin -> usr/sbin
drwxr-xr-x 2 root root 4096 Apr 3 2024 sbin.usr-is-merged
drwxr-xr-x 2 root root 4096 Aug 8 09:05 snap
drwxr-xr-x 2 root root 4096 Apr 23 2024 srv
-rw------- 1 root root 2147483648 Aug 8 07:30 swap.img
dr-xr-xr-x 13 root root 0 Jan 16 13:19 sys
drwxrwxrwt 12 root root 4096 Jan 16 13:20 tmp
drwxr-xr-x 12 root root 4096 Apr 23 2024 usr
drwxr-xr-x 14 root root 4096 Sep 13 10:54 var
If you need to check hidden files execute the following command:
ls -a
mkdir (make directory)
The basic syntax of the mkdir
command is as follows:
mkdir [OPTION]... DIRECTORY...
OPTION
: Represents various flags that alter the command’s behavior.DIRECTORY
: Specifies the name(s) of the directory(ies) you wish to create.
Basic Usage
Create directory:
mkdir directory_name
Useful Options with mkdir
-p
(Parents)
mkdir -p parent/child/grandchild
This command creates the parent
, child
, and grandchild
directories in one go.
-v
(Verbose)
mkdir -v newdir
Output:
mkdir: created directory 'newdir'
- –
m
(Mode)
mkdir -m 755 newdir
This command creates newdir
with permissions set to 755
(rwxr-xr-x).
rm directory (remove directory)
The basic syntax of the rm
command is as follows:
rm [OPTION]... FILE...
OPTION
: Represents various flags that alter the command’s behavior.FILE
: Specifies the name(s) of the file(s) or directory(ies) you wish to delete.
Basic Usage
rm file.txt
Useful options with rm
-r
(Recursive)
# Remove directory and content
rm -r directory
-f
(Force)
rm -f file.txt
The -f
option forces the removal of files or directories without prompting for confirmation. This is useful for scripts or automated tasks.
-i
(Interactive)
The -i
option prompts for confirmation before each file is deleted. For example:
rm -i file1.txt file2.txt
This command prompts you before deleting each file.