LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 02-09-2015, 12:54 PM   #1
packets
Member
 
Registered: Oct 2005
Posts: 319

Rep: Reputation: 32
how to delete number of files


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.

Any suggestions?
 
Old 02-09-2015, 01:04 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,482

Rep: Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089
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?
 
Old 02-09-2015, 01:14 PM   #3
packets
Member
 
Registered: Oct 2005
Posts: 319

Original Poster
Rep: Reputation: 32
Quote:
Is it alright if it's sorted by name or size or date or something, or do they HAVE to be 100% random?
If it could 100% random, that would be great but if there is no tool for that then lets sort by name e.g. A-Z,a-z.

Quote:
What do you mean "regardless of timestamp and filename"?
each files has their own unique filename and there is no pattern.

TIA
 
Old 02-09-2015, 01:30 PM   #4
Miati
Member
 
Registered: Dec 2014
Distribution: Linux Mint 17.*
Posts: 326

Rep: Reputation: 106Reputation: 106
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.

Last edited by Miati; 02-09-2015 at 01:34 PM.
 
Old 02-09-2015, 01:35 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,482

Rep: Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089
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
 
1 members found this post helpful.
Old 02-09-2015, 06:52 PM   #6
packets
Member
 
Registered: Oct 2005
Posts: 319

Original Poster
Rep: Reputation: 32
Thanks guys! Why I don't think of using head. I haven't tried it yet but I'm expecting this will work.
 
Old 02-09-2015, 06:58 PM   #7
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 491Reputation: 491Reputation: 491Reputation: 491Reputation: 491
A slight modification for randomness and better file name handling:

Code:
find -type f -print0 | shuf -z -n 150 | xargs -0 rm -f
 
Old 02-09-2015, 07:01 PM   #8
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,403

Rep: Reputation: 603Reputation: 603Reputation: 603Reputation: 603Reputation: 603Reputation: 603
Quote:
Originally Posted by suicidaleggroll View Post
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
 
Old 02-09-2015, 07:46 PM   #9
Miati
Member
 
Registered: Dec 2014
Distribution: Linux Mint 17.*
Posts: 326

Rep: Reputation: 106Reputation: 106
Quote:
Originally Posted by Keith Hedger View Post
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
Not hard to get around

Code:
find . -type f | head -n 150 | xargs rm
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] vimscript: how delete a variable number of lines porphyry5 Programming 3 09-09-2013 07:53 PM
Delete large number files along with hard links mohan.1418 Linux - Newbie 7 06-06-2012 08:59 AM
How to check for number of folders, then delete IF ... rbalaa Linux - General 4 04-10-2010 09:17 AM
Can I delete files in /mnt/tmp? and Files in the trash can will not delete? M$ISBS Slackware 15 10-02-2009 11:56 PM
To delete a number from a particular position som_kurian Programming 1 01-01-2008 04:01 PM


All times are GMT -5. The time now is 11:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration