LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 09-14-2016, 02:54 AM   #1
deva420patra
LQ Newbie
 
Registered: Apr 2016
Posts: 29

Rep: Reputation: Disabled
Question How to delete large no of files with grep


Dear Team

I need to delete lots files in a folder (wc is around 2392934). The folder contains files of year 2015 & year 2016.
I want to delete files for year 2015 only and for that I have created a script below:-

echo 'going to gived path'
cd /usr/local/nagios/var/spool/xidpe/
pwd
b=$(ls -lrt | grep '2015' | awk '{print $9}')
echo 'listen tail outcome'
echo 'going to del'
rm -rf $b
exit

but it gives error :- ./test.sh: line 10: /bin/rm: Argument list too long.

Is there any changes need to be done in my script or any other ways to delete these files(only for year 2015).
 
Old 09-14-2016, 03:01 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
just try xargs
 
Old 09-14-2016, 03:26 AM   #3
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,484

Rep: Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556
Did you try:
Code:
find /usr/local/nagios/var/spool/xidpe/ -type f -name "*2015*" -delete
Try it first without the final -delete to ensure it's going to give the correct results.
 
Old 09-14-2016, 03:32 AM   #4
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,484

Rep: Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556
If you really, really, really want to use grep then you could try (untested):

Code:
echo 'going to gived path'
cd /usr/local/nagios/var/spool/xidpe/
pwd
ls -lrt | grep '2015' | awk '{print $9}' | while read -r FILE ; do
  echo 'listen tail outcome'
  echo 'going to del'
  rm -f "${FILE}"
done
exit
Also, in your initial script you use rm -rf which would be dangerous if you've any folders with 2015 in the folder name as it would delete everything inside and under that folder. (Which may be your intended result, but I'm just pointing it out.

Last edited by TenTenths; 09-14-2016 at 03:37 AM. Reason: Quotes around ${FILE} to escape spaces etc.
 
1 members found this post helpful.
Old 09-14-2016, 03:43 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,153

Rep: Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125
Scary.
Better to use find - then you can decide between -delete or -exec. Much more controllable.
 
Old 09-14-2016, 05:06 AM   #6
deva420patra
LQ Newbie
 
Registered: Apr 2016
Posts: 29

Original Poster
Rep: Reputation: Disabled
Smile

Thanx a lot, The script really worked. Resolved my issue.


Quote:
Originally Posted by TenTenths View Post
If you really, really, really want to use grep then you could try (untested):

Code:
echo 'going to gived path'
cd /usr/local/nagios/var/spool/xidpe/
pwd
ls -lrt | grep '2015' | awk '{print $9}' | while read -r FILE ; do
  echo 'listen tail outcome'
  echo 'going to del'
  rm -f "${FILE}"
done
exit
Also, in your initial script you use rm -rf which would be dangerous if you've any folders with 2015 in the folder name as it would delete everything inside and under that folder. (Which may be your intended result, but I'm just pointing it out.
 
Old 09-14-2016, 06:38 AM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,359
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
Quote:
Originally Posted by TenTenths View Post
Did you try:
Code:
find /usr/local/nagios/var/spool/xidpe/ -type f -name "*2015*" -delete
Try it first without the final -delete to ensure it's going to give the correct results.
The utility "find" also has some simple regex capabilities using the -regex option. But it's missing built-in PCRE but that can be worked around:

Since "find" does a logical AND between tests, you could use an extra -exec to run "perl" and get full perl regular expressions.

Code:
find . -name '*' -exec perl -e 'if ( q({}) =~ /2015/ ) { exit 0 } else { exit 1 }' \; -exec echo '-delete "{}"' \; -print
 
Old 09-14-2016, 07:14 PM   #8
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by deva420patra View Post
I want to delete files for year 2015 only and for that I have created a script below:-

Code:
echo 'going to gived path'
cd /usr/local/nagios/var/spool/xidpe/
pwd
b=$(ls -lrt | grep '2015' | awk '{print $9}')
echo 'listen tail outcome'
echo 'going to del'
rm -rf $b
exit
Sorry, but no.

Code:
b=$(ls -lrt | grep '2015' | awk '{print $9}')
is a train-wreck.
See http://mywiki.wooledge.org/BashPitfalls
for better example of how to ls stuff and process it.

Observations:
Code:
ls -lrt
output may vary on different systems as "ls" is aliased all over the place these days.
To illustrate run
Code:
bash --norc
then
Code:
ls -ltr
Try find with -mtime or -ctime? See also http://www.cyberciti.biz/faq/howto-f...files-by-date/

Let us know!

Last edited by Habitual; 09-14-2016 at 07:15 PM.
 
Old 09-14-2016, 07:50 PM   #9
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Nice link, going to read that

Quote:
Thanx a lot, The script really worked. Resolved my issue.
Why did you use this when there are better, safer and simpler methods? It was provided as a untested pseudo example.
Use proper methods and tools. It will save you from data loss / headache in the future.
 
Old 09-14-2016, 10:14 PM   #10
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by Sefyir View Post
Nice link, going to read that



Why did you use this when there are better, safer and simpler methods? It was provided as a untested pseudo example.
Use proper methods and tools. It will save you from data loss / headache in the future.
Well, they did say
Quote:
Originally Posted by deva420patra View Post
any other ways to delete these files(only for year 2015).
and TeamLQ responded in spades.
Perhaps they just need some encouragement/links/examples...
 
Old 09-15-2016, 01:15 AM   #11
balvinders
LQ Newbie
 
Registered: Sep 2016
Posts: 5

Rep: Reputation: Disabled
i m also serching for the same. plz help me. thanks in advance
 
Old 09-15-2016, 01:43 AM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
what kind of help do you need? Where did you stuck?
 
  


Reply



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] LibreOffice slow to open moderately large to pretty large files adanedhel728 Ubuntu 17 01-16-2016 03:46 PM
[SOLVED] What are these large files and may I safely delete them? MBA Whore Ubuntu 10 03-19-2015 01:26 AM
Delete large number files along with hard links mohan.1418 Linux - Newbie 7 06-06-2012 08:59 AM
ext3 performance -- very large number of files, large filesystems, etc. td3201 Linux - Server 5 11-25-2008 09:28 AM
I can't delete some large weird files Hosferatu Linux - Newbie 1 08-19-2008 11:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 02:45 PM.

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
Open Source Consulting | Domain Registration