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
How do I replace a string with another string in all files? For example, ~/foo directory has 100s of text file and I’d like to find out xyz string and replace with abc. I’d like to use sed or any other tool to replace all occurrence of the word.
The sed command is designed for this kind of work i.e. find and replace strings or words from a text file under Apple OX, *BSD, Linux, and UNIX like operating systems. The perl can be also used as described below.
sed replace word / string syntax
The syntax is as follows: sed -i 's/old-word/new-word/g' *.txt
GNU sed command can edit files in place (makes backup if extension supplied) using the -i option. If you are using an old UNIX sed command version try the following syntax:
sed 's/old/new/g' input.txt > output.txt
You can use old sed syntax along with bash for loop:
#!/bin/bashOLD="xyz"NEW="abc"DPATH="/home/you/foo/*.txt"BPATH="/home/you/bakup/foo"TFILE="/tmp/out.tmp.$$"[!-d$BPATH]&&mkdir-p$BPATH|| :
for f in$DPATHdoif[-f$f-a-r$f]; then/bin/cp-f$f$BPATHsed"s/$OLD/$NEW/g""$f">$TFILE&&mv$TFILE"$f"elseecho"Error: Cannot read $f"fidone/bin/rm$TFILE
A Note About Bash Escape Character
A non-quoted backslash \ is the Bash escape character. It preserves the literal value of the next character that follows, with the exception of newline. If a \newline pair appears, and the backslash itself is not quoted, the \newline is treated as a line continuation (that is, it is removed from the input stream and effectively ignored). This is useful when you would like to deal with UNIX paths. In this example, the sed command is used to replace UNIX path “/nfs/apache/logs/rawlogs/access.log” with “__DOMAIN_LOG_FILE__”:
#!/bin/bash## Our path_r1="/nfs/apache/logs/rawlogs/access.log"## Escape path for sed using bash find and replace _r1="${_r1//\//\\/}"# replace __DOMAIN_LOG_FILE__ in our sample.awstats.confsed-e"s/__DOMAIN_LOG_FILE__/${_r1}/"/nfs/conf/awstats/sample.awstats.conf >/nfs/apache/logs/awstats/awstats.conf
# call awstats/usr/bin/awstats -c/nfs/apache/logs/awstats/awstats.conf