Thus, Day 2 essentially addressed the fundamentals of Linux commands. We will now cover some advanced Linux commands that are necessary to write scripts.
Let's begin😎
chmod
chmod(change mode) -> To change the access mode of a file/dir. Ex :- chmod 777 test.txt Here 7 is "read + write + execute" permissions read = 4 write = 2 execute = 1 And 3 times "7" means, we are giving all permissions to users, groups and others.
grep
grep(global regular expression print) -> It searches a file for a particular pattern of characters, and displays all lines that contain that pattern. Ex: >cat test.txt Hi Smriti!! How are you? How was your day? >grep "How" test.txt o/p:- How are you? How was your day?
awk
awk -> It filters the data in the form of column. The awk command is a versatile text processing tool that allows you to manipulate and analyze text data. It can extract specific fields from a file, perform calculations, and format data. Default separator is "Space". Ex: >cat test.txt Hi Smriti!! How are you? How was your day? >awk '/How/' test.txt o/p:- How are you? How was your day? >awk '/How/ {print $2}' test.txt #Will print 2nd column o/p:- are was
find
find -> Used to find files and directories and perform subsequent operations on them. Ex: find /home/Smriti *.txt Above command will give list of all files with ".txt" extensions present in the location /home/Smriti.
sed
sed(stream editor) -> Used for manipulating text. It allows you to search for patterns in a text and perform various operations on the matching text, such as replacing it, deleting it, or printing it Ex: >cat fruits.txt Apple Mango Banana Mango Orange >sed 's/Mango/Kiwi/g' fruits.txt #s -> substitution operator #Mango -> Search String #Kiwi -> Replacement String #g -> global replacement o/p:- Apple Kiwi Banana Kiwi Orange
tail/head
tail -20 filename -> To print the last 20 lines of a file head -20 filename -> To print the first 20 lines of a file
tar
tar -> used to create Archive and extract the Archive files. Ex: tar xvf file.tar #Extracts files from an archive. tar cvf file.tar *.txt #creates a tar file which is the Archive of all .txt files.
top
top -> displays all the running process within the environment of your system. It helps in monitoring system usage and performances. Ex: top -o %CPU #display a list of processes sorted by CPU usage.
ssh
ssh(secured shell) -> used to securely log into a remote machine and execute commands on that machine. Syntax: ssh user@host where, user = username on the remote machine host = address or hostname of the remote machine. Ex: ssh user@192.168.43.120
scp
scp(secured copy) -> used to securely copy files between two machines.
Syntax: scp source_file user@destination_host:destination_path
where, source_file = file to copy
user = username on the remote machine
destination_host = address or hostname of the remote machine
destination_path = location on the remote machine where the file will be copied.
Ex: scp file.txt user@192.168.43.120:/home/user/file.txt
*👆The information presented above is based on my interpretation. Suggestions are always welcome.*😊
~ Smriti Sharma✌