15
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
15 points (100.0% liked)
Linux Mint
1916 readers
1 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 3 years ago
MODERATORS
๐๐๐๐ thanks harsh!! I'll study this and report back. I really appreciate your time and effort. There is a lot to learn here, and actually the padding is on my list of things to learn, so thank you sensei! As to your question about the integers, the files need to be in alphabetical order before getting the integer prepended to them, so like
turns to
that way in the folder when it's all said and done I'll have
I'll check if your method works out of the box for that or if I have to use the sort function like you showed me last time. Thanks again!
It won't work the way you need it to as is, since the find command will first move all of those files together into the same folder, and if there are similar names between folder, like:
Then it will throw errors and/or overwrite files.
I'm at work so I can't loom at this right now but I'll look at it later when I get home.
Yea, I just came back to say this. Since cp overwrites by default (I tried copying first before trying moving) and each folder has files named index001 index002 etc then then folder where they all go has only ONE of index001.html, ONE of index002.html etc. So I think what I need to do is find each html file, rename it with a unique integer in front of the name, move it to the common folder.
Okay, took me awhile to write everything up. The script itself is pretty short, but it's still much easier to do in a script than to try to make this a one line command.
I tested this creating a top level directory, and then creating three subdirectories inside it with a different number of html files inside those directories, and it worked perfectly. I'm going to break down exactly what's going on in the script, but do note the two commented commands. I set this script up so you can test it before actually executing it on your files. In the breakdown of the script I'm going to ignore the testing command as if it were not in the script.
The script:
The breakdown of the script is in the reply comment. Lemmy wouldn't let me post it as one comment.
Hey I finally got to try this out and tbh I hit Enter without understanding the whole thing ๐คญ๐คซ but anyway it's perfect! And it left me with a lot to study, your explanation was really helpful. Thanks so much for all your help! I really appreciate the time you spent :)
Excellent! I'm glad it worked for you. :)
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/bash
to execute the script.fileList="$(find ~/path/to/dir/with/html/files -name '*.html' | sort)"
- What this command is doing is creating a variable calledfileList
using command substitution. Command substitution encloses a command in"$()"
to tellbash
to 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 afind
command piped into asort
command.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 callednum
with 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 awhile
loop to process each item saved to thefileList
variable. Within thewhile
loop, 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 thenum
variable 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 inbash
done<<<"$fileList"
- Is three parts.done
indicates 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 withheredoc
you need to use three less than symbols (<<<
). Finally, is the variable holding the chunk of text we want fed into thewhile
loop, which is thefileList
variable (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.
Agree. I think for that a bash script will be a better approach. I'll be off work in a few hours. I'll take a look then.