harsh3466

joined 2 years ago
[–] [email protected] 34 points 7 months ago

30% is soso? Go fuck yourselves.

[–] [email protected] 2 points 7 months ago (11 children)

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.

[–] [email protected] 4 points 7 months ago* (last edited 7 months ago) (13 children)

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 )

[–] [email protected] 3 points 7 months ago (14 children)

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)

[–] [email protected] 2 points 7 months ago* (last edited 7 months ago) (17 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

[–] [email protected] 1 points 7 months 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.

[–] [email protected] 1 points 7 months ago (19 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.

[–] [email protected] 1 points 7 months ago

Mmmm. Sunny side up. They look delicious

[–] [email protected] 5 points 7 months ago

Oof. I'm glad you're getting better. That sounds rough. Don't overdo it back at work.

[–] [email protected] 4 points 7 months ago (1 children)

I think I fixed it with a variation of turning it off and on again. I deleted vim, deleted ~/.viminfo and deleted /etc/vimrc, then reinstalled. So far it's working as expected.

[–] [email protected] 3 points 7 months ago

I tried opening it without config and it acted even weirder, but that still helped me get towards my solution which is in the update in the post. Short of it is I uninstalled and removed .viminfo and /etc/vimrc then reinstalled

view more: ‹ prev next ›