0
0
Bash-scriptingConversionBeginner · 2 min read

Bash Script to Convert Uppercase to Lowercase File

Use the Bash command tr '[:upper:]' '[:lower:]' < inputfile > outputfile to convert all uppercase letters in a file to lowercase.
📋

Examples

InputHELLO WORLD
Outputhello world
InputThis Is A Test FILE.
Outputthis is a test file.
Input1234!@#$
Output1234!@#$
🧠

How to Think About It

To convert uppercase letters to lowercase in a file, read the file content and replace each uppercase letter with its lowercase equivalent. This can be done by mapping uppercase characters to lowercase ones using a simple translation command in Bash.
📐

Algorithm

1
Get the input file name.
2
Read the content of the input file.
3
Replace all uppercase letters with lowercase letters.
4
Write the converted content to a new output file.
5
Print a message confirming the conversion.
💻

Code

bash
#!/bin/bash

input_file="$1"
output_file="$2"

if [[ ! -f "$input_file" ]]; then
  echo "Input file does not exist."
  exit 1
fi

tr '[:upper:]' '[:lower:]' < "$input_file" > "$output_file"
echo "Converted $input_file to lowercase and saved as $output_file."
Output
Converted SAMPLE.TXT to lowercase and saved as output.txt.
🔍

Dry Run

Let's trace converting 'HELLO WORLD' from input file SAMPLE.TXT to output file output.txt.

1

Check if input file exists

Input file SAMPLE.TXT exists.

2

Read input file content

Content read: 'HELLO WORLD'

3

Convert uppercase to lowercase

Converted content: 'hello world'

4

Write to output file

Output file output.txt now contains 'hello world'

5

Print confirmation

Printed message: Converted SAMPLE.TXT to lowercase and saved as output.txt.

StepActionValue
1Input file checkSAMPLE.TXT exists
2Read contentHELLO WORLD
3Convert contenthello world
4Write outputoutput.txt with 'hello world'
5Print messageConverted SAMPLE.TXT to lowercase and saved as output.txt.
💡

Why This Works

Step 1: Reading the file

The script reads the input file content using input redirection with <.

Step 2: Translating characters

The tr command replaces all uppercase letters [:upper:] with lowercase letters [:lower:].

Step 3: Saving output

The converted text is saved to the output file using output redirection with >.

🔄

Alternative Approaches

Using awk
bash
awk '{print tolower($0)}' inputfile > outputfile
This uses awk's tolower function; it is easy to read but may be slower on large files.
Using sed
bash
sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' inputfile > outputfile
This uses sed's transliteration; it works well but is less readable than tr.

Complexity: O(n) time, O(n) space

Time Complexity

The script processes each character once, so time grows linearly with file size.

Space Complexity

The output file requires space proportional to the input file size; the script uses minimal extra memory.

Which Approach is Fastest?

The tr command is generally fastest and simplest for this task compared to awk or sed.

ApproachTimeSpaceBest For
tr commandO(n)O(n)Simple and fast character translation
awk tolowerO(n)O(n)More readable, supports complex processing
sed transliterationO(n)O(n)Works well but less intuitive
💡
Always check if the input file exists before converting to avoid errors.
⚠️
Forgetting to redirect input and output properly, causing no changes or overwriting the original file unintentionally.