Command Line Interface
The command line interface is one of the way to interact with the operating system using special keywords known as commands. By using the commands one can perform all the operations needed skipping the Graphical User Interface.
The interface is provided by a software known as shell. Most used shell is bash.
Common Commands to get started with CLI are as follows:
touch : This command creates new file in the current directory. Another way of creating file would be by specifying path "touch /folder1/folder2/fileName.txt".
touch fileName.txt
mkdir : This command creates new directory or folder in the current path unless a specific path is followed by the command.
mkdir newFolder
cd : This command stands for change directory. Used to navigate between the files and folders.
# change to existing file cd Document
# change directory to specfic path cd /path/to/directory
# change directory to home directory cd
#change directory to parent cd ..
mv : This command is used to move the files. This can be also used to rename files.
mv fileName /destination/path
mv fileName newFileName
cp : This command is used to copy files from source path to destination.
-r : This option allows to copy recursively all the files.
-a : This option allows to preserve the attributes and permissions of the copied files.
-f : This option skips the prompts and overwrites the files if they exist.
-i : This option prompts if the files exist.
cp fileTobeCopied /destination/path
ls : This command is used to list directory content.
-r : reverse sort files.
-a : list all files including hidden files.cp fileTobeCopied /destination/path ls : This command is used to list directory content.
-t : sort files.
-h : human readable format.
-l : long list format.
ls -altrh # combination of all flags
pwd : It stands for print working directory.This command displays current path.
pwd
rm : This command is used to remove files
-r : recursively remove all the files.
-f : forcefully remove all the files without prompting for permission of the owner.
-i : prompts before removing each file.
rm fileName
chmod : 'chmod' stands for change mode. This command is used to modify the file permissions. This can be done numerically or by specifying particular change of permission.
Numeric : The sum of the values of permissions is the given to owner, others, group form a 3 digit numeric code.
Read (r) has value 4.
Write (w) has value 2.
Execute (x) has value 1.
chmod 755 fileName
# owner = 7 => r=4 + w=2 + x=1
# group = 5 => r=4 + x=1
# others = 5 => r=4 + x=1
Symbolic : Each symbol is specified with + or - symbols to add or remove the permissions.
chmod u=rwx,g=r,o=r fileName chmod +r fileName chmod -x fileName
chown : This command is used to change the owner.
chown newOwnerName fileName
cat : This command is used to view the file contents.
cat fileName
grep: This command is used to search for patterns in a file.
-o : displays pattern if found in new line each time.
-w : finds the exact pattern.
-i : ignores cases while searching.
-c : counts number of lines containing the pattern.
grep "patternToSearch" fileName
find : This command is used to find files in the directory specified or the working directory.
find /path/to/search/ -name "fileName.txt" # search by name find /path/to/search/ -name "*.txt" # search by extension find /path/to/search/ -type d # finds the directories only find /path/to/search/ -type f # finds files only
wc : This command is used to count words. It stands for word count.
wc -l fileName # counts lines in the file wc -c fileName # counts bytes in the file wc -w fileName # counts words in the file
sort : This commands sorts alphabetically
sort fileName sort -n fileName # sorts numerically sort -r fileName # sorts in reverse
nano : This opens the specified file in the editor
nano fileName
awk : Used to manipulate and analyse data files. Usually piped with other command.
# This prints line if the line starts with the word Harry awk '$1 == "Harry" { print $0 }' data.txt
sed : 'sed' stands for stream editor. This command is used as a text processing tool.
sed 's/old/new/' file.txt # substitutes old with new in file sed -n '/pattern/p' file.txt # prints lines if pattern is found sed '10,20d' file.txt # deletes lines from 10 to 20 from file sed '10,20p' file.txt # prints lines from 10 to 20 from file
ping : Used to test reachability of the host.
ping -c 5 www.example.com
ps : 'ps' is a versatile tool for monitoring and managing processes.
ps # lists all the processes ps -aux # full info about the process ps -o pid,ppid,user,cmd # custom output of process list