this post was submitted on 31 Jan 2025
8 points (100.0% liked)

Linux Mint

1875 readers
10 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:

linuxmint.com/start/

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
 

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!

you are viewing a single comment's thread
view the rest of the comments
[โ€“] clmbmb@lemmy.dbzer0.com 2 points 5 hours ago (1 children)
[โ€“] skaarl@feddit.nl 1 points 5 hours ago (4 children)

for f in *; do ls $f | tail -n 2 | xargs rm -rf; done

You mean like that? rm -rf followed by a question mark does not inspire confidence XD

[โ€“] harsh3466@lemmy.ml 1 points 51 minutes ago

Additionally, for safety you can add the i flag to be promoted to confirm each removal. It may be tedious depending on the number of files, but it may also save you from deleting files and/or directories you don't want deleted.

[โ€“] harsh3466@lemmy.ml 1 points 53 minutes ago (1 children)

For clarity, be careful with that -rf combo of flags. As another commenter mentioned, -r means recursive, which will delete directories and their contents. You're talking about deleting files. If you do not want directories and their contents removed, DO NOT use the -r flag.

[โ€“] skaarl@feddit.nl 1 points 38 minutes ago (1 children)

Thank you for the tips, but now I'm getting "Cannot remove: No such file or directory" all the way down! The files are there, I see them, they come up in the terminal, but for some reason xargs rm does not want to delete them. When I put the -f flag, rm doesn't give an error but the files are still there! wtf

[โ€“] harsh3466@lemmy.ml 1 points 23 minutes ago* (last edited 21 minutes ago) (1 children)

When you run the command without the xargs bit, like this:

for f in \*; do ls $f | tail -n 2; done\,

Does the output give you the full file path, or just the file names?

The full file path will look something like:

/dir1/dir2/actual-file

And of course the file name would just be:

some-file

If you're getting just the file name, that's the problem. Unless you're in the directory with the file you wish to delete, rm needs the full path.

Edit: grammar

[โ€“] skaarl@feddit.nl 1 points 3 minutes ago* (last edited 56 seconds ago) (1 children)

Yea that must be it! It's spitting out just the file name and not the whole path. There is only 1 level of depth, so I want to remove

  • ./folder1/file 3
  • ./folder1/file4
  • ./folder2/file11
  • ./folder2/file12

so how do I get the whole path into xargs? I tried xargs "$f"/ but fortunately that didn't work because it was trying to delete all the directories lmao XD

[โ€“] harsh3466@lemmy.ml 1 points 1 minute ago

I'm working on a new command to do this, give me a few minutes and I should have it.

[โ€“] huf@hexbear.net 1 points 4 hours ago (1 children)

this will break pretty badly if you have filenames with spaces or newlines in them. so to make this actually robust, you now get to learn about find -print0, xargs -0, and why you always, always need to add "" around variables in bash.

[โ€“] skaarl@feddit.nl 1 points 39 minutes ago (1 children)

Can find be used with tail?

Thank you for the tips, but now I'm getting "Cannot remove: No such file or directory" all the way down! The files are there, I see them, they come up in the terminal, but for some reason xargs rm does not want to delete them. When I put the -f flag, rm doesn't give an error but the files are still there! wtf

[โ€“] huf@hexbear.net 1 points 32 minutes ago

find can be used with tail, but if you're doing nul-delimited stuff (-print0, -0), then you'll want tail to run in nul-delimited mode too (-z apparently).

or you can say "fuck files with newlines in them, i aint supporting that shit", and then you just need the "" to still support filenames with spaces.

[โ€“] clmbmb@lemmy.dbzer0.com 1 points 4 hours ago (1 children)

yes. that's what I suggested.. the question mark was there to ask you if you tried that :-D I'm at work, pretty busy :-D I hope you read the rm manual.

-r means recursive
-f means force, which will delete the files/directories without interaction

[โ€“] skaarl@feddit.nl 1 points 38 minutes ago

Oh I see, lol. Now I'm getting "Cannot remove: No such file or directory" all the way down! The files are there, I see them, they come up in the terminal, but for some reason xargs rm does not want to delete them. When I put the -f flag, rm doesn't give an error but the files are still there! wtf