useful shortcut - pipe results of search to cp
hey i wanted to share this because i've found it invaluable but would like take it 1 step further if someone could help.
after doing a search for files you can pipe the search results through other commands using what's called a subshell (a second running version of your shell) for example if you try to pipe a your search results through cp it will complain that it is mising a parameter: Code:
[root@itchy web]# locate filename.jpg) | cp $1 destination/ but if you put in this syntax it will work!: Code:
cp $(locate filename.jpg) destination/ i was trying to find a faster way copy the results of "ldd" so i wouldn't have to manually hunt down the files but using that same command results in this: Code:
[root@itchy web]#cp $(ldd /usr/bin/scp) test/ thanks, disorderly |
Here you go buddy:
Code:
cp $(ldd /usr/bin/scp | cut -d \( -f1- | cut -d \> -f2-) destination/ |
that rocks! thanks!
do you think you could explain to me how that works? |
Yeah no problem.
I take it you know what the ldd command does so we will skip that one. I took the output of the ldd command and piped it to cut. Cut allows for manipulation of text input to "cut" out what you do not want. The first cut parses the ldd output looking for the "(" character. But, you will see that there is a "\" preceding it. this "\" character is used to "escape out" the "(" character. In other words it tells BASH to treat the "(" character as just that a character and not tack on its special meaning (which is usually used to group commands together). Once parsed it then separates the text (internally) into two fields, one and two. The "-f1-" tells cut I only want field one and not two. This removes the (0xha89078) stuff at the end of the ldd output I then take that output and pipe it to another cut. This time parsing for the ">" character. Again because this character has special meaning to BASH I need to "escape" it out, and hence again it is preceded by the "\" character. Again cut separates this output into two fields. I use the "-f2-" to tell cut I want the second field, which is the absolute path to where those files are located and not just their names. This will remove the name of the library and the => symbols of the ldd output. This leaves you with just the nice little paths to the files you wish to copy. NOTE: reversing the two cut commands will not work. I can't really explain why, as it should do the same thing. But, alas it does not. Sorry for not explaining this in my original post, but I was on lunch break when I wrote that post and did not have time to explain. |
dude that is feckin GREAT. i can think of a dozen ways to use that 'cut' command :cool:
well to take this new piece of knowledge even further.... let take for example Code:
[bach@itchy ~]$ ldd /bin/ls Code:
[bach@itchy ~]$ cp $(ldd /bin/ls | cut -d \( -f1- | cut -d \> -f2-) temp thanks!! (i'm thinking this is how some of the code was used to 'create a chrooted jail' was written - it would search for the relavant commands in directories, then ldd's the commands, then copy them to the correct folders!) |
Oh sorry I see the problem. The line I gave you:
Code:
cp $(ldd /usr/bin/scp | cut -d \( -f1- | cut -d \> -f2-) destination/ Code:
cp $(ldd /usr/bin/scp | cut -d \( -f1 | cut -d \> -f2) destination/ With this new knowledge I tried reversing the two cut commands. Now if you remember: Quote:
I hope you have had as much fun with this as i have. If you have any other type of scripting questions post them here and I'll see wha tI can do. |
another way
I know this is an old thread, but I thought I would add my way of doing it.
Code:
cp $(find /path/to/source/files -iname filename.ext) /path/to/destination Code:
cp $(find ./ -iname *.pdf) ~/pdf_files |
All times are GMT -5. The time now is 05:28 AM. |