You are on page 1of 36

copyright© Binayak Prasad Gupta.

Virtual Machine Needed:

● Kali-Linux.

File and Directory Commands:

● First command: “pwd”

● The pwd command in Linux stands for "Print Working


Directory." When you run the pwd command in a
terminal or shell, it displays the current directory you
are in.
● Example:

● This means that the present directory


is:”/home/kali”.
copyright© Binayak Prasad Gupta.
File Types in Linux:

● Second command: “ls”

● The ls command in Linux is used to list directory


contents. It displays the files and directories in the
specified directory.
● Syntax: “ls [options] <path of directory or file name>”

● Some common options used with “ls”:


i. ls: Simply typing ls lists the files and directories in the
current directory.
ii. ls [directory name]: Lists the files and directories in the
specified directory.
iii. ls -l: Displays a long listing format, showing detailed
information such as permissions, owner, group, size, and
modification date of files.
iv. ls -a: Shows all files, including hidden files (those starting
copyright© Binayak Prasad Gupta.
with a dot.).
v. ls -h: Displays file sizes in a human-readable format (e.g.,
kilobytes, megabytes).
vi. ls -t: Sorts files by modification time, showing the most
recently modified files first.
vii. ls -r: Lists files in reverse order.
viii. ls -R: Lists directory contents recursively, showing
subdirectories and their contents.

● Example 1: List all the files in the present directory.

● Here we will use command: “ls”

● List of the files present in the present working


directory
● Example 2: List all the files in the /etc/apt directory.

● Use command: “ls /etc/apt”

● Example 3: List all the files in the present working


directory in long listing format.

copyright© Binayak Prasad Gupta.


● Use command: “ls -l”

● File type and file permissions:


i. The first bit of the first section displays the file type
followed by the permissions. In the above screenshot, the
first bit ‘d’ means that the file type is a directory.
ii. The next 3 bits displays the permissions of the user,
followed by the next 3 bits displaying the permissions for
the group, followed by the next 3 bits displaying the
permissions for other users.
iii. The character ‘r’ represents read permission, ‘w’ represents
write permission and ‘x’ represents execute permission.
iv. The second section displays the number of hard links,
v. The third section displays the owner of the file.
vi. The fourth section displays the group that the file belongs
to.
vii. The fifth section displays the size of the file in bytes.
viii. The sixth section displays the last modification date of the
file.
copyright© Binayak Prasad Gupta.
ix. The last section displays the name of the file.
x. You can find more options for ls command by running the
command: ls –help.
● Third command: “sudo”

● The sudo command prefix is used to run a command


with superuser privileges.
● Syntax: “sudo <command>”

● Example 1: Display the contents of the /etc/sudoers/


file which requires superuser privileges using the
sudo command.
● Use command: “sudo cat /etc/sudoers”

The sudoers file:


copyright© Binayak Prasad Gupta.
● The sudoers file is used to allocate system rights to the users.
This file is used by the administrators to control user rights.
When you use the sudo command, the system checks if the
user’s name using the sudo is mentioned in the sudoers file. The
system allows the command execution with superuser privileges
only if that user’s name is mentioned in the sudoers file.
● Adding a user to the sudoers file:
i. Only a root user can add users to the sudoers file.
ii. Use command: “useradd -d /home/edureka -m -s /bin/bash
edureka”

iii. To open the sudoers file, run the command:” nano


/etc/sudoers”.
iv. Then you we will see the file open.

v. Now add the username as show in the screenshot below.


copyright© Binayak Prasad Gupta.
vi. Then press Ctrl + S to save and Ctrl + X to exit.
vii. Then use command for creating password for edureka:
“passwd edureka”
viii. Then set the password.

● Fourth command: “mkdir”

● The mkdir command is used to create a new


directory.
● Syntax: “mkdir <directoryname>”.

● Example 1: Create a directory named edureka.

● Use command: “mkdir edureka”

copyright© Binayak Prasad Gupta.


● Fifth command: “cd”

● The cd command is used to change the present


working directory.
● Syntax: “cd [options] <path to directory>”.

● Example 1: Change the present working directory


to /etc/apt
● Use command: “cd /etc/apt”

● Example 2: Moving to the home directory.

● Use command: “cd ~”

● Example 3: Moving up one directory (parent


directory)

