Linux commands


Linux commands are case sensitive, it means ls is different from LS

Windows -> In Windows we create the folder and inside folder we can save the files
Linux -> Same way In Linux we create the directory and save the files

Windows -> folder
Linux -> Directory

Clear: By help of this command we can clear the screen.
Note: All Linux commands are lower case only.

Syntax: mkdir directory_name

Example:
mkdir July23rd2022

[ec2-user@ip-172-31-38-72 ~]$ mkdir july25th2022
[ec2-user@ip-172-31-38-72 ~]$ ls
Ansar Basha July23rd2022 july25th2022
[ec2-user@ip-172-31-38-72 ~]$

We can create multiple directories
Example:
mkdir dir1 dir2 dir3

Options or flag:

-p
-v
-m

-p -> we can create parent directory by using -p option

mkdir -p Test1/Test2/Test3 -> Hit the enter button in keyboard

-v -> verbos -> Display the information while creating directory
mkdir -v sample
mkdir: created directory ‘sample’

[ec2-user@ip-172-31-45-39 ~]$ mkdir ansar
[ec2-user@ip-172-31-45-39 ~]$ mkdir -v Ram
mkdir: created directory ‘Ram’
[ec2-user@ip-172-31-45-39 ~]$

[ec2-user@ip-172-31-45-39 ~]$ mkdir -pv t1/t2/t3
mkdir: created directory ‘t1’
mkdir: created directory ‘t1/t2’
mkdir: created directory ‘t1/t2/t3’
[ec2-user@ip-172-31-45-39 ~]$

-m -> mode -> We can create the directory with specific permission
[ec2-user@ip-172-31-45-39 ~]$ mkdir -m 777 mydir
[ec2-user@ip-172-31-45-39 ~]$ ls -l
drwxrwxrwx. 2 ec2-user ec2-user 6 Jan 5 15:16 mydir
[ec2-user@ip-172-31-45-39 ~]$

Syntax: rmdir directory_name

ec2-user@ip-172-31-45-39 ~]$ ls
ansar Jan5th2023 mydir Ram t1 Test1
[ec2-user@ip-172-31-45-39 ~]$ rmdir ansar -> This command will delete the empty directory
[ec2-user@ip-172-31-45-39 ~]$ ls
Jan5th2023 mydir Ram t1 Test1
[ec2-user@ip-172-31-45-39 ~]$

[ec2-user@ip-172-31-45-39 ~]$ rmdir Test1
rmdir: failed to remove ‘Test1’: Directory not empty
[ec2-user@ip-172-31-45-39 ~]$

Options:
r – recursivily
f – forcefully

[ec2-user@ip-172-31-45-39 ~]$ rm mydir
rm: cannot remove ‘mydir’: Is a directory
[ec2-user@ip-172-31-45-39 ~]$ rm -rf mydir/
[ec2-user@ip-172-31-45-39 ~]$

[ec2-user@ip-172-31-45-39 ~]$ rmdir Test1/
rmdir: failed to remove ‘Test1/’: Directory not empty
[ec2-user@ip-172-31-45-39 ~]$ rm -rf Test1/
[ec2-user@ip-172-31-45-39 ~]$ ls -l
total 0
drwxr-xr-x. 5 ec2-user ec2-user 43 Jan 5 14:59 Jan5th2023
drwxr-xr-x. 3 ec2-user ec2-user 16 Jan 5 15:13 t1
[ec2-user@ip-172-31-45-39 ~]$

How to install the package of a tree command?

Yum install tree -> By using this command we can install the package.

YUM -> Yellow dog Updater, Modified

We have two users:

Normal user -> Less Privileges (permissions)
Admin user -> More Privileges (permissions)
In windows Admin user -> In Linux we called the root user

$ -> Means normal user
-> Means admin/root user

How to login as a root user?

sudo su –

sudo -> super user do
su -> switch user
[ec2-user@ip-172-31-45-39 ~]$ sudo su –
Last login: Thu Jan 5 15:05:19 UTC 2023 on pts/0
[root@ip-172-31-45-39 ~]# exit
logout
[ec2-user@ip-172-31-45-39 ~]$

Exit -> come out from root user.

Syntax : pwd

[ec2-user@ip-172-31-45-39 ~]$ pwd
/home/ec2-user

Syntax: cd Directory_name

~ -> user home directory

cd .. -> One step back
cd ../.. -> Two steps back
cd ~ or cd -> It will take to us to user home directory

