Important Linux CMD

Basic Linux cmd
All users should be familiar with most of these commands as they are required for most operating system tasks and computer programming.
To list all files
ls [option : -l, -a, etc]To get the present working directory
pwdTo create directory
mkdirdir_nameTo navigate between directory
cd [.., ~, /, file_path]To copy file
cp file1 file2To rename file from file1 to file2
mv file1.txt file2.txtTo delete the files created in the file and empty the dir
rm file.txtto delete the files created in the directory
rm -R dir.txtTo open a text editor
touch file.txtTo print/view text file
cat file.txtecho file.tx
awk
awk is a programming language that originated at AT&T Bell Laboratories in 1977. Its name comes from the initials of its designers: Aho, Weinberger, and Kernighan.
The awk command uses a set of user-supplied instructions to compare a set of files, one line at a time, to extended regular expressions supplied by the user. Then actions are performed upon any line that matches the extended regular expressions.
awk is very useful when used with pipe, OS, grep and many more commands to extract useful information.
awk is best with structured data [csv]
Some common awk commands
When you want to extract the top 4 processes or lines use can use
ps -ef | awk ' FNR<=5{print}'with
$you can extract specific columns, if you want to extract 3rd column you can use$3ps -ef | awk ' {print $3}'To get all the data you can use
$0and to get the last column$NF[number of fields]ps -ef | awk ' FNR<=5{print $NF}'To Substitute words globally in the text of the file
awk ' gsub("CMD", "command") ' <file_name>To extract a row that contains specific text
awk ‘BEGIN {IGNORECASE=1} /TEXT/ {print $0}}‘ file_name.csvhere
IGNORECASEis a flag for case-insensitive
sed
sed is a stream editor, and it can be used to edit text files, with its most common use being to replace occurrences of words in a file. This is extremely helpful in situations where a configuration file has a lot of instances of the same word that need replacing throughout the file.
Some common sed commands
To Substitute words globally in the text of the file
sed -n ’s/TEXT1/TEXT2/g’ file_name.txtTo extract specific line
sed -n ‘2,+2p‘ file_name.txtTo get an even line
sed -n ‘2~2p‘ file_name.txtTo delete empty lines in the file
sed ‘/^$/‘ file_name.txtTo get lines that contain text
sed ‘/TEXT/p‘ file_name.txtTo stop where 1st occurrence of TEXT
sed ‘/TEXT/q‘ file_name.txt
File Permission
File permissions control who can access, modify, or execute files and directories. These permissions are categorized into three types: Read (r), Write (w), and Execute (x). They are assigned to the file's owner, the group the file belongs to, and all other users on the system
ls -l to get all files and see their permissions -r-xr-xr-x@ 1 amitgupta staff 1140643 24 Apr 14:58 res.txt here -r-xr-xr-x is permission where 1st bit is for file type d for dir and f for the file, the next 9 bits are for permission for the user, group, and other 3 bits each.
value of permission
execute x = 1
write w = 2
read r\= 4
To change the permission of the file
chmod [u/g/o][+,-][w,r,x] file_namechmod 0777 file_nameadd the value of permissionTo change owner
chown [option] new_owner[:new_group] file_name
Comments
Join the discussionNo comments yet. Be the first to comment.

