DevOps AccessingFilesAndRegex
DevOps AccessingFilesAndRegex
DevOps AccessingFilesAndRegex
● cat file1
● cat -n file1 //gives the numbering
Find
Options
-name : For searching a file with its name
-inum : For searching a file with particular inode number
-type : For searching a particular type of file
-user : For files whose owner is a particular user
-group : For files belonging to particular group
find / -name test1.txt → find file named test1.txt in root dir and subdirectories
find . -name test1.txt → find all files whose name is test1.txt in current directory
E.g
find / -name dpkg.log
cd /etc
find . -name 50-cloud-init.yaml
find /etc/ -group shadow -ls
find / -type l -ls → find files with symbolic link
find / -type d -name Pictures
locate pattern
The locate command works reading contents from the database.
apt-get install mlocate
E.g
#locate dpkg.log
#locate -i readme.md
$ cat word.txt
Surya is Studentx1000
surya is studentx1000
studentx1000 is surya
studentx1000 is Surya
surya
surya is surya
Timmy
Tommy
TImmy
TOmmy
Linux is a great OS
I like Linux M 10
This is a random Number 9018234103874
Sometimes I mess up case like this sUrYa
$ grep surya word.txt --> to find every line that has word surya but it does not give
lines with different cases.
$ grep -i surya word.txt --> ignores the case and displays every line that has word
surya.
$ grep [sS]urya word.txt --> displays the only lines that start with a capital
or lower case but not random capital letters.
Ths characters placed inside the square bracket are the replacement values.
$ grep ^Surya word.txt --> prints lines that begin with capital Surya
$ grep ^Surya word.txt --> caret ^ symbol represents lines that begin with.
$ grep ^[Ss]urya word.txt --> gives all lines that have capital and lower-case s.
$ grep [Ss]urya$ word.txt --> gives all the lines that end with capital or lowercase
s.
$ grep [s]urya$ word.txt --> gives lines ending with lower surya.
$ grep [S]urya$ word.txt --> gives a line ending with capital Surya.
$ grep ^surya$ word.txt --> gives a line that contains surya alone.
$ grep [0-9] word.txt --> searching numeric value. gives all the lines that contain
numeric values.
$ grep [0-9]$ word.txt --> gives lines that end with numeric value.
$ grep [0-9][0-9][0-9]$ word.txt --> gives lines that ends with the last three
characters numeric.
sed
sed stands for stream editor, which is used to search a word in the file and
replace it with the word required to be in the output
Cut
Extract texts
cut -c1 /etc/passwd
→ To print the 1st character of every row of the 1st column.
cut -c1-5 /etc/passwd
→ To print the 1st to 5th character.
cut -d ':' -f1 /etc/passwd
Here -d is the delimiter(breaks) and f1 represents field1 column 1.
cut -d -f filename(where d stands for delimiter eg. :, " " etc and f stands for
field)
AWK
AWK is a family of tools that are primarily used for processing texts files.
The most basic use of AWK is parsing the files and generating reports.