copyright© Binayak Prasad Gupta.


● Use command: “cd ..”

● Example 4: Moving to the previous directory.

● Use command: “cd ~ “

● Sixth command: “cp”

● The cp command is used to copy files and directories


from one directory to another.
● Syntax: “cp <source filename> <destination path>”.

● Example 1: Copy the sources.list file from /etc/apt/ to


the /root directory.
● Use command: “cp /etc/apt/sources.list /root”

copyright© Binayak Prasad Gupta.


● Seventh command: “mv”

● The mv command is used to move files and


directories from one directory to another or to
rename files.
● Syntax: mv <source filename> <destination path>

● Example 1: Move the sources.list file from /home/kali


to the /home/kali/Documents directory.
● Use command: “mv sources.list Documents/”

● Example 2: Rename a file using the mv command.

● Use command: “mv sources.list source1.list”

copyright© Binayak Prasad Gupta.


● Eighth command: “cat”

● The cat command is used to create file(s), view the


contents of the file, concatenate files and redirect
output.
● Syntax: cat <filename>

● Example 1: Display the contents of the /etc/hosts file.

● Command: “cat /etc/hosts”

● Example 2: Display the contents of the /etc/hosts and


/etc/apt/sources.list file
copyright© Binayak Prasad Gupta.
● Command: “cat /etc/hosts /etc/apt/sources.list”

● You can find more options for cat command by


running the command: “cat --help”
● Nineth command: “>”

● The > (redirection) operator is used to redirect the


output of a command to a file.
● Syntax: “<command 1> > <filename>”

● Example 1: Use command “ls -l > a.txt”

copyright© Binayak Prasad Gupta.


● A single redirection operator writes into the file. If
you want to append the file, you will have to use
double redirection operator (>>).
● Example 2:

● Tenth command: “less”

● The less command is used to read the contents of a


file one page at a time.
● Syntax: “less [options] <filename>”

● Example 1: Use command: less sources.list

copyright© Binayak Prasad Gupta.


● You can find more options for less command by
running the command: less --help
● Eleventh command: “echo”

● The echo command is used to display the string


passed as an argument to the terminal.
● Syntax: “echo [options] <string>”

● Example: Use command: “echo hello”

copyright© Binayak Prasad Gupta.


● You can find more options for echo command by
running the command: “man echo”.
● Twelfth command: “touch”.

● The touch command is used to create a file or change


the timestamp of the file,
● Syntax: “touch [options] <filename>”

● Example 1: Use command: “touch a.txt”

● You can find more options for touch command by


running the command: “touch --help”
● Thirteenth command: “chown”

● The chown command is used to change the owner


copyright© Binayak Prasad Gupta.
and group of files.
● Syntax: “chown [options] <[Owner][:Group]>
<filename>”
● Example 1: Changing the owner of a file.

● Use command:” chown edureka a.txt”

● Example 2: Changing the group of a file.

● Command: chown: edureka a.txt

● Example 3: Changing the owner and group of a file.

● Command: chown root:root a.txt

copyright© Binayak Prasad Gupta.


● You can find more options for chown command by
running the command: “chown --help”.
● Fourteenth command: “chmod”.

● The chmod command is used to change the


permissions of a file.
● Syntax:” chmod [options] <new permissions>
<filename>”
● The permissions can be set in two modes:
i. Octal Mode: In octal mode the permissions are set using 3
bits for users(‘u’), group(‘g’) and other(‘o’) users
respectively. Each bit is a binary to decimal conversion of
permission.

copyright© Binayak Prasad Gupta.


Example: Changing the permissions of a file so that the user
can read, write and execute, the group can read
and execute and other users can only execute using octal
mode.
Command: “chmod 751 a.txt”

ii. Symbolic Mode: In symbolic mode the permissions are


changed by using the character ‘+’ to add, ‘-‘ to remove and
‘=’ to set the permissions. The permissions are represented
by ‘r’ for read, ‘w’ for write, ‘x’ for execute.

Example 1: Changing the permissions of a file so that the


user can read and execute, the group can only read,
and other users can only read using symbolic mode.
Command: “chmod u=rx,g=r,o=r b.txt”

copyright© Binayak Prasad Gupta.


iii. Example 2: Add execute permissions to a file using symbolic
mode.
iv. Command: “chmod +x c.txt”

