Bash script recursively find all files in directory

Bash script recursively find all files in current and subfolders based on wildcard matching, you can use find normally but tree with -P option is easier to read.

find will find all files that match a pattern:

However, if you want a picture:

Hope this helps!

Bash script arguments example

In this post, I will shortly share Bash script arguments via an easy example so it can help you understand how it works and write a bash script with parameters input from command line in terminal.

Let’s imagine the following command line:

If your_bash_program were a bash shell script, we could read each item on the command line because the positional parameters contain the following:

  • $0 would contain “your_bash_program
  • $1 would contain “word1”
  • $2 would contain “word2”
  • $3 would contain “word3”

Here is a script you can use to try this out:

Bash script for loop

This type of for bash script for loop is characterized by counting. The range is specified by a beginning (#1) and ending number (#5). The for loop executes a sequence of commands for each member in a list of items. A representative example in BASH is as follows to display welcome message 5 times with for loop:

Bash script while loop

Bash script while loop is one of the most fundamental and powerful constructs computing because it allows us to repeat a set of commands, as many times as we want, upon a list of items of our choosing. The loop is how we make the computer do that repetitive work

while-loop is a variation of the above, but is safer for reading lines from a file:

A while loop with interactive

or in .sh file

 

Bash script check a valid phone number

We are given a text file calledsample.txt that contains la ist of phone numbers ( each is per line), we will write a bash script to check which one is a valid phone numbers.

Valid phone number format as: (xxx) xxx-xxxx or xxx-xxx-xxxx. (x means a digit)

You may also assume each line in the text file must not contain leading or trailing white spaces.

Example:

Assume that sample.txt has the following content:

Our script should output the following valid phone numbers:

We appreciate your comments as the answer for “bash script check a valid phone number

The Regular Expression is:

  • ^ denotes the start of the string.
  • $ denotes the end of the string.
  • [0-9] means numbers
  • {3} means 3 numbers and {4} means 4 numbers
  • (A)|(B) means match pattern A or pattern B
  • \( escapes the bracket

So, you can use grep with parameter -P to match the pattern.

Alternatively, you can use awk or sed.

So we can use
[[:digit:]]{3} or \d{3} to replace [0-9]{3}.

Bash script to count the frequency of each word in a text file

Linux shell — text processing — Write a bash script to count the frequency of each word in a text file

Explanation:

  • cat: open content
  • tr: replace all space with a newline ( all words will be as one line )
  • sed: delete blank lines in the file
  • tr: convert all to lowercase
  • sort: sort alpha
  • uniq: count word
  • sort: sort with reverse order
  • awk: print by format we want