[ec2-user@ip-172-31-45-39 ~]$ pwd
/home/ec2-user
[ec2-user@ip-172-31-45-39 ~]$ ls ( it list the all directories )
Jan5th2023 t1
[ec2-user@ip-172-31-45-39 ~]$ cd t1
[ec2-user@ip-172-31-45-39 t1]$ ls
t2
[ec2-user@ip-172-31-45-39 t1]$ cd t2
[ec2-user@ip-172-31-45-39 t2]$ ls
t3
[ec2-user@ip-172-31-45-39 t2]$ cd t3
[ec2-user@ip-172-31-45-39 t3]$ ls
[ec2-user@ip-172-31-45-39 t3]$ pwd
/home/ec2-user/t1/t2/t3
[ec2-user@ip-172-31-45-39 t3]$ mkdir t4
[ec2-user@ip-172-31-45-39 t3]$ cd t4
[ec2-user@ip-172-31-45-39 t4]$ pwd
/home/ec2-user/t1/t2/t3/t4
[ec2-user@ip-172-31-45-39 t4]$ cd ~
[ec2-user@ip-172-31-45-39 ~]$ pwd
/home/ec2-user
[ec2-user@ip-172-31-45-39 ~]$ cd
[ec2-user@ip-172-31-45-39 ~]$ cd /home/ec2-user/t1/t2/t3/t4
[ec2-user@ip-172-31-45-39 t4]$ pwd
/home/ec2-user/t1/t2/t3/t4
[ec2-user@ip-172-31-45-39 t4]$

Syntax: ls or ls filename

ls directory_name -> it will display all files or directories in side mention dirctory

options:

l -> list the long format

a -> all files

. or .. are hidden files

 Example : .bashrc .ansar

i -> inode number

  inode is Data structure, it will store file information. size of the file, file creation date and time and it won’t store the file name.

t -> time stamp  -> combination of date and time

r -> reverse

[ec2-user@ip-172-31-45-39 ~]$ ls

Jan5th2023  t1

[ec2-user@ip-172-31-45-39 ~]$

[ec2-user@ip-172-31-45-39 ~]$ ls -l

total 0

drwxr-xr-x. 6 ec2-user ec2-user 56 Jan  6 15:05 Jan5th2023

drwxr-xr-x. 3 ec2-user ec2-user 16 Jan  5 15:13 t1

[ec2-user@ip-172-31-45-39 ~]$ ls Jan5th2023/

Bindu  dir1  dir2  mydir

[ec2-user@ip-172-31-45-39 ~]$

[ec2-user@ip-172-31-45-39 ~]$ ls -a

.  ..  .bash_logout  .bash_profile  .bashrc  Jan5th2023  .ssh  t1

[ec2-user@ip-172-31-45-39 ~]$

[ec2-user@ip-172-31-45-39 ~]$ ls -la

total 12

drwx——. 5 ec2-user ec2-user 116 Jan  6 15:31 .

drwxr-xr-x. 3 root     root      22 Jan  4 14:56 ..

-rw-r–r–. 1 ec2-user ec2-user   0 Jan  6 15:31 .ansar

-rw-r–r–. 1 ec2-user ec2-user  18 Aug  8 13:07 .bash_logout

-rw-r–r–. 1 ec2-user ec2-user 141 Aug  8 13:07 .bash_profile

-rw-r–r–. 1 ec2-user ec2-user 492 Aug  8 13:07 .bashrc

drwxr-xr-x. 6 ec2-user ec2-user  56 Jan  6 15:05 Jan5th2023

drwx——. 2 ec2-user ec2-user  29 Jan  4 14:56 .ssh

drwxr-xr-x. 3 ec2-user ec2-user  16 Jan  5 15:13 t1

[ec2-user@ip-172-31-45-39 ~]$

[ec2-user@ip-172-31-45-39 ~]$ ls -li

total 0

 8399788 drwxr-xr-x. 6 ec2-user ec2-user 56 Jan  6 15:05 Jan5th2023

17383424 drwxr-xr-x. 3 ec2-user ec2-user 16 Jan  5 15:13 t1

[ec2-user@ip-172-31-45-39 ~]$

[ec2-user@ip-172-31-45-39 ~]$ ls -l

total 0

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:37 a

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:37 b

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:37 c

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:37 d

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:37 e

drwxr-xr-x. 6 ec2-user ec2-user 56 Jan  6 15:05 Jan5th2023

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:38 n

drwxr-xr-x. 3 ec2-user ec2-user 16 Jan  5 15:13 t1

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:40 zam

[ec2-user@ip-172-31-45-39 ~]$ ls -lt

