command Line Interface (CLI)
Introduction
CLI: A command line interface is a text-based user interface where we can input commands that interact with computer's operating system and used to run programs, managing files.
we will explore some CLI commands
1. Creating a directory :
The mkdir command is used to create a new directory . To use the mkdir command , we have to type mkdir followed by the name of the directory that we want to create. For example:
mkdir new
2. Creating a File:
The touch command is used to create a new File. To use the touch command , we have to type touch followed by the name of the text file that we want to create. For example:
touch example.txt
3. Navigating the FileSystem:
The cd command is used to navigate the file system. To use the cd command , we have to type cd followed by the name of the directory that we want to navigate. For example:
cd Desktop
we will explore some options with cd
cd . :-
The cd . command will display the current directory.Example:
cd .
cd .. :-
The cd .. command is used to navigate to the parent directory
cd ..
cd ~ :- The cd ~ command is called tlide is used to navigate to home directory
cd ~
cd - :- The cd - command is used to switch to previous directory .
cd -
4 . File Manipulation:
cp :-
The cp command is used to copy the contents of file and directory .Example
cp file1.txt file2.txt
It copies the content from file1.txt to file2.txt
cp -r :- The command used to copy all files and sub-directories.
cp -r directory1 destination/
mv :-
The mv command used to rename the files and also to move the files from one location to another location.
mv file1.txt file2.txt
mv file1.txt new_directory/
rm :-
The rm command is used to delete files .
rm file1.txt
rm -r :-
The rm -r command is used to delete the directory that is not empty
rm -r new_directory
rmdir :-
The rmdir command is used to delete the empty directory
rmdir directory
5. Text Processing :-
cat :-
The cat command is used to display the content of file
cat file1.txt
less :-
The less command is used to view the content of file and also it allows us to search using pattern and also it allows forward and backward movement though a file.
less file1.txt
grep :-
The grep command is used to search for a specific strings in the file
grep 'word' file1.txt
- will explore some options with grep
grep -o :- The command is used to match specified words only
grep -o 'word' file1.txt
grep -l :-
The command is used to print the name of the file that matches with the pattern we specified
grep -l 'word' file1.txt
sed :-
The sed command is used to manipulate text in the file .It can allows us to substitute,delete and add the text to files
sed 's/old/new/' file1.txt
sed -n '1,2p' file1.txt
the first command will replace the old text with new text in that file
the second command will print the first and second lines form the file.
awk :-
The awk command is used for text processing .For example if we want to serach for the specific word in the peice of given text or to select the specific pattern or column
awk '{print $2}' file1.txt
This command will print the text in first column
wc :-
The wc command is used to count the number of lines and words in a file
wc -l
wc -c
wc -m
pipe :-
The pipe command is used to execute two or more commands at a time .and it will take the output of another command as input and process the result.
grep -o 'word' file1.txt | wc -l
6. Process Related commands :-
ps :-
The ps command is used to list all the running process on the system . It has options like -ef it will display full format list.
ps -ef
- To get the pid of the process
ps aux |awk '{print S2}'
df :-
The df command is used to find the diskspace available ,total and used.
df
kill :-
The kill command is used to terminate the process
kill pid
kill -9 :-
This command is used to forcefully terminate the process
kill -9 pid
free :-
The free command is used to display the memory(RAM) of the system
free
uname :-
The uname command is used to display all the details of system including user,kernel version, system architecture
uname -a
lspci :-
The command lspci is used to display all the pci buses connected to the system like graphic crads ,hardware devices.
lspci
7. Network Related :
ping :-
The ping command is used to check network connectivity and to troubleshoot network issues
ping www.google.com
iconfig :-
The ifconfig command is used to display ip address of the system
ifconfig
8. File permissions:
chmod :-
The chmod command is used to change the file permissions. there are 3 permissions for every file read,write,execute and numerical values for them r=4 and w=2 and x=1 .we can able to change the permissions of file using numerical values also. For example:
chmod 775 file1.txt
chmod u+r file1.txt
chmod u-x file1.txt
- here the "+" will add that permission to the file
- "-" will remove the permisiion from file
chown :-
The chown command is used to change the ownership of the files and directories.
Let's say we have a file named example.txt and we want to change its ownership to a user named user1 and a group named group1. Here's how we can do it:
sudo chown user1:group1 example.txt
sudo :-
The sudo command is used to execute the commands with security privileges.
Shell Scripting:
It means it accepts commands as input from the user and execute them. we have to create the file with .sh extension everytime.
Example:
#!/bin/bash
clear
ls -al