Introduction to Command Line Interface
Last updated
Last updated
▪ GUI = A graphical user interface, where we have graphical elements that you can interact with, like buttons
▪ 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
▪ Work more efficient
▪ Easier for bulk operations
▪ CLI is more powerful
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.
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
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’.
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.
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
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.
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
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
You can use ls – l to see the new file created. The two files will be exactly of the same size.
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.
mkdir [directory name] if you would like to create a directory in the name ‘myproject’ type
mkdir myproject
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
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
This command is used to
chown owner_name file_name
Assume that if you are a user named user1 and you want to change ownership to root use “sudo” before syntax.
The cat command (short for “concatenate “) is one of the most frequently used commands in Linux. cat command
Output will show the entire contents of the file(s).
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.
Will display the output as
\n the newline character is interpreted by the echo –e command
wc [options] filenames.
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.
This command lets you clear the terminal screen.
apt -get is a powerful and free front-end package manager for Debian/Ubuntu systems. apt – stands for advanced packaging tool.
This command may be used to