total 0

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:40 zam

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:38 n

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:37 a

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:37 b

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:37 c

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:37 d

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:37 e

drwxr-xr-x. 6 ec2-user ec2-user 56 Jan  6 15:05 Jan5th2023

drwxr-xr-x. 3 ec2-user ec2-user 16 Jan  5 15:13 t1

[ec2-user@ip-172-31-45-39 ~]$

[ec2-user@ip-172-31-45-39 ~]$ ls -lr

total 0

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:40 zam

drwxr-xr-x. 3 ec2-user ec2-user 16 Jan  5 15:13 t1

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:38 n

drwxr-xr-x. 6 ec2-user ec2-user 56 Jan  6 15:05 Jan5th2023

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:37 e

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:37 d

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:37 c

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:37 b

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:37 a

[ec2-user@ip-172-31-45-39 ~]$

[ec2-user@ip-172-31-45-39 ~]$ ls -ltr

total 0

drwxr-xr-x. 3 ec2-user ec2-user 16 Jan  5 15:13 t1

drwxr-xr-x. 6 ec2-user ec2-user 56 Jan  6 15:05 Jan5th2023

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:37 e

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:37 d

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:37 c

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:37 b

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:37 a

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:38 n

drwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:40 zam

[ec2-user@ip-172-31-45-39 ~]$

d rwxr-xr-x. 2 ec2-user ec2-user  6 Jan  6 15:37 a

[ec2-user@ip-172-31-45-39 Linuxpractice]$ ls -l Jan9th2023

-rw-r–r–. 1 ec2-user ec2-user 69 Jan  9 15:36 Jan9th2023

 r -> Read Permission

 w -> Write Permission

 x -> Executeble Permission

– -> Normal file

d -> directory

[ec2-user@ip-172-31-38-72 Linuxpractice]$ ls -ltr

total 4

drwxrwxr-x. 3 ec2-user ec2-user 15 Jul 26 15:55 unix

-rw-rw-r–. 1 ec2-user ec2-user 23 Jul 27 14:56 myfile

– rw-rw-r–. 1 ec2-user ec2-user  0 Jul 27 15:12 xyz.txt

– rw-rw-r–. 1 ec2-user ec2-user  0 Jul 27 15:17 abc

[ec2-user@ip-172-31-38-72 Linuxpractice]$

   dir/file permissions  group owner of file file creation time

d rwxrwxr-x. 3 ec2-user ec2-user 69 Jul 27 15:45 Linuxpractice

            links owner          size of the file file/dir name

drwxrwxr-x. -> dir/file permissions

 3   -> links

 ec2-user ->  owner of file/dir

 ec2-user -> group owner of file/dir

 69  -> size of the file/dir

 Jul 27 15:45 -> file/dir creation date and time

 Linuxpractice -> file/dir name

R -> Read

W -> Write

x -> Executable

           

cat

touch

vi editor

cat > filename

[ec2-user@ip-172-31-45-39 Linuxpractice]$ cat > Jan9th2023

Hi Guys,

Linux is very easy if you practice every day

[ec2-user@ip-172-31-45-39 Linuxpractice]$ cat Jan9th2023

Hi Guys,

Linux is very easy if you practice every day

[ec2-user@ip-172-31-45-39 Linuxpractice]$

[ec2-user@ip-172-31-45-39 Linuxpractice]$ cat < Jan9th2023

Hi Guys,

Linux is very easy if you practice every day

[ec2-user@ip-172-31-45-39 Linuxpractice]$

Example:

[ec2-user@ip-172-31-45-39 Linuxpractice]$ cat >> Jan9th2023

Hello guys

[ec2-user@ip-172-31-45-39 Linuxpractice]$ cat Jan9th2023

Hi Guys,

Linux is very easy if you practice every day

Hello guys

[ec2-user@ip-172-31-45-39 Linuxpractice]$

Syntax:

touch filename

Syntax for Multiple file creation:

touch file1 file2 file3 -> create the multiple files.

touch {a..z } -> Multiple creation

[ec2-user@ip-172-31-45-39 Linuxpractice]$ touch Jan9th2023_1  -> Single file creation

[ec2-user@ip-172-31-45-39 Linuxpractice]$ ls -l

total 4

-rw-r–r–. 1 ec2-user ec2-user 54 Jan  9 15:21 Jan9th2023

-rw-r–r–. 1 ec2-user ec2-user  0 Jan  9 15:26 Jan9th2023_1

[ec2-user@ip-172-31-45-39 Linuxpractice]$ ls -l Jan9th2023_1

-rw-r–r–. 1 ec2-user ec2-user 0 Jan  9 15:26 Jan9th2023_1

[ec2-user@ip-172-31-45-39 Linuxpractice]$ touch t1 t2 t3 t4 t5  -> Multiple file creation

[ec2-user@ip-172-31-45-39 Linuxpractice]$ ls -l

total 4

-rw-r–r–. 1 ec2-user ec2-user 54 Jan  9 15:21 Jan9th2023

-rw-r–r–. 1 ec2-user ec2-user  0 Jan  9 15:26 Jan9th2023_1

-rw-r–r–. 1 ec2-user ec2-user  0 Jan  9 15:28 t1

-rw-r–r–. 1 ec2-user ec2-user  0 Jan  9 15:28 t2

-rw-r–r–. 1 ec2-user ec2-user  0 Jan  9 15:28 t3

-rw-r–r–. 1 ec2-user ec2-user  0 Jan  9 15:28 t4

-rw-r–r–. 1 ec2-user ec2-user  0 Jan  9 15:28 t5

[ec2-user@ip-172-31-45-39 Linuxpractice]$

[ec2-user@ip-172-31-45-39 Linuxpractice]$ touch {a..z} -> It will create 26 files

[ec2-user@ip-172-31-45-39 Linuxpractice]$ touch {1..1000} -> It will create 1000 files

Note: If we touch the existing file, the file time stamp will update.

[ec2-user@ip-172-31-45-39 Linuxpractice]$ ls -l Jan9th2023

-rw-r–r–. 1 ec2-user ec2-user 69 Jan  9 15:36 Jan9th2023

[ec2-user@ip-172-31-45-39 Linuxpractice]$ touch Jan9th2023

[ec2-user@ip-172-31-45-39 Linuxpractice]$ ls -l Jan9th2023

-rw-r–r–. 1 ec2-user ec2-user 69 Jan  9 15:43 Jan9th2023

[ec2-user@ip-172-31-45-39 Linuxpractice]$

In vi editor we have two modes

1.Command mode
2.Insert mode

Syntax: vi filename

How to create the file in vi editor?

vi file_name
file is opend in command mode only
then we have press i button in keyboard it will take to us Insert mode
Allow to enter the data in file
press esc button in keyboard
shift:wq
w – save
q- quit

step1: vi filename
step2: command mode
step3: Press I button in key board -> Insert mode
step4: Enter the data in a file
step5: Press Esc
step6: shift+:wq

echo $?: By help of this commands we can check previous command or script ran successfully or not
0 means -> success
morethan 0 means -> un-success

touch {a..z} -> It will creat the 26 files
touch {1..7}

rm : By help this command we can remove files/directories dirs.
we can remove/delete the multiple files aswell.
r – recursivily
f – forcefully

rm {a..z} -> It will delete the all 26 files
rm {1..7} -> It will delete the all 7 files

rm -rf a* -> letter a starts Files will deleted
rm -rf ? -> It will delete the single name file
rm -rf f* -> It will delete all files start with f

  • -> Anyting or multiple characters
    ? -> Single character

rm {a..f}
rm {a..z} -> we can remove the all files from a to z
rm *.txt -> to remove .txt files
rm ??? -> It will delete 3 characters file

[ec2-user@ip-172-31-38-72 Linuxpractice]$ rm -rf a*
[ec2-user@ip-172-31-38-72 Linuxpractice]$ ls -l
total 8
-rw-rw-r–. 1 ec2-user ec2-user 0 Jul 28 15:25 file1.csv
-rw-rw-r–. 1 ec2-user ec2-user 0 Jul 28 15:26 file2.txt
-rw-rw-r–. 1 ec2-user ec2-user 19 Jul 28 15:43 firstfile.txt
-rw-rw-r–. 1 ec2-user ec2-user 0 Jul 29 14:49 name1
-rw-rw-r–. 1 ec2-user ec2-user 0 Jul 29 14:49 name2
-rw-rw-r–. 1 ec2-user ec2-user 0 Jul 29 14:49 name3
-rw-rw-r–. 1 ec2-user ec2-user 23 Jul 28 15:31 xyz.txt
[ec2-user@ip-172-31-38-72 Linuxpractice]$ rm -rf *.txt
[ec2-user@ip-172-31-38-72 Linuxpractice]$ ls -l
total 0
-rw-rw-r–. 1 ec2-user ec2-user 0 Jul 28 15:25 file1.csv
-rw-rw-r–. 1 ec2-user ec2-user 0 Jul 29 14:49 name1
-rw-rw-r–. 1 ec2-user ec2-user 0 Jul 29 14:49 name2
-rw-rw-r–. 1 ec2-user ec2-user 0 Jul 29 14:49 name3
[ec2-user@ip-172-31-38-72 Linuxpractice]$

