[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

Uses of % in bash

Hello, I am trying to interpret one command and I am not able to get the get what does % symbolizes here..

find /path/to/diretory -type f -print0 | xargs -0 -i% cp -v % /path/to/new/directory

Open in new window

Please let me know how cp is taking % as the argument supplied via find using pipe. I know it works but how it works I am failed to get it. Does bash shell playing any role here or it is magic of xargs command? Thanks!
2 Solutions
The -i argument to xargs is the "replace string". Instances of this string in the initial argument are replaced by input From std-in.

So cp % ... Is replaced by each file from the find command.

Not how I would have done it. I'd have used -exec argument to find.
By default, the -i flag to xargs uses {} as a placeholder for arguments.  It is the equivalent of -I{}

So you can specify any character you like as the placeholder, eg: %

The find statement is better written as:
find /path/to/diretory -type f  | xargs -i cp -v /path/to/new/directory

Open in new window


Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now