Pipe grep to rm


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Pipe grep to rm
# 1  
Old 09-27-2011
Pipe grep to rm

We have a number of OS X 10.6 machines who have home folders with incorrect permissions, which are causing managed prefs not to be applied correctly and not allowing saving. I need to delete all home folders whose name is numerical and modified before a certain date. I'm not sure of the date part but here is what I have as a command to be run over ARD:

ls /Volumes/Macintosh\ HD/Users | grep '[0-9]' | xargs rm -r

This works up until after the second pipe but doesn't actually remove the folder, just returns me to a command prompt (I'm testing on another machine in Terminal). I'd also like to add the find command after the grep command to narrow it down to folders modified before a certain date (unless grep can do mod dates, haven't had any luck finding that). Any help or suggestions for a n00b?
# 2  
Old 09-27-2011
Take a look at the find command's manual page. find can match a file's name against a pattern, check the file's last access/modification/change time against a specified value, and it can also execute a command (rm in this case) with matching file names.

find(1) alone can do the job. You don't need to use grep or xargs or any other tool for the situation you described.

And, remember, when trying to delete a non-empty directory, you may need to pass to rm the -f option (or, depending on a file's permissions, you may be repeatedly prompted for confirmation).

Regards, good luck, and welcome to the forum,
Alister
This User Gave Thanks to alister For This Post:
# 3  
Old 09-27-2011
That's what I get for too much thinking. Simplified it down to a single find command and worked like charm. Thanks!!

find /Volumes/Macintosh\ HD/Users/ -nouser -delete
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How To Pipe Find To Grep?

hi what wrong with my syntax here? find / -name "*dhcp*" | grep -l -r 'timeout' thanks (3 Replies)
Discussion started by: johnywhy
3 Replies

2. Shell Programming and Scripting

pipe search pattern into a grep

comm -13 tmpfile tmpfile2 | grep -v <filename> >newfile so i want to 1. find records in 1 file bot not in another 2. The output of the first part is 1 field in a file with many fields. 3. find all the records that do not have the value piped from step #1 4. redirect to a new file ... (4 Replies)
Discussion started by: guessingo
4 Replies

3. UNIX for Dummies Questions & Answers

Bash - CLI - grep - Passing result to grep through pipe

Hello. I want to get all modules which are loaded and which name are exactly 2 characters long and not more than 2 characters and begin with "nv" lsmod | (e)grep '^nv???????????? I want to get all modules which are loaded and which name begin with "nv" and are 2 to 7 characters long ... (1 Reply)
Discussion started by: jcdole
1 Replies

4. Shell Programming and Scripting

pipe result from grep

Trying to create a command line script to look for all files matching a pattern, grep for a specific value in each file, and write out the filename long list. It's possible the filename won't containe the value. { echo “Running....” for fname in 811_Intermediate_File_* do grep -l... (3 Replies)
Discussion started by: gavineq
3 Replies

5. Shell Programming and Scripting

pipe output of grep to sed?

Is there a way I can do this: search for text and replace line containing matched text with a different line? For example: "I want to replace text" I want to search for replace and then change the line to I am perplexed. Hope that makes sense. Thanks in advance. (4 Replies)
Discussion started by: arsh
4 Replies

6. Shell Programming and Scripting

problem using a pipe to grep

Hello ! I want to process a text file in order to extract desired data using sed and grep... Now I am facing a problem piping to grep... nothing happens.. The text consists of blocks of 3 lines that may (or not) contain the Desired data. the desired data is on each 2... (4 Replies)
Discussion started by: ShellBeginner
4 Replies

7. Shell Programming and Scripting

pipe grep command

Hi all, Can someone help me with the following problem. I am executing the following command: (search for occurences of 'error' in files that match cl-*.log expression) > grep -cw -i --max-count=1 'error' cl-*.log this command outputs: cl-apache.log:1 cl-apache_error.log:1... (3 Replies)
Discussion started by: epro66
3 Replies

8. Shell Programming and Scripting

Pipe Data From Grep Into A File

I am somewhat of a novice at unix scripting and I need a little help. Here is what im trying to do: I am trying to figure out how to pipe the following grep results into a file. /source/programs: grep "WSBL020" W* WBMB991.cbl: COPY WSBL020. WDCB003.cbl: COPY... (4 Replies)
Discussion started by: katinicsdad
4 Replies

9. UNIX for Dummies Questions & Answers

Filter results through pipe with grep

ls -ltr | grep string How can I use regular expressions to filter the results provided even more. I am using the above command as a reference. (1 Reply)
Discussion started by: ckandreou
1 Replies
Login or Register to Ask a Question