LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   piping output of list to cp (https://www.linuxquestions.org/questions/linux-general-1/piping-output-of-list-to-cp-881068/)

andy.l 05-16-2011 04:58 PM

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

Chirel 05-16-2011 05:08 PM

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


andy.l 05-16-2011 05:35 PM

But if there are spaces in filenames? Hos do i handle thore?

/Andy

David the H. 05-16-2011 06:02 PM

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.


All times are GMT -5. The time now is 05:59 AM.