v. Example 3: Remove write permissions of a file using


symbolic mode.
vi. Command: “chmod a-w c.txt”

vii. You can find more options for chown command by running
the command: “chmod --help”
viii. Example 4: Add sticky bit to a file using octal and symbolic
mode.
ix. Command: “chmod +t c.txt”
copyright© Binayak Prasad Gupta.
x. Command: “chmod 1777 c.txt”

xi. Example 5: Add SUID to a file using octal and symbolic


mode.
xii. Command: “chmod u+s c.txt”

xiii. Command: “chmod 4751 c.txt”

xiv. When the file has execute permission, the SUID bit is
copyright© Binayak Prasad Gupta.
represented by a lower-case ‘s’. When the file does not has
execute permission, the SUID bit is represented by an
upper-case ‘S’.
xv. Example 6: Add SGID to a file using octal and symbolic
mode.
xvi. Command: “chmod g+s a.txt”

xvii. Command: “chmod 2751 b.txt”

xviii. When the file has execute permission, the SUID bit is
represented by a lower-case ‘s’. When the file does not has
execute permission, the SUID bit is represented by an
upper-case ‘S’. You can find more options for chmod
command by running the command: “chmod --help”.

● Fifteenth command: “rm”.

● The rm command is used to remove file or directories


copyright© Binayak Prasad Gupta.
● Syntax: “rm [options] <file or directory name>”
i. Example 1: Remove the file “sources.list” from “/root”
directory.
ii. Command: “rm /root/sources.list”

iii. Example 2: Remove a directory named “test_directory” and


all the contents in it.
iv. Command: “rm -r test_directory”

v. The “-r” option is used to recursively remove all the


contents of a directory.
vi. Example 3: Force remove a directory named
“test_directory2” and all the contents in it.
vii. Command: “rm -rf test_directory2”

viii. The “-f” option is used to force remove files or directories.


ix. You can find more options for rm command by running the
command: “rm --help”.
Regular Expressions:
copyright© Binayak Prasad Gupta.
● Sixteenth command: “grep”.

● The grep command is used to search for patterns in a


file or a string.
● Syntax:
1. “grep [options] <pattern> <filename>”
2. “cat <filename> | grep <pattern>”
● Basic Regular Expressions:

i. Example 1: List of the Hotmail email ids.


ii. Command: “cat email.txt | grep $’hotmail.com’”

copyright© Binayak Prasad Gupta.


iii. Example 2: List the email ids that start with a numeric
character.
iv. Command: “cat email.txt | grep ’^[0-9]’”

v. Example 3: List the email ids that has a numeric character


in it.
vi. Command: “cat email.txt | grep ’[0-9]’”

vii. Example 4: List the email ids whose usernames end with a
number.
viii. Command: “cat email.txt | grep -E ’.+[0-9]+@.+’”
copyright© Binayak Prasad Gupta.
ix. The above command is matched for the pattern where
there are one or more number of any characters (‘.+’’)
followed by one or more number of a numeric character
(‘[0-9]’) followed by an at symbol (‘@’) followed by one or
more number of any characters. The above command is
matched for the pattern where there are one or more
number of any characters (‘.+’’) followed by one or more
number of a numeric character (‘[0-9]’) followed by an at
symbol (‘@’) followed by one or more number of any
characters.

x. Example 5: List the email ids having three consecutive


vowels.
xi. Command: “cat email.txt | grep -E ’[aeiou]{3}]’”

copyright© Binayak Prasad Gupta.


● Seventeenth command: “|”

● The | (pipeline) operator is used to redirect the output of one


command as the input to another command.
● Syntax: “<command 1> | <command 2>”

● Example:

● Command: “cat /etc/hosts | grep kali”

Searching Through File System:

● Eighteenth command: “find”

● The find command is used to search for a file or


directory based on a pattern,
i. Example 1: Find the file named “resolv.conf”.
ii. Command: “find . -name ‘resolv.conf’”

copyright© Binayak Prasad Gupta.


iii. Example 2: Find all the files having txt extension.
iv. Command: “find . -type f -name ‘*.txt’”

v. Example 3: Find all the files having read, write and


execute permission.
vi. Command: “find . -type f -perm 777 -print”

vii. Example 4: Find all the empty files.


