pass output as an argument for cp in bash
It would be:
cp `ls -SF | grep -v / | head -5` Directory
assuming that the pipeline is correct. The backticks substitute in the line the output of the commands inside it.
You can also make your tests:
cp `echo a b c` Directory
will copy all a
, b
, and c
into Directory
.
How to pass output from sed into cp command?
Just use xargs
sed 's|<pattern>|<replace>|' file | xargs -i{} cp {} somewhere
How to pipe output from grep to cp?
grep -l -r "TWL" --exclude=*.csv* | xargs cp -t ~/data/lidar/tmp-ajp2/
Explanation:
- grep
-l
option to output file names only - xargs to convert file list from the standard input to command line arguments
- cp
-t
option to specify target directory (and avoid using placeholders)
How to pass command output as multiple arguments to another command
You can use xargs
:
grep 'pattern' input | xargs -I% cp "%" "%.bac"
How To Pass Find results to CP such that File Names with Spaces work
Try this:
find . -name \*.pdf -print0 | xargs -0 -n 1 -Ifoo cp --parents foo /new_path/
Or
find . -name \*.pdf -exec cp --parents {} /new_path/ \;
bash: how to implement the output of 'wc -l' as an argument to another script
when passing file as standard input wc doesn't echo filename
wc -l < NRL.txt
Passing more than one argument through to a command in a shell wrapper
#!/bin/sh
cp -rf -- "$@" /support/save
Use "$@"
to expand to your entire argument list. It is essential that this be placed in double-quotes, or else it will behave identically to $*
(which is to say, incorrectly).
The --
is a widely implemented extension which ensures that all following arguments are treated as literal arguments rather than parsed as options, thus making filenames starting with -
safe.
To demonstrate the difference, name the following script quotdemo
.
#!/bin/sh
printf '$@: '; printf '<%s>\n' "$@"
printf '$*: '; printf '[%s]\n' $*
...and try running:
touch foo.txt bar.txt "file with spaces.txt" # create some matching files
quotdemo *.txt # ...then test this...
quotdome "*.txt" # ...and this too!
Is it possible to pipe the results of FIND to a COPY command CP?
Good question!
- why cant you just use | pipe? isn't that what its for?
You can pipe, of course, xargs
is done for these cases:
find . -iname "*.SomeExt" | xargs cp Destination_Directory/
- Why does everyone recommend the -exec
The -exec
is good because it provides more control of exactly what you are executing. Whenever you pipe there may be problems with corner cases: file names containing spaces or new lines, etc.
- how do I know when to use that (exec) over pipe | ?
It is really up to you and there can be many cases. I would use -exec
whenever the action to perform is simple. I am not a very good friend of xargs
, I tend to prefer an approach in which the find
output is provided to a while
loop, such as:
while IFS= read -r result
do
# do things with "$result"
done < <(find ...)
Related Topics
Where Is the Stack Memory Allocated from for a Linux Process
Why Does Find -Exec Mv {} ./Target/ + Not Work
How to Check If There Are Symbolic Links Pointing to a Directory
Differencebetween Ld_Library_Path and -L at Link Time
How to Use Make and Compile as C99
Why How to Cast Sockaddr to Sockaddr_In
Cross Compiling for Mips Router from X86
Assigning Dynamic Bash Variable Names Using a for Loop Seq
Save and Restore Terminal Content
Remove Redundant Paths from $Path Variable
Where Are All My Inodes Being Used
What Is a Way to Read Man Pages in Vim Without Using Temporary Files
Hard Time in Understanding Module_Device_Table(Usb, Id_Table) Usage