0

I hope to make scp work with pipe, like

firstlove@chenli ~/Downloads $ ls -liart | tail -n5 | grep 0001                                  
51384329 -rw-r--r--  1 firstlove firstlove       1440 Oct 31 13:02 0001-add-UNA_PRINT-for-unalign-access-info.patch
51384385 -rw-r--r--  1 firstlove firstlove       3028 Oct 31 13:02 0001-support-new-em_machine.patch
firstlove@chenli ~/Downloads $ scp `ls -liart | tail -n5 | grep 0001` wuxi-jump-128:/tmp         
[email protected]'s password: 
[email protected]'s password: 
ls -liart | tail -n5 | grep 0001: No such file or directory

But it promotes "No such file or directory", is there other workaround?

Chen Li
  • 347
  • 4
  • 14
  • There probably is, but could you explain in more detail what exactly you want to accomplish? If I interpret your command line correctly, you want to copy the last five files in your directory, but among them only those starting with "0001". I am not sure that this _really_ is what you want, and even then, you should **never** use the output of `ls -l` to generate a list of files to be copied. – AdminBee Oct 31 '19 at 07:51
  • @AdminBee Yes, that is what I want. why using `ls -l` is a bad idea? and is there any other ways? – Chen Li Oct 31 '19 at 08:19
  • Did you _actually_ use backticks in your command, or did you use single quotes around the pipeline? Also note that if the command substitution worked, `scp` would not know what to do with the long listing output of `ls`. Can you describe what you want to achieve with your pipeline? It looks as if you want to transfer the files that contain `0001` out of the five most recently modified files. – Kusalananda Oct 31 '19 at 09:21
  • The reason why the output of `ls -l` is a bad idea is that any command to which you pipe this result would take _all_ output tokens as single input parameters, so `scp` would first look for a file called `51384328`, then trip on `-rw-r--r--` which it might interpret as a parameter (see e.g. [here](https://unix.stackexchange.com/questions/548700/why-would-the-command-ls-generate-an-error-about-invalid-options)) etc. Leaving aside you will always be told "never to parse the output of ls", you must at least pipe the output through `awk` to select only the last column containing the filenames. – AdminBee Oct 31 '19 at 10:23

0 Answers0