Basic Linux Commands
- grep [options] pattern [files]
- -c : This prints only a count of the lines that match a pattern
- -i : Ignores, case for matching
- -w : Match whole word
- grep -i "UNix" <file_Name>
- grep -v "#" <file_Name> -v option tells grep to invert its output
- find . -name “*.txt” | grep –i JayZ | grep –vi “remix”
- grep -n "ERROR" <file_Name> returns line # of search pattern
- grep -wr "ERROR" * -r recursively search for word in subdirectories
- egrep -wr "one|new" * filter with OR
- File Handling
- Touch command to create a file of 0kb size.
- touch <file_Name>
- Vi command to edit a file using
- vi <fileName>
- ESC :q! Enter to exit without save
- ESC :wq Enter to exit & save
- Cat command to view a file
- cat <file_Name>
- Less command
- less <file_Name>
- Press Q to close the file
- Editing File
- Sed command
- Substitution/replace in file
- sed -i 's/linux/unix/' <file_Name> for single occurrence in each line
- sed -i 's/linux/unix/g' <file_Name> for all occurrences in all lines
- sed -i '3 s/linux/unix/' <file_Name> for 1st occurrence in 3rd lines
- sed -i '3 s/linux/unix/g' <file_Name> for all occurrence in 3rd lines
- sed -i '1,3 s/linux/unix/g' <file_Name> for all occurrence in 1st to 3rd line
- sed -i '3,$ s/linux/unix/g' <file_Name> for all occurrence in 3rd to last line
- sed -i 's/linux/unix/p' <file_Name> print replaced lines again
- Deletion in file
- sed -i 'nd' <file_Name> delete nth line
- sed -i '$d' <file_Name> delete last line
- sed -i 'x,yd' <file_Name> delete from range x to yth line
- sed -i '/pattern/d' <file_Name> delete pattern matching line e.g sed -i '/ERROR/d' <file_Name>
- lines containing ERROR are deleted.
- awk: Awk is abbreviated from the names of the developers – Aho, Weinberger, and Kernighan.
- It is a scripting language used for manipulating data and generating reports.
- The awk command programming language requires no compiling,
- and allows
the user to use variables, numeric functions, string functions, and logical
operators.
- print content of file
- awk '{print}' <file_Name>
- print content of file which matches pattern "manager"
- awk '/pattern/ {print}' <file_Name>
- print content of file only column 1 and 2 separated by space delimiter
- awk '{print $1,$2}' <file_Name>
- print content of file only column 1 separated by - delimiter as defined in command
- awk -F"-"' '{print $1}' <file_Name>
- print content of file with ROW NUMBER NR and all content with $0
- awk '{print NR,$0}' <file_Name>
- print content of file with ROW NUMBER NR and only last column $NF
- awk '{print NR,$NF}' <file_Name>
- print content of file with ROW NUMBER NR and all content with $0 from 3 to 6 row.
- awk ' NR==3 , NR==6 {print NR,$0}' <file_Name>
- print all only if column 3 contains B6
- awk '{ if($3 == "B6") print $0;}' <file_Name>
- for loop
- awk 'BEGIN { for(i=1;i<=6;i++) print "square of", i, "is",i*i; }'
- Find command
- Find all files in current directory and subdirectory
- find -type f
- Find all directories in current directory and subdirectory
- find -type d
- Find file using name and Ignore the case in home directory
- find /home -iname newFile.txt
- Find file using name in current directory
- find . -name newFile.txt
- Find directories using Name
- find / -type d -name Documents
- Find last 50 days modified file
- find / -mtime +50
- Find last 50 days accessed file
- find / -atime +50
- Find changed file in last 60 min
- find / -cmin +60
- Find accessed file in last 60 min
- find / -amin +60
- Find greater than 50MB file
- find . -size +50M
- Find less than 50MB file
- find . -size -50M
- Find 50MB file
- find . -size 50M
- Find the file with extension
- find /root -name '*.txt'
- find . -name '*.c' -print
- find . -name \*.c -print
- find . -perm -666
- find . -perm /u=w,g=w
- find directories having permission 777
- find /root -type d -perm 777
- find files having permission 655
- find /root -type f -perm 777
- Cut and paste:
- Position the cursor where you want to begin cutting.
- Press v to select characters (or uppercase V to select whole lines).
- Move the cursor to the end of what you want to cut.
- Press d to cut (or y to copy).
- Move to where you would like to paste.
- Press P to paste before the cursor, or p to paste after.
- Copy and paste is performed with the same steps except for step 4
- where you would press y instead of d:
- d = delete = cut y = yank = copy
- more to come
Comments
Post a Comment