Introduction to Command Line Interface

GUI

▪ GUI = A graphical user interface, where we have graphical elements that you can interact with, like buttons

⭕ CLI & Terminal

▪ CLI = Command Line Interface, where users type in commands and see the results printed on the screen.

▪ Terminal = the GUI window that you see on the screen. It takes commands and shows output.

▪ On servers, you only have the CLI, no GUI GUI = A graphical user interface, where we have graphical elements that you can interact with, like buttons GUI vs CLI GUI CLI & Terminal

⭕ Why CLI over GUI?

▪ Work more efficient

▪ Easier for bulk operations

▪ CLI is more powerful

Basic Linux Commands

ls – List directory contents.

If you know windows you would know that the command dir is used to list the contents in a directory. In Linux, the ls command is used to list out files and directories. Some versions may support color-coding. The names in blue represent the names of directories.

ls -l | more – this helps to paginate the output so you can view page by page. Otherwise the listing scrolls down rapidly. You can always use ctrl c to go back to the command line.

┌──(asaf㉿kali)-[~]
└─$ ls -l filename

cd /var/www – Change the current directory.

The forward slash is to be used in Linux. The example is a Linux directory that comes with all versions of Linux.

When you use ls –I you will be able to see more details of the contents in the directory

It will list down the

  • Permissions associated with the file

  • The owner of the file

  • The group associated with the file

  • The size of the file

  • The timestamp

  • The name of the file

┌──(asaf㉿kali)-[~]
└─$ cd /var/www

grep – Find text in a file.

The grep command searches through many files at a time to find a piece of text you are looking for.

grep PATTERN [FILE]

grep failed transaction.log

The above command will find all of the words in the files that matched the word ‘failed’.

┌──(asaf㉿kali)-[~]
└─$ grep ‘failed’ transaction.log

su / sudo command –

There are some commands that need elevated rights to run on a Linux system. So you run them as a System Administrator which normal users cannot do.

su command changes the shell to be used as a super user and until you use the exit command you can continue to be the super user

sudo – if you just need to run something as a super user, you can use the sudo command. This will allow you to run the command in elevated rights and once the command is executed you will be back to your normal rights and permissions.

Example – shutdown command the shutdown command safely turns off the computer system.

  • sudo shutdown 2 – shutdown and turns of the computer after 2 minutes

  • sudo shutdown –r 2 – shuts down and reboots in 2 minutes

  • Using ctrl C or shutdown –c helps in stopping the shutdown process.

┌──(asaf㉿kali)-[~]
└─$ sudo shutdown 2
┌──(asaf㉿kali)-[~]
└─$ sudo shutdown –r 2

pwd – Print Working Directory

One way to identify the directory you are working in is the pwd command

It displays the current working directory path and is useful when directory changes are often

$ pwd

passwd – Change the user account password.

Though looks similar to the pwd command the role it plays is different.

You could change your password or the password of other users. Note that the normal system users may only change their own password, while root may modify the password for any account.

passwd [username] - changes the password for the user.

┌──(asaf㉿kali)-[~]
└─$ passwd admin

mv – Move a file

To move a file or rename a file you would use the mv command.

Here the file name gets changed from first.txt to second.txt.

Type ls to view the change

┌──(asaf㉿kali)-[~]
└─$ mv first.txt second.txt

cp – Copy a file

cp source file destination file. In case you need a copy of the file second.txt in the same directory you have to use the cp command

┌──(asaf㉿kali)-[~]
└─$ cp second.txt third.txt

You can use ls – l to see the new file created. The two files will be exactly of the same size.

rm – Remove files in a directory or the directory itself

A directory cannot be removed if it is not empty.

rm [name of the file]

rm –r removes all the contents in a directory and the directory as well.

┌──(asaf㉿kali)-[~]
└─$ rm file1
┌──(asaf㉿kali)-[~]
└─$ rm -r myproject

mkdir – to make a directory.

