Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm trying to figure out if find could do this. I have a folder with 1000 files. I want to delete 150 files on that folder regardless of timestamp and filename. Is there a tool, command or option on find that could do this, please let me know.
Combining mtime or ctime to find is not advisable since it will not count the files or even if there are matches, I would still need to sum up the files until I reach 150 files.
What do you mean "regardless of timestamp and filename"? Which files do you want to remove? How is the script supposed to decide which 150 need to be deleted?
Is it alright if it's sorted by name or size or date or something, or do they HAVE to be 100% random?
I'd like to know what you've tried
But here's some hints, sort has some options to randomize lists of information, head or tail can be set to limit output (perhaps to 150) and while loops can read lines of text from output of commands.
For example:
Code:
while read i; do echo "$i"; done <<< $(command)
See man pages for sort and head or tail.
I don't know if find can delete a random list of exactly 150 though.
However, combining the above makes it pretty easy.
If you don't care which files are deleted, then you can just pipe the output of ls or find to head/tail to trim it to 150 and then pass it on to rm, eg:
Code:
find . | head -n 150 | xargs rm
Of course this will be susceptible to files with "bad" characters in the names (spaces, etc.). Usually you would use -print0 and -0 in xargs to get around that, but then you can't use head/tail. This post has some information on getting around that: http://superuser.com/questions/39711...-option-print0
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150
Rep:
Quote:
Originally Posted by suicidaleggroll
If you don't care which files are deleted, then you can just pipe the output of ls or find to head/tail to trim it to 150 and then pass it on to rm, eg:
Code:
find . | head -n 150 | xargs rm
Of course this will be susceptible to files with "bad" characters in the names (spaces, etc.). Usually you would use -print0 and -0 in xargs to get around that, but then you can't use head/tail. This post has some information on getting around that: http://superuser.com/questions/39711...-option-print0
Small problem with that is find would also include folders etc which would count towards the 150 but would not be removed by the rm command
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.