(Bash) `cp`, `mv` with wildcard
- Use
find -name <NAME_TO_CAPTURE>to capture filenames to copy or move, then dispatch those files tocpormvusing-exec- E.g.
find /home/admin -name "*.txt" -exec mv '{}' /home/admin/txtfiles/ \; - Wildcard string (e.g.
"*.txt") must be enclosed by quotes {}denotes each of the captured filenames\;at the end is semi-colon escaped to denote end of command for-exec
- E.g.
- Another approach using
xargs- E.g.
ls -1 *.txt | xargs -I{} mv /home/admin/{} /home/admin/txtfiles/{} ls -1printslsoutput one-file-per-line-I{}makes{}the placeholder for standard input (implies-x -L 1where-L 1makesxargsconsider at most 1 line per command)
- E.g.
References:
Written on March 25, 2021
