Nifty (and non-working) File Copy Using ls


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Nifty (and non-working) File Copy Using ls
# 1  
Old 10-02-2014
Nifty (and non-working) File Copy Using ls

Greetings Smilie

Got a basic one to muddle through tonight; and hoping for a smidgen of help 'long the way Smilie

Here 'tis:
Code:
ls ./dir1/ | grep -i .jpg | xargs cp -f ./dir1/$1 ../../dir2/$1

Simple enough; and to this amateur eye, it looks like things should work pretty well. But, of course, it tanks out...

What's the jot or tittle I might be missing this time?


Thanks a bunch!
# 2  
Old 10-02-2014
Maybe

Code:
ls ./dir1/*.jpg | xargs -I file cp -f file ../../dir2/

This User Gave Thanks to Aia For This Post:
# 3  
Old 10-02-2014
xargs is not a shell. It doesn't have a $1, $2, ... They just turn into whatever parameters the shell already had, if any (once and exactly once, before xargs is even run).

To be honest I'm not even sure what you're trying to do, what you expected $1 $2 ... to be.

xargs works like this:
Code:
# These commands are effectively equivalent
cat a b c d e
echo "a b c d e" | xargs cat


Last edited by Corona688; 10-02-2014 at 10:16 PM..
# 4  
Old 10-02-2014
I think this might be closer to what you want:

Code:
echo path/* | xargs cp -t /path/to/destfolder

This will expand to cp -t /path/to/destfolder file1 file2 file3 ...

The -t is necessary because, ordinarily, cp expects the folder to come last, not first, which xargs cannot do.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 10-02-2014
Quote:
Originally Posted by LinQ
Greetings Smilie

Got a basic one to muddle through tonight; and hoping for a smidgen of help 'long the way Smilie

Here 'tis:
Code:
ls ./dir1/ | grep -i .jpg | xargs cp -f ./dir1/$1 ../../dir2/$1

Simple enough; and to this amateur eye, it looks like things should work pretty well. But, of course, it tanks out...

What's the jot or tittle I might be missing this time?


Thanks a bunch!
The script you have shown us makes no sense to me at all. Instead of telling us it tanks out, it would be a lot easier to help you if you would tell us what you're trying to do.
# 6  
Old 10-03-2014
As pointed out, xargs doesn't support positional arguments using $1, etc.

What you can do is use -I <replace string> to explictly tell xargs where to put the arguments in the command. It's worth noting that in this case that arguments are separated by newline, not just any whitespace as normal.
# 7  
Old 10-03-2014
@all:

Thank you for the input. My apologies for any confusion, but I was trying to accomplish the same thing which most of us have done at one time or other with DOS's "COPY" commandline to copy select content of one directory over to another.

In the end, Aia's
Code:
ls ./dir1/*.jpg | xargs -I file cp -f file ../../dir2/

worked well; along with Corona688's
Code:
echo ./dir1/*.jpg | xargs cp -t ../../dir2/

Smilie

My tinkering found, likewise, that
Code:
find ./dir1/ -name '*' | grep -i .jpg | xargs -I file cp -f file ../../dir2/

and
Code:
find ./dir1/ -name '*' | grep -i .jpg | xargs cp -t ../../dir2/

also did the trick using "find".

So I guess there's more than one way to cook a fish; but I can't help but wish there was something syntactically as simple as the old DOS "COPY" command for those of us who have moved along to UNIX...


Thanks again, all; and have a great day Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Output encoding in copy with ant is not working

Not sure whether I can post 'ant' related question here. There is a requirement to convert files to UTF-8 format. So, I tried this simple ant script. <project name="try_copy" basedir="." default="copy_this"> <target name="copy_this"> <copy file="myfile.txt"... (2 Replies)
Discussion started by: anand_bh
2 Replies

2. Shell Programming and Scripting

Change the file name and copy old file content to new file names.

Hi, I have a files in a directory as below :- ls -1 mqdepth-S1STC02 proc-mq-S1STC01 proc-mq-S1STC02 proc-mq-S1STC03 Whereever i have S1STC i need to copy them into new file with file name S2STC. expected output :- ls -1 mqdepth-S2STC02 proc-mq-S2STC01 proc-mq-S2STC02... (3 Replies)
Discussion started by: satishmallidi
3 Replies

3. Shell Programming and Scripting

Working out days of the week and processing file in 3 working days

Hi guys i need advice on the approach to this one...... I have a file say called Thisfile.20130524.txt i need to work out from the date 20130524 what day of the week that was and then process the file in 3 working days. (so not counting saturday or sunday....(will not worry about bank... (2 Replies)
Discussion started by: twinion
2 Replies

4. Shell Programming and Scripting

how to copy the directory but not copy certain file

Hi experts cp bin root src /mnt but not copy bin/bigfile any help? ( I post this thread in the "redhat" forum wrongly, I don't know how to withdraw that question in that wrong forum) Thanks (6 Replies)
Discussion started by: yanglei_fage
6 Replies

5. UNIX for Dummies Questions & Answers

UUCP (Unix to unix copy) not working

I have a problem using uucp. I have Ubuntu 10.4 and i installed the 'uucp' package. In my LAN there are a desktop pc, a laptop, and the router the desktop local ip is: 192.168.0.2 the laptop local ip is: 192.168.0.4 Here are the /etc/uucp/config and the /etc/uucp/sys i used on the desktop.... (4 Replies)
Discussion started by: mghis
4 Replies

6. Shell Programming and Scripting

Bash copy file contents into an existing file at a specific location

Hi all I need to copy the entire contents of one file into an existing file at a specific location. I know the exact line number where I need to put it. It appears I would use either sed or awk to do this, but I have been unsuccessful so far: File A line 1 line 2 line 3 line 4 ... (6 Replies)
Discussion started by: gshepherd7
6 Replies

7. UNIX for Dummies Questions & Answers

Copy Command in Script Not Working

Hi, I have about enough UNIX knowledge to fill a thimble, but when a co-worker left I inherited an interface that runs Informatica on a UNIX box. I like UNIX, but my lack of skill is a real hindrance right now and I need to be able to archive the 9 csv files that are put in a directory by the FTP... (10 Replies)
Discussion started by: JeffR
10 Replies
Login or Register to Ask a Question