LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-16-2011, 04:58 PM   #1
andy.l
Member
 
Registered: Feb 2007
Location: Scandinavia
Distribution: Centos/Redhat
Posts: 97

Rep: Reputation: 15
Question piping output of list to cp


Hi
I have the following code
Code:
ls ~/ | grep ^[[:lower:]]
and would like to take the result of this output and use this as the basis for a cp command to move these files to another folder. How can I easily achieve this?

/Andy
 
Old 05-16-2011, 05:08 PM   #2
Chirel
Member
 
Registered: Nov 2009
Posts: 55

Rep: Reputation: 19
Even if it's ugly, and if you have no space in filenames you can do something like this

Code:
for i in $(ls ~/ | grep ^[[:lower:]]); do cp $i newdestdir; done
 
Old 05-16-2011, 05:35 PM   #3
andy.l
Member
 
Registered: Feb 2007
Location: Scandinavia
Distribution: Centos/Redhat
Posts: 97

Original Poster
Rep: Reputation: 15
But if there are spaces in filenames? Hos do i handle thore?

/Andy
 
Old 05-16-2011, 06:02 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
You should almost never need to use ls to get a list of files. Bash has excellent globbing ability, and can do almost everything you need. Particularly when you use extended globbing patterns.

Code:
cp [[:lower:]]* targetdirectory
Edit: Note also that word splitting isn't done on the results of a glob, at least as long as it's being passed directly to the program being executed (splitting happens before globbing), so spaces and other reserved characters shouldn't be a problem. This wouldn't be true if additional processing of the line is done after the globbing substitution is made, such as when using it inside a $() command substitution.

Last edited by David the H.; 05-16-2011 at 06:43 PM. Reason: changed link target
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Piping output fm cut into sed wtaicken Programming 7 12-09-2008 10:54 AM
piping output of find to vim babu198649 Linux - Newbie 4 07-24-2008 06:25 AM
Piping output between processes. C. banan Programming 1 11-12-2006 11:25 PM
Piping file list through rm swingheim Linux - Newbie 4 01-17-2004 05:48 PM
Piping output to a text file joadoor Linux - Newbie 7 04-19-2002 03:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration