LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 05:58 PM   #1
andy.l
Member
 
Registered: Feb 2007
Location: Scandinavia
Distribution: Centos/Redhat
Posts: 96

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, 06: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, 06:35 PM   #3
andy.l
Member
 
Registered: Feb 2007
Location: Scandinavia
Distribution: Centos/Redhat
Posts: 96

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

/Andy
 
Old 05-16-2011, 07:02 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
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 07:43 PM. Reason: changed link target
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 11:54 AM
piping output of find to vim babu198649 Linux - Newbie 4 07-24-2008 07:25 AM
Piping output between processes. C. banan Programming 1 11-13-2006 12:25 AM
Piping file list through rm swingheim Linux - Newbie 4 01-17-2004 06:48 PM
Piping output to a text file joadoor Linux - Newbie 7 04-19-2002 04:50 AM

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

All times are GMT -5. The time now is 02:20 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration