Delete files returned by ls


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Delete files returned by ls
# 1  
Old 04-23-2009
Delete files returned by ls

I need to delete the files that are returned by this ls command:

Code:
root@16:06:37@whci03:/var/log/whci# ls -ltrhS | grep "Apr  [234]"
-rw-r--r-- 1 whci  users 180M Apr  4 00:00 20090403_whci_Access.txt
-rw-r--r-- 1 whci  users 185M Apr  4 00:00 20090403_whci_Access.txt
-rw-r--r-- 1 whci  users 189M Apr  4 00:00 20090403_whci_Access.txt
-rw-r--r-- 1 whci  users 190M Apr  2 00:00 20090401_whci_Access.txt
-rw-r--r-- 1 whci  users 193M Apr  2 00:00 20090401_whci_Access.txt
-rw-r--r-- 1 whci  users 194M Apr  2 00:00 20090401_whci_Access.txt
-rw-r--r-- 1 whci  users 201M Apr  3 00:00 20090402_whci_Access.txt
-rw-r--r-- 1 whci  users 204M Apr  3 00:00 20090402_whci_Access.txt
-rw-r--r-- 1 whci  users 205M Apr  3 00:00 20090402_whci_Access.txt

I tired:
Code:
ls -ltrhS | grep "Apr  [234]" | rm -f

but I got the following error msg:

rm: invalid option -- w
Try `rm --help' for more information.
# 2  
Old 04-23-2009
Try this.....(although there might be a better way)


Code:
ls -ltrhS | grep "Apr  [2-4]" | awk -F' ' '{print $9}' | xargs rm -f

You can't pass the output of 'ls -ltrhS' into rm, you only need the filename


EDIT:
Actually if you think about it....without the AWK the rm command looks like this(which is invalid):
Code:
rm -rw-r--r-- 1 whci  users 205M Apr  3 00:00 20090402_whci_Access.txt

Should be:

rm -f 20090402_whci_Access.txt

That's where you were getting the 'invalid option --w'.

Make sense?

Last edited by bassman121; 04-23-2009 at 01:41 PM.. Reason: adding to response
# 3  
Old 04-23-2009
Thanks for the clear explanation bassman.

It makes sense Smilie
# 4  
Old 04-23-2009
use the find command to find the file(s) and remove them....

find *whci_Access.txt -mtime +10 -exec rm {} \;

this will find all files with the name *whci_Access.txt older than 10 days and remove them.

Last edited by theninja; 04-23-2009 at 02:38 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

2. Shell Programming and Scripting

Delete all files if another files in the same directory has a matching occurrence of a specific word

he following are the files available in my directory RSK_123_20141113_031500.txt RSK_123_20141113_081500.txt RSK_126_20141113_041500.txt RSK_126_20141113_081800.txt RSK_128_20141113_091600.txt Here, "RSK" is file prefix and 123 is a code name and rest is just timestamp of the file when its... (7 Replies)
Discussion started by: kridhick
7 Replies

3. UNIX for Dummies Questions & Answers

Concatenate files and delete source files. Also have to add a comment.

- Concatenate files and delete source files. Also have to add a comment. - I need to concatenate 3 files which have the same characters in the beginning and have to remove those files and add a comment and the end. Example: cat REJ_FILE_ABC.txt REJ_FILE_XYZ.txt REJ_FILE_PQR.txt >... (0 Replies)
Discussion started by: eskay
0 Replies

4. Shell Programming and Scripting

how to delete the older files other than the recently added 5 files

Number of files will get created in a folder automatically daily.. so i hav to delete the older files other than the recently added 5 files.. Could u help me through this..?? (5 Replies)
Discussion started by: shaal89
5 Replies

5. Shell Programming and Scripting

Need help creating a script to FTP files to a server and then delete the files that were transfered.

I am trying to FTP files to a Windows server through my Linux machine. I have setup the file transfer with no problems but am having problem deleting those files from the Linux box. My current non-working solution is below. Any ideas, anyone?? :wall: Please be gentle, I'm fairly new to this... (4 Replies)
Discussion started by: jmalfhs
4 Replies

6. Shell Programming and Scripting

Perl, searching multiple files and printing returned line to new file

I am trying to find a way to utilise the full potential of my cpu cores and memory on my windows machine. Now, I am quite familiar with grep, however, running a Unix based OS is not an option right now. Unfortunately, the 32 bit grep for windows that I am running, I cannot run multiple... (1 Reply)
Discussion started by: Moloch
1 Replies

7. Shell Programming and Scripting

Delete all files if another files in the same directory has a matching occurence of a specific word

Hello, I have several files in a specific directory. A specific string in one file can occur in another files. If this string is in other files. Then all the files in which this string occured should be deleted and only 1 file should remain with the string. Example. file1 ShortName "Blue... (2 Replies)
Discussion started by: premier_de
2 Replies

8. UNIX for Dummies Questions & Answers

Delete files returned by ls

I need to delete the files that are returned by this ls command: root@16:06:37@whci03:/var/log/whci# ls -ltrhS | grep "Apr " -rw-r--r-- 1 whci users 180M Apr 4 00:00 20090403_whci_Access.txt -rw-r--r-- 1 whci users 185M Apr 4 00:00 20090403_whci_Access.txt -rw-r--r-- 1 whci users 189M... (1 Reply)
Discussion started by: whci98
1 Replies

9. UNIX for Dummies Questions & Answers

Problem with files returned using Find command

When I enter the command below grep appears to be returning a file it shouldn't. find . -name "*.*" -exec grep "testing" {} /dev/null \; :tps3Mailfile ./SSI.ksh: # create TECHOUT dummy for test for testing purposes ./ftprimi1.ksh:# before running job in prod... change FTP to go to rimi... (3 Replies)
Discussion started by: dfb500
3 Replies

10. UNIX for Dummies Questions & Answers

when I try to run rm on multiple files I have problem to delete files with space

Hello when I try to run rm on multiple files I have problem to delete files with space. I have this command : find . -name "*.cmd" | xargs \rm -f it doing the work fine but when it comes across files with spaces like : "my foo file.cmd" it refuse to delete it why? (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question