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!

  • skaarl@feddit.nlOP
    link
    fedilink
    English
    arrow-up
    1
    ·
    6 hours ago

    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
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      6 hours ago

      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.nlOP
        link
        fedilink
        English
        arrow-up
        1
        ·
        edit-2
        5 hours ago

        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
          link
          fedilink
          arrow-up
          2
          ·
          5 hours ago

          Here’s the command to delete the files:

          for f in *; do find ./"$f" -type f | sort | tail -n 2 | xargs -n 1 rm; done

          If you want to insure it will target the correct files, first run this command (I HIGHLY recommend you do this first. Verify BEFORE you delete so you don’t lose data):

          for f in *; do find ./"$f" -type f | sort | tail -n 2; done

          I’ll be adding another comment reply with a breakdown of the command shortly (just need to write it up)

          • harsh3466
            link
            fedilink
            arrow-up
            3
            ·
            edit-2
            5 hours ago

            Here’s what’s happening in the command;

            for f in *; do

            You already know this for loop, which is using the * glob to iterate over each directory in the current directory.

            find ./"$f" -type f

            Instead of your original ls command, which gives the file names, and not their full paths, we’re using GNU find, which outputs the full path of what it finds. The arguments are:

            ./"$f" - This tells find where to start its search. I double qouted the $f variable to properly expand the directory name even if it has nonstandard characters in it like spaces.

            -type f - This tells find what kind of file object to look for. So it’s two parts. -type to tell find there will be a specific type to look for, and the f flag, which means file. Meaning, it will only find files

            The output of find is not sorted alaphabetically, so before piping the output to tail, we first pipe it to sort, which by default will sort alphanumerically, which we then pipe to tail to grab just the last two files, and finally we get to the xargs bit.

            Here I added the -n 1 argument to xargs to get it to work on the files one at a time. This isn’t actually necessary. You could just run it as xargs rm. I didn’t realize that before I posted the command. (I’m still learning too! The learning never ends. :D )

            • skaarl@feddit.nlOP
              link
              fedilink
              English
              arrow-up
              1
              ·
              5 hours ago

              Thanks so much harsh!!! I will study this and hit Enter after I understand it.

              Thanks again, that’s epic.

              • harsh3466
                link
                fedilink
                arrow-up
                1
                ·
                5 hours ago

                You’re welcome! Happy I could help.

                One other quick note, do the filenames or directories have spaces in them? If they do, that will cause a problem with the command as it is and need some additional modification. I accounted for the possible spaces in the directory names with the find command, but not with xargs. I just realized that as I was looking it over again.

                • skaarl@feddit.nlOP
                  link
                  fedilink
                  English
                  arrow-up
                  1
                  ·
                  35 minutes ago

                  That was it! Thank you. I got rid of over 150 files in 127 directories with a lot less clicks than through the file explorer.

                  Luckily this time there were no spaces in the names. Spaces in names are a PITA at my stage of learning, and I’m never sure if I should use ’ or ".

                  Btw, new challenge in the edited original post, if you haven’t yet exhausted your thinking quota for the day lol.