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:
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.
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?
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.
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.
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?
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.
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..
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$
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)