mkdir [directory name] if you would like to create a directory in the name ‘myproject’ type

mkdir myproject

┌──(asaf㉿kali)-[~]
└─$ mkdir myproject

chmod – Change the mode of a file system object

Files can have r – read, w- write and x-execute permissions.

For example:

  • chmod mode FILE

  • chmod 744 script.sh

  • The first number stands for the user who is associated with the file

  • The second number is for the group associated with the file

  • The third number is associated with everyone else who is not a part of the user or group

┌──(asaf㉿kali)-[~]
└─$ chmod 744 script.sh

Octal Notation

Permission

Symbolic Representation

0

No Permission

---

1

Execute Permission Only

--x

2

Write Permission Only

-w-

3

Write and Execute Permissions (1+2)=3

-wx

4

Read Permission Only

r--

5

Read and Execute Permissions (1+4)=5

r-x

6

Read and Write Permissions (2+4)=6

rw-

7

Read, Write and Execute Permissions, Means Full Permissions (1+2+4)=7

rwx

From the table you will see that the rights given will be as

  • -rwxr- - r- - rwx for user

  • r - - for the group (read only)

  • r - - for others (read only)

Other ways of using chmod are

  • chmod a-w first.txt

This means all users have no write access to the file first.txt.

  • chmod u + x script.sh

The owner of script.sh can execute the file

chown – Change the ownership of a file/folder or even multiple files/folders for a specified user/group.

This command is used to

chown owner_name file_name

┌──(asaf㉿kali)-[~]
└─$ chown user1 script.sh

Assume that if you are a user named user1 and you want to change ownership to root use “sudo” before syntax.

┌──(asaf㉿kali)-[~]
└─$ sudo chown root script.sh

cat - Allows us to create single or multiple files, view contents of files, concatenate files, and redirect output in terminal or files.

The cat command (short for “concatenate “) is one of the most frequently used commands in Linux. cat command

┌──(asaf㉿kali)-[~]
└─$ cat file.txt
┌──(asaf㉿kali)-[~]
└─$ cat file1.txt file2.txt

Output will show the entire contents of the file(s).

echo – Display a text or a string to the standard output or a file.

This command is used to

$ echo “This is an article on basic linux commands”

This is an article on basic linux commands

The echo –e option acts as an interpretation of escape characters that are back-slashed.

┌──(asaf㉿kali)-[~]
└─$ echo –e “This is an article is for beginners. \nIt is on basic linux commands

Will display the output as

This is an article is for beginners.
It is on basic linux commands

\n the newline character is interpreted by the echo –e command

wc - Find out the number of new lines, word count, byte and characters count in a file specified by the file arguments.

wc [options] filenames.

┌──(asaf㉿kali)-[~]
└─$ wc –l readme.txt

Shows the output as - 120 readme.txt

  • wc -l : Prints the number of lines in a file.

  • wc -w : prints the number of words in a file.

  • wc -c : Displays the count of bytes in a file.

  • wc -m : prints the count of characters from a file.

  • wc -L : prints only the length of the longest line in a file.

man – View the on-line reference manual pages for commands/programs.

┌──(asaf㉿kali)-[~]
└─$ man grep

history – Show previously used commands or to get information about the commands executed by a user.

┌──(asaf㉿kali)-[~]
└─$ history
    1  ping 10.163.5.1
    2  ping 10.163.5.183

clear – Clear the terminal screen

This command lets you clear the terminal screen.

┌──(asaf㉿kali)-[~]
└─$ clear

apt –get - Install new software packages, remove available software packages, upgrade existing software packages as well as upgrade the entire operating system.

apt -get is a powerful and free front-end package manager for Debian/Ubuntu systems. apt – stands for advanced packaging tool.

┌──(asaf㉿kali)-[~]
└─$ sudo apt-get update

reboot – Halt, power-off or reboot a system as follows.

This command may be used to

┌──(asaf㉿kali)-[~]
└─$ reboot

Last updated