cancel
Showing results for 
Search instead for 
Did you mean: 

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
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

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

Re: deleting files

>RK: for I in `cat /tmp/abc`

If you can to this, it wasn't a big list. You could do:
rm $(ls -lrt | grep 'Jan 23' | grep gz | awk '{print $9}')

>johnsonpk: command should be ...

You forgot to mention that your xargs solution is faster than the for-loop. :-)

If you know you don't have spaces then this is faster:
... | xargs -n40 rm
R.K. #
Honored Contributor

Re: deleting files

Lot of things happening today :-)
Don't fix what ain't broke
James R. Ferguson
Acclaimed Contributor

Re: deleting files

Hi Shiv:

Doing pipelines with 'grep' (multiple times) and 'awk' is a waste of process resources.

Variations like:

# ls -lrt|grep 'Jan 23'|grep gz|awk '{print $9}'

...to list file names modified "Jan 23" and (presumably) with the '.gz' extention is wasteful and may yield fuzzy matches.

First, 'awk' and 'grep' both do pattern matching, so why use both?

You could do:

# ls -lrt|awk '/Jan 23/ && /\.gz$/ {print $9}'

This lets 'awk' do all the work you want and insures that you only match files that end in '.gz'.

While probably a bit of overkill, you could insure that the date you are matching really belongs to the timestamp column and isn't part of a filename:

# ls -lt|awk '$6=="Jan" && $7==23 && $9~/\.gz$/ {print $9}'

Regards!

...JRF...


Shivkumar
Super Advisor

Re: deleting files

In the below command is it possible to specify option so that it ask befor deletion (like we specify with rm -i somefiles)
?

$ls -lrt|awk '/Aug 10/ && /\.gz$/ {print $9}'

$ls -lrt|awk '/Aug 10/ && /\.gz$/ {print $9}'|xargs -i -l rm

Steven Schweda
Honored Contributor

Re: deleting files

> [...] is it possible [...]

Did you try changing "rm" to "rm -i" in that
command?
James R. Ferguson
Acclaimed Contributor

Re: deleting files

Hi SHiv:

> In the below command is it possible to specify option so that it ask befor deletion (like we specify with rm -i somefiles)

Yes, one way is like this:

# ls -lrt|awk '/Aug 10/ && /\.gz$/ {print $9}'|xargs pp -l rm

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: deleting files

Hi (again) SHiv:

My apologies, there was a typo in my last post. The command should read:

# ls -lrt|awk '/Aug 10/ && /\.gz$/ {print $9}'|xargs -p -l rm

Regards!

...JRF...

Re: deleting files

>In the below command is it possible to specify option so that it ask before deletion?

Are you going to want to hit "y" before every removal? (very big list)
Is there some way to automate your decision?
James R. Ferguson
Acclaimed Contributor

Re: deleting files

Hi Shiv:

To generalize your ability to match the date value in the output from 'ls', you might want to accommodate systems that space-fill and ones that zero-fill the day.

Instead of doing:

ls -l | awk '/Feb 1/ {print $9}'

...with TWO spaces after "Feb" to find files of "Feb 1" but:

# ls -l | awk '/Feb 01/ {print $9}'

...on some systems (like AIX)

...and in the first case, only putting ONE space so that actually "Feb 11" is matched instead, change the regular expression to this:

# ls -l | awk '/Feb[0 ]*1[ ]/ {print $9}'

This matches "Feb" followed by an *optional* zero or space character followed by a "1" and a space. Now change the "1" to "11" and you still differentiate both whether or not the "1" is zero filled or not.

Regards!

...JRF...

Re: deleting files

>JRF: you might want to accommodate systems that space-fill and ones that zero-fill the day.

Not just systems, various locales.
You can scan all your locales to find these evil ones.
for i in $(locale -a); do echo "Doing $i: =="; LANG=$i locale -k d_fmt; done