Linux Tips


List current directory with entire matched path

To list directories in Linux we can use the “ls” commands like

ls -ltrh

If we want to list the matching files with the entire directory structure we can use “find”. Below command will list all paths starting from present directory including all subfolders. Say we have file “file1.txt” in root directory and another one inside “test” directory. Then the following command will output

find "$PWD" | grep file1*

Output

$ find "$PWD" | grep file1*

/test/file1.txt
file1.txt

Replace text inside file using sed

“sed” is a handy command to do text substitution and programmatically changing file contents. Below example will replace all the matching contents of file with the replacement provided.

sed "s:matchpattern:replacement:;" -i filename

sed "s:abcd:efgh:;" -i ./mytext.txt # Will replace all instances of "abcd" with "efgh" in file mytext.txt

SRV lookup

We can do srv lookup to find the address matching srv using “host” command

 host -t srv _rest_service._tcp.example.com

Dig command for dns lookup

“dig” command can be used for dns lookup using system dns resolver or using a dns server. This is useful for testing if the dns resolution is working across dns servers.

System resolution

dig google.com

Output

; <<>> DiG 9.11.3-1ubuntu1.11-Ubuntu <<>> google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 9798
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;google.com.			IN	A

;; ANSWER SECTION:
google.com.		89	IN	A	216.58.194.206

;; Query time: 12 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Sun Feb 02 12:58:58 PST 2020
;; MSG SIZE  rcvd: 55

Dns resolution using google dns

dig @8.8.8.8 google.com

Output

; <<>> DiG 9.11.3-1ubuntu1.11-Ubuntu <<>> @8.8.8.8 google.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 38343
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;google.com.			IN	A

;; ANSWER SECTION:
google.com.		209	IN	A	172.217.5.110

;; Query time: 12 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Sun Feb 02 12:59:10 PST 2020
;; MSG SIZE  rcvd: 55