Nobody likes to be called a dummy by a dummy.

dumbass linux question

I select a bunch of files using

ls|grep blahblah

now I just want to move all those to some location

xargs? how do i do this?

cheers
Permalink $-- 
September 3rd, 2006 2:59pm
pipe it to mv?
Permalink Colm 
September 3rd, 2006 3:08pm
ls |grep blah| mv somewhere

mv: missing argument
Permalink $-- 
September 3rd, 2006 3:12pm
Grep just returns to the standard output (screen).

I use Perl for that sort of thing, but a BASH script probably does it as well, using the sed command.

Get a directory handle, loop through all files checking each one for xyz using a regexp like $variable ~= m/xyz/;
Permalink LinuxOrBust 
September 3rd, 2006 3:19pm
mv *.blah /new directory
Permalink Bad Religion 
September 3rd, 2006 3:19pm
Oh man, I'm checking out grep, it's got a lot of things.  You can have it output just the filename using -l, for example.  I think you can do what you say just on the command-line.
Permalink LinuxOrBust 
September 3rd, 2006 3:24pm
You can do anything on the commandline. The syntax is just too convoluted for anyone to remember.
Permalink Bad Religion 
September 3rd, 2006 3:35pm
LOB@ubuntu:~$ grep -l Add ./ *
mplcpnl.cfg
tracker.c
tracker.c~
LOB@ubuntu:~$ grep -l Add ./ *


There I greped the word 'Add' and got the filenames that contained them.  Just have to pipe the filenames to the mv command now.
Permalink LinuxOrBust 
September 3rd, 2006 3:37pm
God created the world with a commandline on a Sys V.
Permalink Bad Religion 
September 3rd, 2006 3:39pm
It's tricky. xargs can do it, but I always have to remind myself how to use it. I end up using Perl one-liners mostly. Whatever I do, I always do try runs with echo or print to check what commands are going to be executed before I do it for real.

More often than not, it's easier to:

a) just select all the files in the GUI and move them by hand,
b) paste the list of files into a text editor and create a script containing dozens of individual mv commands to do what you want.
Permalink Send private email bon vivant 
September 3rd, 2006 3:40pm
Wait, wait wait.
What are we talking about? The correct answer to the OP is:






21
Permalink Bad Religion 
September 3rd, 2006 3:41pm
Now I'm noticing that peculiar phenomenon that occurs after reading a textbook, the one where only their examples work.

It's seems commands like grep and wc can take an input, but commands like ls and mv and cp can't.

So, back to shell script.  Put filenames into variable names and have 'cp $variable' in a loop.
Permalink LinuxOrBust 
September 3rd, 2006 3:59pm
or mv in your case.
Permalink LinuxOrBust 
September 3rd, 2006 3:59pm
I'd still go with 21.
Permalink Bad Religion 
September 3rd, 2006 4:04pm
ls | grep blahblah | xargs mv {} dest

problem is that this calls mv for each file, which is slooooow.  better is:

mv `ls | grep blahblah` dest

which won't work in {t}csh for more than ~1024 files (because csh is shit).  use bash.

seriously.
Permalink  
September 3rd, 2006 4:41pm
That first example doesn't work because mv doesn't get both arguments, so I had to use this:

grep -l Add ./ * | xargs -i mv -f {} /home/LOB/test/

http://www.unixreview.com/documents/s=8274/sam0306g/


Where 'Add' was the word I was searching for.
Permalink LinuxOrBust 
September 3rd, 2006 5:23pm
Your second example works:

mv `grep -l Add ./ *` /home/LOB/test/
Permalink LinuxOrBust 
September 3rd, 2006 5:33pm
classic colm

http://www.crazyontap.com/topic.php?TopicId=10007#126072

doesn't know the answer, kind of knows some right sounding words in the general direction, willing to pass himself off as knowledgable.
Permalink  
September 3rd, 2006 5:41pm
haha.  Sounds like a labor to keep track of that.

The -f after mv is unnecessary, it's not even in the documentation so it must be a Unix thing.  The -i stands for replacing the (first) argument with a string.
Permalink LinuxOrBust 
September 3rd, 2006 5:44pm
Tool misuse :-) Use the find command.

http://www.onlamp.com/pub/a/bsd/2002/02/21/FreeBSD_Basics.html

Go

>man find

for kickers.
Permalink trollop 
September 3rd, 2006 6:25pm
Heh. find has so many options it's pretty hard to remember how to do things with it without having to resort to the documentation every time too.
Permalink Send private email bon vivant 
September 3rd, 2006 9:53pm
Agreed, that's true for anything beyond

>find / whattheblip.* -print
Permalink trollop 
September 3rd, 2006 10:01pm

This topic is archived. No further replies will be accepted.

Other topics: September, 2006 Other topics: September, 2006 Recent topics Recent topics