As the title says, I just started with linux mint and am falling in love with bash scripts ๐ Actually I'm not sure if it's considered a script, but I want to delete the last 2 files in all subfolders in a folder. So far I've (after great effort) got the terminal to list the files, but I want to delete them. Here is how I get them listed:
for f in *; do ls $f | tail -n 2; done
All their names come satisfyingly up in the terminal. Now what? I tried adding | xargs rm but that didn't delete them. I also tried something with find command but that didn't work either. Some folders have 3 items, so I want to delete #2 and 3. Some folders have 15 items so I want to delete #14 and 15. Folders are arranged by name, so it's always the last 2 that I want to delete.
It's frustrating to be sooooo clooooose, but also very fun. Any help is appreciated!
EDIT: Thanks for the awesome help guys! The next part of this is to move all the .html files into one folder (named "done"), prepending their name with an integer. So far I got:
n=1; for f in *; do find ./"$f" -type f | sort | xargs mv done/"$n$f"; n=$((n+1)); done
but that is... not really doing anything. The closest I have gotten so far is some error like
mv: Missing destination file operand
Any help is again appreciated!
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. :)