[ec2-user@ip-172-31-38-72 Linuxpractice]$ ls -l
total 0
-rw-rw-r–. 1 ec2-user ec2-user 0 Jul 28 15:25 file1.csv
-rw-rw-r–. 1 ec2-user ec2-user 0 Jul 29 14:49 name1
-rw-rw-r–. 1 ec2-user ec2-user 0 Jul 29 14:49 name2
-rw-rw-r–. 1 ec2-user ec2-user 0 Jul 29 14:49 name3
[ec2-user@ip-172-31-38-72 Linuxpractice]$ rm -rf n????
[ec2-user@ip-172-31-38-72 Linuxpractice]$ ls -l
total 0
-rw-rw-r–. 1 ec2-user ec2-user 0 Jul 28 15:25 file1.csv
[ec2-user@ip-172-31-38-72 Linuxpractice]$

[ec2-user@ip-172-31-45-39 Linuxpractice]$ ls -l
total 4
-rw-r–r–. 1 ec2-user ec2-user 0 Jan 11 15:31 abc.txt
-rw-r–r–. 1 ec2-user ec2-user 0 Jan 11 15:29 b.java
-rw-r–r–. 1 ec2-user ec2-user 0 Jan 11 15:29 c.csv
-rw-r–r–. 1 ec2-user ec2-user 0 Jan 11 15:29 d.xlsx
-rw-r–r–. 1 ec2-user ec2-user 69 Jan 9 15:43 Jan9th2023
-rw-r–r–. 1 ec2-user ec2-user 0 Jan 9 15:26 Jan9th2023_1
-rw-r–r–. 1 ec2-user ec2-user 0 Jan 11 15:33 xyz
[ec2-user@ip-172-31-45-39 Linuxpractice]$ rm -rf ???
[ec2-user@ip-172-31-45-39 Linuxpractice]$ rm -rf ???.txt
[ec2-user@ip-172-31-45-39 Linuxpractice]$ ls -lrt
total 4
-rw-r–r–. 1 ec2-user ec2-user 0 Jan 9 15:26 Jan9th2023_1
-rw-r–r–. 1 ec2-user ec2-user 69 Jan 9 15:43 Jan9th2023
-rw-r–r–. 1 ec2-user ec2-user 0 Jan 11 15:29 b.java
-rw-r–r–. 1 ec2-user ec2-user 0 Jan 11 15:29 d.xlsx
-rw-r–r–. 1 ec2-user ec2-user 0 Jan 11 15:29 c.csv
[ec2-user@ip-172-31-45-39 Linuxpractice]$

Syntax: cp filename directory_name
or cp /homedirectory/filename /homedirectory/directory_name or cp filename filename -> it will copy the data

[ec2-user@ip-172-31-45-39 Linuxpractice]$ ls -l
total 4
drwxr-xr-x. 2 ec2-user ec2-user 20 Jan 11 15:41 backup_data
-rw-r–r–. 1 ec2-user ec2-user 0 Jan 11 15:29 b.java
-rw-r–r–. 1 ec2-user ec2-user 0 Jan 11 15:29 c.csv
-rw-r–r–. 1 ec2-user ec2-user 0 Jan 11 15:29 d.xlsx
-rw-r–r–. 1 ec2-user ec2-user 69 Jan 9 15:43 Jan9th2023
-rw-r–r–. 1 ec2-user ec2-user 0 Jan 9 15:26 Jan9th2023_1
[ec2-user@ip-172-31-45-39 Linuxpractice]$ pwd
/home/ec2-user/Linuxpractice
[ec2-user@ip-172-31-45-39 Linuxpractice]$ cp /home/ec2-user/Linuxpractice/c.csv /home/ec2-user/Linuxpractice/backup_data/
[ec2-user@ip-172-31-45-39 Linuxpractice]$ cd backup_data/
[ec2-user@ip-172-31-45-39 backup_data]$ ls
b.java c.csv
[ec2-user@ip-172-31-45-39 backup_data]$ cd ..
[ec2-user@ip-172-31-45-39 Linuxpractice]$ cp d.xlsx backup_data/
[ec2-user@ip-172-31-45-39 Linuxpractice]$ cd backup_data/
[ec2-user@ip-172-31-45-39 backup_data]$ ls
b.java c.csv d.xlsx
[ec2-user@ip-172-31-45-39 backup_data]$

Categories Linux

Leave a Comment