Pass grep's Output to mv?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Pass grep's Output to mv?
# 1  
Old 05-24-2012
Pass grep's Output to mv?

Hi!
I was looking for files to clean out, and I remembered reading somewhere that you could pipe the output of grep to rm. But I prefer to mv stuff to ~/.Trash/
I thought I'd try something like this:

Code:
ls  ~/Library/Preferences/ |grep '.*.\.iTunes\.plist\.[0-9].*' |xargs -J % mv % ~/.Trash/

I tried it a couple of different ways, but can't get it to work. I usually end up with the mv "usage" message. I was thinking that maybe mv wasn't receiving the full path, so I cd'ed to the source dir, but that didn't help.
Is there a way to pass grep's output to mv? Being a noob and an amateur, I'm a little leery of rm.

I'm using OS X (10.6.8) and bash.
# 2  
Old 05-24-2012
Code:
ls  ~/Library/Preferences/*.iTunes.plist.[0-9].* |
while read fname
do
  mv $fname ~/.Trash/
done

Start with that. You can use filename matching to get what you want, skipping grep entirely.
# 3  
Old 05-24-2012
Quote:
Originally Posted by jim mcnamara
Code:
ls  ~/Library/Preferences/*.iTunes.plist.[0-9].* |
while read fname
do
  mv $fname ~/.Trash/
done

Start with that. You can use filename matching to get what you want, skipping grep entirely.
Jim, forgive my ignorance, but I'm not sure what I'm looking at. I have no background with coding and I'm trying learn this stuff on my own.
This a shell script, not command line arguments, right?
# 4  
Old 05-24-2012
The glob ~/Library/Preferences/*.iTunes.plist.[0-9].* expands to match all the files you want. The shell does this by the way -- not ls.

It uses ls to list the files you want. Then it uses read in a while loop to read them one by one, and each one it reads, causes it to run mv filename ~/.Trash/. When it runs out of lines to read, read will return false, and the loop will break.

That looks pretty redundant, though. How about

Code:
mv ~/Library/Preferences/*.iTunes.plist.[0-9].* ~/.Trash/

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 05-24-2012
Yeah, that worked after I fixed up the search pattern a little. mv can't read a regex, (or the shell won't expand it), which is why I got grep involved, but as it happens I didn't need a regex. At least not for this group of files.

Code:
mv ~/Library/Preferences/*.iTunes.plist\.* ~/.Trash/

But it seems like, if you did need/desire to use a regex to locate files, there should be some way to pass that output to mv, no?
# 6  
Old 05-24-2012
A generic Bourne shell won't do regexes. A new enough BASH can, but doesn't expand them into filenames the way it can do with globs. This is because of a fundamental difference between globs and regexes -- a regex only has to match part of a filename, but a glob always has to match an entire filename.

You can expand on Jim's code to do what you want. You're piping filenames into a while-read loop. Nothing stops you from shoving a grep in the way first. This is what makes text streams generic.

Code:
ls  ~/Library/Preferences/ | grep "regex" |
while read fname
do
  echo mv "$fname" ~/.Trash/
done

Remove the 'echo' once you've tested it and are sure it does what you want.

This is not as efficient as using a glob, though, since it runs mv 100 times to move 100 files, instead of once.

Globs have limits though. If you had millions of files, a glob probably couldn't handle them all, and you'd have to use straight ls or find with pipes...

Last edited by Corona688; 05-24-2012 at 06:41 PM..
# 7  
Old 05-24-2012
noglob

I went to see what version of bash I had, and see that the noglob option is set. Could that be the problem?

noclobber off
noexec on
noglob on
nolog off
notify off
nounset off
onecmd off
physical off
pipefail off
posix off
privileged off
verbose off
vi off
xtrace off
bash-3.2$
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Can't pass a variable representing the output of lsb_release to a docker dontainer

I don't know why, but the rendering of my code mucks up the spacing and indentation, despite being correct in the original file. I'm having issues getting the following script to run (specifically the nested script at the end of the docker command near the end of the script; I think I'm not passing... (2 Replies)
Discussion started by: James Ray
2 Replies

2. Homework & Coursework Questions

How to Dynamically Pass Parameter to plsql Function & Capture its Output Value in a Shell Variable?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 2. Relevant commands, code, scripts, algorithms: #! /bin/ksh v="ORG_ID" ... (2 Replies)
Discussion started by: sujitdas2104
2 Replies

3. Shell Programming and Scripting

Pass ls command output to diff

Hi , Can some one help me how to pass ls command output to diff command ex : - ls *.xml will return files which have time stamps abc-<time-stamp>.xml xyz-<time-stamp>.xml diff abc-<time-stamp>.xml xyz-<time-stamp>.xml >> newfile.txt we need to... (9 Replies)
Discussion started by: techie_09
9 Replies

4. Shell Programming and Scripting

Need to pass Oracle SQL output to Linux and back... Help!

Hi all, Hopefully you can help. This is what I'm trying to achieve: Obtain a list of usernames out of an Oracle Database Based on this list, link each username with an Oracle Internet Directory (OID) GUID Using the username and GUID perform a database update for all users Here are the... (7 Replies)
Discussion started by: exm
7 Replies

5. Shell Programming and Scripting

cannot pass a echo output to a variable in bash

Hi, I have a problem with passing a echo output into a variable in bash file='1990.tar' NAME='echo $file | cut -d '.' -f1'; echo $NAME the result is echo $file | cut -d . -f1 however with this one,#!/bin/bash file='1990.tar' echo $file | cut -d '.' -f1 the result is what I... (2 Replies)
Discussion started by: 1988PF
2 Replies

6. Shell Programming and Scripting

Simple ways to pass variations to grep

can someone please show me a better way (if there is one), to grep out all variations (upper and small case letters) from a file? egrep "(panic|error|failed|fail|FAIL)" /var/log/messages I'm interested in finding any string that may indicate somethings wrong with ANY part of a system. so, if... (6 Replies)
Discussion started by: SkySmart
6 Replies

7. Shell Programming and Scripting

How to Pass the Output Values from the PL/SQL Procedure to Shell Script?

hi, Could anyone tell me how to pass the output values of the PL/SQL procedure to Shell script and how to store that values in a shell script variable... Thanks in advance... (5 Replies)
Discussion started by: funonnet
5 Replies

8. Shell Programming and Scripting

Pass input and output file as parameter to awk script

Hi, i am new to awk. I am using csv2pipe script(shown below) BEGIN { FS=SUBSEP; OFS="|" } { result = setcsv($0, ",") print } # setcsv(str, sep) - parse CSV (MS specification) input # str, the string to be parsed. (Most likely $0.) # sep, the separator between the values. # #... (6 Replies)
Discussion started by: bhaskarjha178
6 Replies

9. Shell Programming and Scripting

[csh] How to capture output from a command and pass it on to a variable?

Hi there! I'm trying to write a script that will capture output from a command and assign it to a variable. Let's say, for example, I'd like to catch from inside the script whatever the following command outputs: ls *.aaa and put it into a variable "listoffiles". What I tried was: set... (3 Replies)
Discussion started by: machinogodzilla
3 Replies

10. Shell Programming and Scripting

how to pass variable to grep?

Hi I have a such conditional: SPAMH="it is SPAM" if grep -q $SPAMH $NMDIR/$mail; then SPAMHFLAG=1 else SPAMHFLAG=0 fi And grep doesn't catch this string, even it exists there. I think it's a problem with passing $SPAMH to grep. I tried... (2 Replies)
Discussion started by: xist
2 Replies
Login or Register to Ask a Question