14
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
this post was submitted on 31 Jan 2025
14 points (81.8% liked)
Linux Mint
3665 readers
3 users here now
Linux Mint is a free Linux-based operating system designed for use on desktop and laptop computers.
Want to see the latest news from the blog? Set the Firefox homepage to:
where is a current or past release. Here's an example using release 21.1 'Vera':
https://linuxmint.com/start/vera/
founded 5 years ago
MODERATORS
The breakdown:
#! /bin/bash- This heads every bash script and is necessary to tell your shell environment what interpreter to use for the script. In this case we're using/bin/bashto execute the script.fileList="$(find ~/path/to/dir/with/html/files -name '*.html' | sort)"- What this command is doing is creating a variable calledfileListusing command substitution. Command substitution encloses a command in"$()"to tellbashto execute the command(s) contained within the substitution group and save the output of the command(s) to the variable. In this case the commands are afindcommand piped into asortcommand.find ~/path/to/dir/with/html/files -name '*.html' | sort- So this is the command set that will execute and the output of this command will be saved to the variablefileList.num=1- Here we're creating a variable callednumwith a value of1. This is for adding sequential numbers to the files as they are moved from their source directory to the destination directory.while IFS= read -r line; do- This script uses awhileloop to process each item saved to thefileListvariable. Within thewhileloop, the moving and renaming of the files will take place.pad="$(printf '%03d' $num)"- This is another variable being created using command substitution. What the command in the substitution group does is take thenumvariable and pad it with zeroes to be a three digit number.printf '%03d' $num- This is the command that runs inside the substitution set.mv $line /path/to/dest/"$pad${line##*/}"- This command actually moves and renames the file.((num++))- Is a nice easy way to increment a number variable inbashdone<<<"$fileList"- Is three parts.doneindicates the end of the while loop.<<<is forheredoc, which is a bash utility that allows you to pass a multiline chunk of text into a command. To pass a variable into a command withheredocyou need to use three less than symbols (<<<). Finally, is the variable holding the chunk of text we want fed into thewhileloop, which is thefileListvariable (double quoted to insure proper expansion ignoring spaces and other nonstandard characters).And that's the script! Let me know how it works for you.