viii. Command: “find . -type f -empty”

copyright© Binayak Prasad Gupta.


ix. You can find more options for find command by
running the command: “find --help”.
● Nineteenth command:” locate”

● The locate command is used to search for a file by


name. The locate command is fast because there is a
background process that runs on your system that
continuously finds new files and stores them in a
database.
● Syntax: “locate [options] <pattern>”
i. Example 1: Find the file named resolv.conf.
ii. Command: “locate resolv.conf”

copyright© Binayak Prasad Gupta.


iii. Example 2: Print the statistics of your database.
iv. Command: “locate -S”

v. You can find more options for locate command by


running the command: “locate --help”
Operating System and Kernel Information:

● Twentieth command: “lsb_release”.

● The lsb_release command displays LSB (Linux


Standard Base) information about your specific Linux
distribution, including version number, release
codename, and distributor ID.
● Syntax:” lsb_release [options]”
i. Example 1: Display the description of the
distribution.
ii. Command: “lsb_release -d”

iii. Example 2: Display all available information about


copyright© Binayak Prasad Gupta.
the distribution.
iv. Command: “lsb_release -a”

v. You can find more options for lsb_release


command by running the command: “lsb_release
--help”
● Twenty-oneth command: “uname”.

● The uname command is used to get basic


information about the current system.
● Syntax: “uname [options]”
i. Example 1: Display the kernel name of the
system.
ii. Command: “uname -s”

iii. Example 2: Display all basic information about the


current system
iv. Command: “uname -a”
copyright© Binayak Prasad Gupta.
v. You can find more options for uname command
by running the command: “uname --help”
Archive Manager:

● Twenty-second command: “tar”.

● The tar command is used to create archive and


extract files from archive.
● Syntax: “tar [options] <filename>”
i. Example 1: Extract an archive.
ii. Command: “tar xvf sample.tar”

iii. Option ‘x’ represents extract, ‘v’ represents


verbose and ‘f’ represents file
iv. Example 2: Create an archive of all the text files in
the directory.
v. Command: “tar cvf sample.tar *.txt”

copyright© Binayak Prasad Gupta.


vi. Option ‘c’ represents create, ‘v’ represents
verbose and ‘f’ represents file.
vii. You can find more options for tar command by
running the command: “tar --help”.
File Transfer Commands:

● Twenty-third command: “curl”.

● The curl command is used to transfer data from or to


a server.
● Syntax: “curl [options] <url>”
i. Example 1: Download a file from a server.
ii. Command: “curl -o curl.txt https://curl.haxx.se/”

iii. Example 2: Resume a file download.


iv. Command: curl -C - -O
http://mirrors.estointernet.in/apache/zookeeper/zookeeper-3.4.14/zooke
eper-3.4.14.tar.gz

copyright© Binayak Prasad Gupta.


v. Example 3: Get the cookies from a website.
vi. Command: “curl --cookie-jar cookie.txt
http://www.edureka.co/index.html -O

vii. You can find more options for curl command by running the
command: “curl --help”
● Twenty-fourth command:” wget”

● The wget command is used to retrieve files using the most


widely used Internet protocols.
● Syntax: “wget [options] <url>”
i. Example 1: Download a file from a server
ii. Command: “wget http://ftp.gnu.org/gnu/wget/wget-
1.5.3.tar.gz”

iii. Example 2: Download multiple files at a time


copyright© Binayak Prasad Gupta.
iv. Command: “wget
http://mirrors.estointernet.in/apache/zookeeper/zookeeper3.4.14/zookeeper-
3.4.14.tar.gz http://ftp.gnu.org/gnu/wget/wget-1.5.3.tar.gz”

v. Example 3: Resume a file download.


vi. Command: “wget -c
http://mirrors.estointernet.in/apache/zookeeper/zookeeper-3.4.14/zooke
eper-3.4.14.tar.gz”

vii. Example 5: Limit the download speed


viii. Command: “wget --limit-rate=200k log.txt
http://mirrors.estointernet.in/apache/zookeeper/zookeeper-
3.4.14/zookeeper-3.4.14.tar.gz”

ix. You can find more options for wget command by running
the command: “wget --help”

copyright© Binayak Prasad Gupta.


copyright© Binayak Prasad Gupta.

You might also like