[SOLVED] How to delete large no of files with grep
Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
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 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).
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.
Thanx a lot, The script really worked. Resolved my issue.
Quote:
Originally Posted by TenTenths
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.
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.
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
any other ways to delete these files(only for year 2015).
and TeamLQ responded in spades.
Perhaps they just need some encouragement/links/examples...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.