What does this Unix code snippet do?

Data Engineers & Java Developers must have a good handle on Unix. Most production systems are Linux based with 1000’s of scripts.


Q01. What does the below Unix code do?

A1.The above code echoes previous month’s end date. For example, if today’s date is 2022-07-02 then the above code will output 2022-06-30.

“$(date +%Y-%m-01) -1 day” returns 1st of the current month & subtracts 1 day to get the last day of previous month.

Back Quotes

Q02. How do you store the previous month end date to a variable to be used later?
A02. Add the code within back quote as shown below:

The back quote is used when you want to assign output from system commands to variables. It tells the shell to take whatever is between the back quotes as a system command and execute its output.

Another example of back quote will be:


Q03. What does the below code do?

A03. It traverses through all the .conf files in the current directory & outputs the file names & the contents with the word “prod”.

Grep is an acronym that stands for Global Regular Expression, which is used to search text and strings in a given file.


Q04. What does the below code do?

A04. It traverses through all the .conf files in the current directory & replaces all the occurrences of prod with dev.

The sed command, short for stream editor, performs editing operations on text coming from standard input or a file. “s” is for substitute & “g” is for global to replace all occurrences. “/” are used to separate what you are replacing with. You can use regular expressions. For example, if you want to replace “${ENV}” with prod, then you can use the code below. Note that “\” is used to escape $ as it has special meaning in regular expression.

Note: Use the -i switch with care as it will overwrite the original file. Perform the operations without the -i switch first and then run the command again with -i once you have what you want. Create a backup of the original file or redirect the output to a file. It’s very easy to accidentally alter the original file with the -i switch.

Q05. How will you write the above sed command code without “-i” & redirect the output to a new file?
A05. If it is a single file, you can redirect with “>” or “tee” commends as shown below:


When you have multiple files to traverse through then use a for loop as shown below:

find & sed

Q06. Can you use a find with sed?
A06. find command is one the most important and commonly used command-line utility in Unix based systems. It can be used with a sed command. The below code modifies the original files.

-exec sed is used in conjunction with find to execute a command after matching files are found.
{} is used to temporarily store the matching files for execution with sed command.
\; at the end of our statement marks the end of the sed command


Q07. Can you use awk instead of sed?
A07. Yes. awk is a powerful scripting language that is designed for text processing and is often used for data extraction and reporting purposes.

The sed & awk commands are covered in detail in other posts.

variable with default

Q08. What does this Unix snippet do?

A08. If variable “businessdate” is not set or null, use “prevMonthEndDate” as a default.

800+ Java Interview Q&As