Skip to main content
added 301 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

%P will give the relative path of the file starting from the directory used as starting point, so that if find is run with some/path as starting point and it finds the pathname some/path/to/file, then %P will expand to to/file.

When GNU find is not given a starting point (as in the command given in the question), it will use the current directory (.) as the starting point. The %P format will therefore remove ./ from the found paths in this case.

To do the same thing inas -printf '%P\0' with a non-GNU find implementations, assuming -mindepth is still available (as in find on BSD systems):

find . -mindepth 1 -exec sh -c '
    for pathname do
        printf "%s\0" "${pathname#./}"
    done' sh {} +

The embedded sh -c script will get a batch of pathnames from find to work on and uses a standard parameter expansion that removes the initial ./ from the pathname before printing it with a terminating nul-character.


The same thing, but with a variable holding the single top-level directory path:

topdir=/some/path

find "$topdir" -mindepth 1 -exec sh -c '
    topdir=${1%/}; shift
    for pathname do
        printf "%s\0" "${pathname#$topdir/}"
    done' sh "$topdir" {} +

%P will give the relative path of the file starting from the directory used as starting point, so that if find is run with some/path as starting point and it finds the pathname some/path/to/file, then %P will expand to to/file.

When GNU find is not given a starting point (as in the command given in the question), it will use the current directory (.) as the starting point. The %P format will therefore remove ./ from the found paths in this case.

To do the same thing in non-GNU find implementations, assuming -mindepth is still available (as in find on BSD systems):

find . -mindepth 1 -exec sh -c '
    for pathname do
        printf "%s\0" "${pathname#./}"
    done' {} +

The embedded sh -c script will get a batch of pathnames from find to work on and uses a standard parameter expansion that removes the initial ./ from the pathname before printing it with a terminating nul-character.

%P will give the relative path of the file starting from the directory used as starting point, so that if find is run with some/path as starting point and it finds the pathname some/path/to/file, then %P will expand to to/file.

When GNU find is not given a starting point (as in the command given in the question), it will use the current directory (.) as the starting point. The %P format will therefore remove ./ from the found paths in this case.

To do the same thing as -printf '%P\0' with a non-GNU find implementations, assuming -mindepth is still available (as in find on BSD systems):

find . -mindepth 1 -exec sh -c '
    for pathname do
        printf "%s\0" "${pathname#./}"
    done' sh {} +

The embedded sh -c script will get a batch of pathnames from find to work on and uses a standard parameter expansion that removes the initial ./ from the pathname before printing it with a terminating nul-character.


The same thing, but with a variable holding the single top-level directory path:

topdir=/some/path

find "$topdir" -mindepth 1 -exec sh -c '
    topdir=${1%/}; shift
    for pathname do
        printf "%s\0" "${pathname#$topdir/}"
    done' sh "$topdir" {} +
deleted 1 character in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

%P will give the relative path of the file starting from the directory used as starting point, so that if find is run with some/path as starting point and it finds the pathname some/path/to/file, then %P will expand to to/file.

When GNU find is not given a starting point (as in the command given in the question), it will use the current directory (.) as the starting point. The %P format will therefore remove ./ from the found paths in this case.

To do the same thing in non-GNU find implementations, assuming -mindepth is still available (as in find on BSD systems):

find . -mindepth 1 -exec sh -c '
    for pathname do
        printf "%s\\0""%s\0" "${pathname#./}"
    done' {} +

The embedded sh -c script will get a batch of pathnames from find to work on and uses a standard parameter expansion that removes the initial ./ from the pathname before printing it with a terminating nul-character.

%P will give the relative path of the file starting from the directory used as starting point, so that if find is run with some/path as starting point and it finds the pathname some/path/to/file, then %P will expand to to/file.

When GNU find is not given a starting point (as in the command given in the question), it will use the current directory (.) as the starting point. The %P format will therefore remove ./ from the found paths in this case.

To do the same thing in non-GNU find implementations, assuming -mindepth is still available (as in find on BSD systems):

find . -mindepth 1 -exec sh -c '
    for pathname do
        printf "%s\\0" "${pathname#./}"
    done' {} +

The embedded sh -c script will get a batch of pathnames from find to work on and uses a standard parameter expansion that removes the initial ./ from the pathname before printing it with a terminating nul-character.

%P will give the relative path of the file starting from the directory used as starting point, so that if find is run with some/path as starting point and it finds the pathname some/path/to/file, then %P will expand to to/file.

When GNU find is not given a starting point (as in the command given in the question), it will use the current directory (.) as the starting point. The %P format will therefore remove ./ from the found paths in this case.

To do the same thing in non-GNU find implementations, assuming -mindepth is still available (as in find on BSD systems):

find . -mindepth 1 -exec sh -c '
    for pathname do
        printf "%s\0" "${pathname#./}"
    done' {} +

The embedded sh -c script will get a batch of pathnames from find to work on and uses a standard parameter expansion that removes the initial ./ from the pathname before printing it with a terminating nul-character.

Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

%P will give the relative path of the file starting from the directory used as starting point, so that if find is run with some/path as starting point and it finds the pathname some/path/to/file, then %P will expand to to/file.

When GNU find is not given a starting point (as in the command given in the question), it will use the current directory (.) as the starting point. The %P format will therefore remove ./ from the found paths in this case.

To do the same thing in non-GNU find implementations, assuming -mindepth is still available (as in find on BSD systems):

find . -mindepth 1 -exec sh -c '
    for pathname do
        printf "%s\\0" "${pathname#./}"
    done' {} +

The embedded sh -c script will get a batch of pathnames from find to work on and uses a standard parameter expansion that removes the initial ./ from the pathname before printing it with a terminating nul-character.