An alias is nothing but the shortcut to commands. The alias command allows the user to launch any command or group of commands (including options and filenames) by entering a single word. Use alias command to display a list of all defined aliases. You can add user-defined aliases to ~/.bashrcfile. You can cut down typing time with these aliases, work smartly, and increase productivity at the command prompt.
More about aliases
The general syntax for the alias command for the bash shell is as follows:
Task: List aliases
Type the following command:
alias
Sample outputs:
alias ..='cd ..'
alias amazonbackup='s3backup'
alias apt-get='sudo apt-get'
...
By default alias command shows a list of aliases that are defined for the current user.
The wc command shows the number of lines, words, and bytes contained in file. The syntax is as follows to get the file size: wc -c /path/to/file wc -c /etc/passwd Sample outputs:
5253 /etc/passwd
You can easily extract the first field either using the cut or awk command: wc -c /etc/passwd | awk '{print $1}' Sample outputs:
How to get the size of a file in a bash script using stat command
The stat command shows information about the file. The syntax is as follows to get the file size on GNU/Linux stat: stat -c %s "/etc/passwd" OR stat --format=%s "/etc/passwd" To assign this size to a bash variable:
myfilesize=$(stat--format=%s "/etc/passwd")echo"$myfilesize"## or ##myFileSizeCheck=$(stat-c%s "/etc/resolv.conf")printf"My file size = %d\n"$myFileSizeCheck
The syntax is as follows to get the file size on BSD/MacOS stat: stat -f %z "/etc/passwd" Please note that if the file is symlink you will get size of that link only with the stat command.
How can I extract or fetch a domain name from a URL string (e.g. https://www.cyberciti.biz/index.php) using bash shell scripting under Linux or Unix-like operating system?
You can use standard Unix commands such as sed, awk, grep, Perl, Python and more to get domain name from URL. No need to write regex. It is pretty simple.
Let use see various commands and option to grab the domain part from given variable under Linux or Unix-like system.
Get domain name from full URL
Say your url name is stored in a bash shell variable such as $x: x='https://www.dbsysnet.com/' You can use the awk as follows: echo "$x" | awk -F/ '{print $3}' ### OR ### awk -F/ '{print $3}' <<<$x Sample outputs:
www.dbsysnet.com
Extract domain name from URL using sed
Here is a sample sed command: url="https://www.dbsysnet.com" echo "$url" | sed -e 's|^[^/]*//||' -e 's|/.*$||'
Extract domain name from URL using bash shell parameter substitution
# My shell variable f="https://www.cyberciti.biz/faq/copy-command/"## Remove protocol part of url ##f="${f#http://}"f="${f#https://}"f="${f#ftp://}"f="${f#scp://}"f="${f#scp://}"f="${f#sftp://}"## Remove username and/or username:password part of URL ##f="${f#*:*@}"f="${f#*@}"## Remove rest of urls ##f=${f%%/*}## Show domain name only ##echo"$f"
I want to find out how many times a word (say foo or an IP address) occurs in a text file using the grep command on Linux or Unix-like system?
You can use the grep command to search strings, words, text, and numbers for a given patterns. You can pass the -coption to grep command. It only shows the number of times that the pattern has been matched for each file.
Show the total number of times that the word foo appears in a file named bar.txt
The syntax is: grep -c string filename grep -c foo bar.txt Sample outputs:
3
To count total number of occurrences of word in a file named /etc/passwd root using grep, run: grep -c root /etc/passwd To verify that run: grep --color root /etc/passwd Pass the -w option to grep to select only an entire word or phrase that matches the specified pattern: grep -w root /etc/passwd OR grep -c -w root /etc/passwd In this example only match a word being with root: grep --color -w '^root' /etc/passwd grep -c -w '^root' /etc/passwd To show only the matching part of the lines. grep -o 'root' /etc/passwd grep -c -o 'root' /etc/passwd Sample session:
Fig.01: Counting occurrence of words/strings using grep command
I want to display a countdown before purging cache from CDN network. Is there an existing command to show a conuntdown from 30..1 as 30,29,28,…1 on Linux or Unix bash shell script?
There are various ways to show a countdown in your shell scripts.
First define your message: msg="Purging cache please wait..." Now clear the screen and display the message at row 10 and column 5 using tput: clear tput cup 10 5 Next you need to display the message: echo -n "$msg" Find out the length of string: l=${#msg} Calculate the next column: l=$(( l+5 )) Finally use a bash for loop to show countdown: for i in {30..01} do tput cup 10 $l echo -n "$i" sleep 1 done echo Here is a complete shell script:
#!/bin/bash# Purpose: Purge urls from Cloudflare Cache# Author: Vivek Gite {www.cyberciti.biz} under GPL v2.x+# --------------------------------------------------------# Set me first #zone_id="My-ID"api_key="My_API_KEY"email_id="My_EMAIL_ID"row=2
col=2
urls="$@"
countdown(){msg="Purging ${1}..."clear
tput cup $row$colecho-n"$msg"l=${#msg}l=$(( l+$col))for i in{30..1}do
tput cup $row$lecho-n"$i"sleep 1
done}# Do itfor u in$urlsdoamp_url="${u}amp/"
curl -X DELETE "https://api.cloudflare.com/client/v4/zones/${zone_id}/purge_cache" \
-H"X-Auth-Email: ${email_id}" \
-H"X-Auth-Key: ${api_key}" \
-H"Content-Type: application/json" \
--data"{\"files\":[\"${u}\",\"${amp_url}\"]}"&>/dev/null && countdown "$u"doneecho