1753479 Members
4843 Online
108794 Solutions
New Discussion юеВ

deleting files

 
SOLVED
Go to solution
Shivkumar
Super Advisor

deleting files

Hi,

I want to delete the output of this command which is a very big list:

$ls -lrt|grep 'Jan 23'|grep gz

How to delete it ?

Thanks,
Shiv
19 REPLIES 19
Johnson Punniyalingam
Honored Contributor
Solution

Re: deleting files

$ls -lrt|grep 'Jan 23'|grep gz|wc -l -> check how many files or lines

$ls -lrt |grep 'Jan 23'|grep gz |rm

Problems are common to all, but attitude makes the difference
johnsonpk
Honored Contributor

Re: deleting files

Hi Shiv,
verify the targeted files

$ls -lrt |grep "Jan 23" |grep ".gz" |awk '{print $9}'|xargs -i -l echo {}

once verified
$ls -lrt |grep "Jan 23" |grep ".gz" |awk '{print $9}'|xargs -i -l rm {}

there are many alternate methods ..thought to modify the command you used for listing files


Johnson
R.K. #
Honored Contributor

Re: deleting files

Hi Shiv,

$ls -lrt | grep 'Jan 23' | grep gz > /tmp/abc

for I in `cat /tmp/abc`
do
rm $I
done
Don't fix what ain't broke
Johnson Punniyalingam
Honored Contributor

Re: deleting files

You can also use "find command with time frame" as below will delete "& days older files

List before you delete.
=======================

find /folder_name -name "*.gz" -mtime +7 -exec ll {} \;

Remove files after confirmed
============================

find /folder_name -name "*.gz" -mtime +7 -exec rm {} \;
Problems are common to all, but attitude makes the difference
TARUN SHARMA_1
Advisor

Re: deleting files

ls -lrt | grep "Jan 23" | grep .gz > /tmp/abc

for I in `cat /tmp/abc`
do
rm $I
done

It will remove all the zipped file of Jan 23 from the current folder.
Sr Tech Lead
Tech Mahindra
Data Canter Tubli Bahrain
johnsonpk
Honored Contributor

Re: deleting files


R.K & Tarun>>

$ls -lrt | grep 'Jan 23' | grep gz > /tmp/abc

for I in `cat /tmp/abc`
do
rm $I
done

ls -lrt command output have 9 fields , in order to delete you need to extract the file name from ls -lrt out put


comand should be

ls -lrt | grep 'Jan 23' | grep gz |awk '{print $9}' >/tmp/abc
R.K. #
Honored Contributor

Re: deleting files

I meant:
$ls | grep 'Jan 23' | grep gz > /tmp/abc

Don't fix what ain't broke
johnsonpk
Honored Contributor

Re: deleting files

>>I meant:
$ls | grep 'Jan 23' | grep gz > /tmp/abc

again ls wont display time stamp and grep with "Jan 23" wont have any effect on the out put

R.K. #
Honored Contributor

Re: deleting files

Sorry..I missed the date part f the question when trying on a test box.
Don't fix what ain't broke