Linux – Adding useful aliases

I prefer to add the aliases to a different file rather than the .bashrc. You can modify the .bashrc file to include the .bash_aliases file:

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

And then create a new file .bash_aliases in the home directory with the following or your own aliases:

#!/bin/bash
# Adding custom aliases
alias l='ls -lrt'
alias lwc='ls -lrt | wc -l'
alias la='ls -lart'
alias lm='ls -lrt | more'
alias lwc='ls | wc -l'
alias f='find . -name $1'
alias b='cd ..'

Finally, to execute the bash_aliases in your current session without having to logout and login again, execute the following command:

. ~/.bash_aliases

Rename files in a sequential order

My camera shoots images until DSC_9999 and after that it resets to DSC_0001. It messes up the sequence of images shot during a day if the sequence is reset. I found this cool solution which resets them in sequential order starting from 1 and also pads 0’s to the beginning if the number is less than 100.

a=1
for i in *.JPG; do
  new=$(printf "DSC_%04d.JPG" "$a") #04 pad to length of 4
  mv -i -- "$i" "$new"
  let a=a+1
done

Debian – Switching the hostname/IP of the machine cleaned up the resolv.conf file

I built a new server to replace an existing staging server. After I did the cutover and made the new one my staging server, I realized I wasn’t able to ping internally on the network as well as outside. I checked the networking on the VMware server and it was set correctly but I still kept getting the following error on ping:

$ ping google.com
ping: unknown host google.com

I checked the hosts file under /etc/hosts and the hostname file and both of them were fine. However, I tried to ping the IP of google and I was able to connect. Which made me realize that resolving of the domain names to IP via DNS (Domain Name System) is not working.

Next, I did a nslookup for an internal system and google and didn’t get any response:

$ nslookup google.com
;; connection timed out; no servers could be reached

And then I tried doing a nslookup by using Google’s Name Server 8.8.8.8. A Name Server is a specialized server on the Internet that handles queries or questions from your local computer, about the location of a domain name’s various services.

A great simple way to think about name servers is using a phone book analogy. If you were trying to call Joe’s Trading Company, you’d want to look it up in a phone directory to find the phone number. The Name Server or DNS Server as it is also called does the same thing with finding out IP addresses for websites/DNS entries.

But anyways, here’s the output of nslookup when I used Google’s Name Server:

$  nslookup google.com 8.8.8.8
Server:		8.8.8.8
Address:	8.8.8.8#53

Non-authoritative answer:
Name:	google.com
Address: 172.217.5.110

So, I came to a conclusion that the DNS resolution wasn’t working. I checked out the resolv.conf file on my local machine and realized it had been blanked out – possibly during the host/IP switchover. I checked another working server and copied over the configuration to my local file under /etc/resolv.conf. resolv.conf is the name of a computer file used in various operating systems to configure the system’s Domain Name System (DNS) resolver.

domain company.pvt
search company.pvt
nameserver 192.168.XXX.XX
nameserver 192.168.XXX.XX

After saving the file, I did a nslookup and got the following result:

$ nslookup google.com
Server:		192.168.XXX.XX
Address:	192.168.XXX.XX#53

Non-authoritative answer:
Name:	google.com
Address: 216.58.217.174

A little bit more about DNS requests and how they work
But just how exactly did your computer know what webpage to display for you, and what server to pull it from?

Your web-browser knows you typed google.com into the address bar.

Your current nameservers from the resolv.conf file are retrieved.

Your computer asks the nameservers for the A (address) record for google.com.

The nameservers respond back with the IP address 216.58.217.174.

Your computer sends a request to that IP address along with the page you're requesting.

The web server hosted on google.com then sends your web browser the requested page.

Note that on occasions, there might be canonical names (CNAME) assigned to a website. If you do a nslookup on the website, the nameserver will respond back stating that the requested entry is a canonical entry and then provide the address (A) record for the main server. e.g. Google uses WebEx’s services and have a unique URL for their operation. They may not have a standalone server but may be sharing the same WebEx’s server with other clients of WebEx. I did a nslookup on google.webex.com and the result confirmed that the dns was a canonical (CNAME) entry

$ nslookup google.webex.com
Server:		192.168.XXX.XX
Address:	192.168.XXX.XX#53

Non-authoritative answer:
google.webex.com	canonical name = nebulau.webex.com.
nebulau.webex.com	canonical name = global-nebulau.webex.com.
Name:	global-nebulau.webex.com
Address: 66.114.168.201

Get PID’s of multiple processes

I had to kill multiple deamon processes running for a long time. I could get the details about the process using the following command:

ps -ef | grep update3Parser | grep -v grep

But in order to get just the PID’s so I can efficiently kill them using a single command, I had to get the output of just the 2nd column. This is what I did:

ps -ef | grep update3Parser | grep -v grep | awk '{print $2}'

After I had the output in a column, I used a regex word editor to replace \n with space and then used kill -9 PID’s to kill all the processes. Easy!