**The Challenge
**Write a Bash script based on the following requirements:
Creates 25 empty (0 KB) files. (Hint: Use the touch command.)
The file names should be , , , and so on.
Design the script so that each time you run it, it creates the next batch of 25 files with increasing numbers starting with the last or maximum number that already exists.
Do not hard code these numbers. You need to generate them by using automation.
Test the script. Display a long list of the directory and its contents to validate that the script created the expected files.
At first glance I'm thinking I need a way to check the system for current number of files, so I start searching:
I'll need a dynamic loop, checking system files for numbers so I search for lookup methods, grep comes up and find comes up as well:
[Link(https://stackoverflow.com/questions/24655436/how-can-i-find-a-file-directory-that-could-be-anywhere-on-linux-command-line)] To get rid of permission errors (and such), you can redirect stderr to nowhere
find / -name "something" 2>/dev/null
I've heard of this null thing before, and it's pretty neat. I found an interesting reason to use this, the 2> is a bash syntax to tell the system to redirect stderr (2) so all errors will be ignored, and the carrot > redirects stdout. I was doing some bandit over the wire hacking, and often times to get around the permissions errors, you chmod 400 the file if you have permission if not copy file and make a tmp directory and paste into that directory and work on it there.
Here's the final code (we can walk through it as a team):
`#!/bin/bash
yourName="zach"
logFile="file_creation.log"
Get list of existing files matching the pattern
existingFiles=$(ls ${yourName}[0-9]* 2>/dev/null)
Extract the highest number from existing files
if [ -z "$existingFiles" ]; then
maxNum=0
else
maxNum=$(echo "$existingFiles" | grep -oP "${yourName}\K[0-9]+" | sort -n | tail -1)
fi
Timestamp for this batch
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo "[$timestamp] Creating files starting from ${yourName}$((maxNum + 1)) to ${yourName}$((maxNum + 25))" >> "$logFile"
Create 25 new files and log each one
for ((i=1; i<=25; i++)); do
newNum=$((maxNum + i))
fileName="${yourName}${newNum}"
touch "$fileName"
echo "[$timestamp] Created $fileName" >> "$logFile"
done
Show the results
echo "Created files:"
ls -l ${yourName}[0-9]* | tail -25
`
!/bin/bash -> tells system to use Bash to interpret the code
yourName="zach"
logFile="file_creation.log"
--> setting up basic static variables:
existingFiles=$(ls ${yourName}[0-9]* 2>/dev/null)
--> 2>/dev/null suppresses errors if no files exist yet. Lists all files match pattern.
if [ -z "$existingFiles" ]; then
maxNum=0
else
maxNum=$(echo "$existingFiles" | grep -oP "${yourName}\K[0-9]+" | sort -n | tail -1)
fi
--> checks if any matching files exist, if none exist start numbering from 0, If files do exist:
grep -oP "${yourName}\K[0-9]+" extracts just the number part from filenames.
sort -n sorts numerically.
tail -1 grabs the highest number so we can start from there.
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
--> captures date saves for logging
echo "[$timestamp] Creating files starting from ${yourName}$((maxNum + 1)) to ${yourName}$((maxNum + 25))" >> "$logFile"
--> logs batch file_creation.log
for ((i=1; i<=25; i++)); do
newNum=$((maxNum + i))
fileName="${yourName}${newNum}"
touch "$fileName"
echo "[$timestamp] Created $fileName" >> "$logFile"
done
--> meat of the operation here, 25 loops calculates next num +1 - creates files with touch, logs them with time stamp
echo "Created files:"
ls -l ${yourName}[0-9]* | tail -25
--> displays last 25 files (can use head as well to see the first 25 but doesn't add much value.
Now that's all of the code. It's a lot to take in as a beginner bash scripter. One thing to take note of is the programmatic approach to solve this, you can't hard code the for loop and just have it work. It needs to do a few things so good to research grep, find, sort, tail functions in bash.
Wish you all the best,
Top comments (0)