LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 04-21-2010, 08:27 AM   #1
shravee
Member
 
Registered: Sep 2009
Posts: 75

Rep: Reputation: 15
how to delete selectively


hi all,
In the below screenshot I need to delete only the files starting with " WIReportServer " and "ccm".
also, it should not be greater than Feb. i.e. I can delete only old files till Feb end. I should not touch march or april files.

I tried. ls -ltr |find ccm* but ls comd did not have any effect.. instead. only the output of find ccm* was working.

plz help me out with this.

regards.
shravee




rwxr-xr-x 1 cabodvg boste 1465 Feb 07 09:02 ccm_20100207_1265551322687.log
-rwxr-xr-x 1 cabodvg boste 3610 Feb 14 03:20 boe_cmsd_20100207_140204_2097176.log
-rwxr-xr-x 1 cabodvg boste 2186 Feb 14 06:08 ccm_20100214_1266145682115.log
-rwxr-xr-x 1 cabodvg boste 1465 Feb 14 09:02 ccm_20100214_1266156122815.log
-rwxr-xr-x 1 cabodvg boste 2021 Feb 18 03:20 boe_cmsd_20100214_140207_1495290.log
-rwxr-xr-x 1 cabodvg boste 8725 Feb 18 10:14 WIReportServer_20100214_140212_2539616.log
-rwxr-x--- 1 cabodvg boste 2186 Feb 18 10:25 ccm_20100218_1266506715395.log
-rwxr-x--- 1 cabodvg boste 1465 Feb 18 10:25 ccm_20100218_1266506725350.log
-rwxr-x--- 1 cabodvg boste 11259 Feb 19 07:09 WIReportServer_20100218_152528_1454202.log
-rwxr-x--- 1 cabodvg boste 1029 Feb 20 03:20 boe_cmsd_20100218_152526_2031674.log
-rwxr-x--- 1 cabodvg boste 2186 Feb 20 06:29 ccm_20100220_1266665383038.log
-rwxr-x--- 1 cabodvg boste 1465 Feb 20 18:47 ccm_20100220_1266709621424.log
-rwxr-x--- 1 cabodvg boste 533 Feb 21 03:20 boe_cmsd_20100220_234703_1798264.log
-rwxr-xr-x 1 cabodvg boste 2186 Feb 21 06:08 ccm_20100221_1266750481118.log
-rwxr-xr-x 1 cabodvg boste 1465 Feb 21 09:02 ccm_20100221_1266760922509.log
-rwxr-xr-x 1 cabodvg boste 4019 Feb 24 12:06 WIReportServer_20100221_140205_1740972.log
-rwxr-xr-x 1 cabodvg boste 1123 Feb 24 15:25 WIReportServer_20100224_170713_1740984.log
-rwxr-xr-x 1 cabodvg boste 6915 Feb 26 07:27 WIReportServer_20100224_203241_1740996.log
-rwxr-xr-x 1 cabodvg boste 399 Feb 26 09:51 WIReportServer_20100226_124102_1741008.log
-rwxr-xr-x 1 cabodvg boste 3787 Feb 28 03:20 boe_cmsd_20100221_140203_2113554.log
-rwxr-xr-x 1 cabodvg boste 2186 Feb 28 06:08 ccm_20100228_1267355282911.log
-rwxr-xr-x 1 cabodvg boste 1465 Feb 28 09:02 ccm_20100228_1267365722329.log
-rwxr-xr-x 1 cabodvg boste 15607 Mar 04 05:04 WIReportServer_20100228_140210_1826974.log
-rwxr-xr-x 1 cabodvg boste 1837 Mar 04 06:09 WIReportServer_20100304_101148_905310.log
-rwxr-xr-x 1 cabodvg boste 2557 Mar 04 07:02 WIReportServer_20100304_111303_905322.log
-rwxr-xr-x 1 cabodvg boste 5437 Mar 05 04:29 WIReportServer_20100304_120928_905334.log
-rwxr-xr-x 1 cabodvg boste 2557 Mar 05 15:17 WIReportServer_20100305_093241_905346.log
-rwxr-xr-x 1 cabodvg boste 3663 Mar 07 03:20 boe_cmsd_20100228_140204_1249444.log
-rwxr-xr-x 1 cabodvg boste 2185 Mar 07 06:08 ccm_20100307_1267960081502.log
-rwxr-xr-x 1 cabodvg boste 1465 Mar 07 09:02 ccm_20100307_1267970522614.log
-rwxr-xr-x 1 cabodvg boste 58681 Mar 12 17:07 WIReportServer_20100307_140209_1949698.log
-rwxr-xr-x 1 cabodvg boste 3783 Mar 14 03:20 boe_cmsd_20100307_140203_1896676.log
-rwxr-xr-x 1 cabodvg boste 2185 Mar 14 06:08 ccm_20100314_1268561282797.log
-rwxr-xr-x 1 cabodvg boste 1465 Mar 14 09:02 ccm_20100314_1268571723111.log
-rwxr-xr-x 1 cabodvg boste 533 Mar 15 03:20 boe_cmsd_20100314_130205_2736314.log
-rwxr-xr-x 1 cabodvg boste 5105 Mar 15 16:54 WIReportServer_20100314_130208_2375728.log
-rwxr-x--- 1 cabodvg boste 2185 Mar 15 17:19 ccm_20100315_1268687942856.log
-rwxr-x--- 1 cabodvg boste 1465 Mar 15 21:57 ccm_20100315_1268704661420.log
-rwxr-x--- 1 cabodvg boste 94881 Mar 20 20:07 WIReportServer_20100316_015745_1773674.log
-rwxr-x--- 1 cabodvg boste 4005 Mar 21 03:20 boe_cmsd_20100316_015743_1704008.log
-rwxr-xr-x 1 cabodvg boste 2186 Mar 21 06:08 ccm_20100321_1269166080847.log
 
Old 04-21-2010, 08:36 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
First, what was intended by the pipe? (You can only use a pipe if the second command expects the input that is produced by the first.)

You probably want something of the general form:
find <path> <search criteria> -exec rm {} \;

OR:

for file in $(ls -lr|grep <keyword>|grep <keyword>); do rm $file; done

Note that you can string together as many grep statements as it takes
 
Old 04-21-2010, 08:44 AM   #3
shravee
Member
 
Registered: Sep 2009
Posts: 75

Original Poster
Rep: Reputation: 15
so can i try

ls -ltr | grep ccm* | grep WIReportServer*

but how will i sort the time stamp
 
Old 04-21-2010, 08:48 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
sort's --month-sort option?
 
Old 04-21-2010, 08:54 AM   #5
shravee
Member
 
Registered: Sep 2009
Posts: 75

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by catkin View Post
sort's --month-sort option?
i did not understand. do u want me to use.
ls -ltr | grep ccm * | grep WI* --month Jan

but when i tried ls -month Jan. It says jan not found.

regards
shravee
 
Old 04-21-2010, 09:07 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
He said the --month-sort option to the sort command (not grep). Look at the man pages for grep and sort.

To search for keyword1 OR keyword2, you would not use a pipe (that gives an AND function)

For OR, you want the Regex alternation operator---e.g.:

egrep "(ccm|WI)"

Finally, don't use * in grep keywords--at least not as a wildcard. In a regex, * has a totally different meaning.
 
Old 04-21-2010, 09:30 AM   #7
shravee
Member
 
Registered: Sep 2009
Posts: 75

Original Poster
Rep: Reputation: 15
hi pixellany,
I am looking for an AND operation. I need to delete both log types.
one which start with ccm and
other which starts with WI

and also . i should do this only till feb, march or later shud not touched.

can someone tell me the simple cmd. to execute this. Its killing my time to delte it individually.

regards
shravee
 
Old 04-21-2010, 10:19 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
A bit tricky but a find command like this should do the trick
Code:
touch -t 201003010000 /tmp/reftime && find /path/to/dir \( \( -name WIReportServer_\* -o -name ccm_\* \) -a ! -newer /tmp/reftime \) -exec ls -l "{}" \; && rm /tmp/reftime
The trick is to create a temporary file with the pivot date and to use the -newer option of find (with the negation operator ! in front of it, so that it actually results in "-older").

The command above lists all the files matching your criteria in the directory /path/to/dir. Check the results and if it's all right, substitute "ls -l" with "rm" to actually remove the files. See man find and man touch for details.
 
Old 04-21-2010, 10:37 AM   #9
shravee
Member
 
Registered: Sep 2009
Posts: 75

Original Poster
Rep: Reputation: 15
but coluix, how to sort files based on month. I have not sorted the file anywhere based on month.

I also should delte only older files spicific to feb month.

how will i append to the above cmd. ?

regards
shravee
 
Old 04-21-2010, 10:58 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by shravee View Post
but coluix, how to sort files based on month. I have not sorted the file anywhere based on month.
Why do you need to sort something? If you want to selectively remove some files, find will give you the instruments to refine the search criteria and take an action on the results (remove the files, in this case). I cannot see the need for sorting, here.
Quote:
I also should delte only older files spicific to feb month.
Look carefully at each passage of my command line. The first command (touch) creates an empty file with timestamp set at 1-Mar-2010 00:00. Then find uses this timestamp to find all files older than that (! -newer) that is all WIReportServer_ and ccm_ files whose modification time is before 1-Mar-2010 00:00.
Quote:
how will i append to the above cmd. ?
Sorry but... append to what? I thought you were looking for a command line to execute in one-shot. Just copy/paste it into your terminal but change "/path/to/dir" with the actual path of the directory that contains the files you want to remove.

If I did not understand something, please tell me again.
 
Old 04-21-2010, 12:03 PM   #11
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
Originally Posted by shravee View Post
hi pixellany,
I am looking for an AND operation. I need to delete both log types.
one which start with ccm and
other which starts with WI
No--you need OR

If you write:

"grep this OR that"--it means select any line containing "this" or "that"---hence it returns both

"grep this AND that" ---means the the line has to contain both--this does not fit your problem statement. (There is no syntax for "grep this and that"--which I why I said to string 2 together.)
 
Old 04-21-2010, 12:17 PM   #12
shravee
Member
 
Registered: Sep 2009
Posts: 75

Original Poster
Rep: Reputation: 15
hi colucix,

thanks a lot.. u have finally provided the solution.. realy a guru. hats off to you.

regards
shravee
 
Old 04-21-2010, 12:30 PM   #13
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You're welcome! find is a very powerful command if used properly. I hope you've learned something useful.
 
  


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
Using VPN selectively? fortknox Linux - Newbie 4 01-26-2010 03:32 PM
Postfix email routing selectively abhinandan Linux - Server 2 11-13-2008 04:49 AM
How to selectively install from a RPM areftaidi Fedora 2 06-01-2007 12:26 PM
Can you untar selectively? stefaandk Linux - Newbie 1 06-14-2005 10:15 PM
How to selectively change permissions Xzanron Linux - Newbie 8 01-31-2005 08:59 AM


All times are GMT -5. The time now is 02:58 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