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.
✅ 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
✅ 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’.
✅ 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.
✅ 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
✅ 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.
✅ 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
✅ 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
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.
✅ mkdir – to make a directory.
mkdir [directory name] if you would like to create a directory in the name ‘myproject’ type
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
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
Assume that if you are a user named user1 and you want to change ownership to root use “sudo” before syntax.
✅ 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
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.
Will display the output as
\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.
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.
✅ history – Show previously used commands or to get information about the commands executed by a user.
✅ clear – Clear the terminal screen
This command lets you clear the terminal screen.
✅ 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.
✅ reboot – Halt, power-off or reboot a system as follows.
This command may be used to
Last updated