Equivalent to GNU find -printf flag in other find implementations
Specifically, in mkinitcpio the command find -mindepth 1 -printf '%P'
is used, what would be a way to recreate a command with identical output without the -printf flag. https://git.archlinux.org/mkinitcpio.git/tree/mkinitcpio This is the full script in case it is useful.
find posix
add a comment |
Specifically, in mkinitcpio the command find -mindepth 1 -printf '%P'
is used, what would be a way to recreate a command with identical output without the -printf flag. https://git.archlinux.org/mkinitcpio.git/tree/mkinitcpio This is the full script in case it is useful.
find posix
Do any of your file or directory names contain the newline character?
– meuh
Sep 18 at 10:33
@meuh I don't know what files or directories it's finding, it's using it to find all the files to put into the initramfs. I don't see a reason the files or directories would have a newline character though.
– E5ten
Sep 18 at 13:18
add a comment |
Specifically, in mkinitcpio the command find -mindepth 1 -printf '%P'
is used, what would be a way to recreate a command with identical output without the -printf flag. https://git.archlinux.org/mkinitcpio.git/tree/mkinitcpio This is the full script in case it is useful.
find posix
Specifically, in mkinitcpio the command find -mindepth 1 -printf '%P'
is used, what would be a way to recreate a command with identical output without the -printf flag. https://git.archlinux.org/mkinitcpio.git/tree/mkinitcpio This is the full script in case it is useful.
find posix
find posix
edited Sep 18 at 3:12
jasonwryan
49.1k14134184
49.1k14134184
asked Sep 18 at 2:01
E5ten
31
31
Do any of your file or directory names contain the newline character?
– meuh
Sep 18 at 10:33
@meuh I don't know what files or directories it's finding, it's using it to find all the files to put into the initramfs. I don't see a reason the files or directories would have a newline character though.
– E5ten
Sep 18 at 13:18
add a comment |
Do any of your file or directory names contain the newline character?
– meuh
Sep 18 at 10:33
@meuh I don't know what files or directories it's finding, it's using it to find all the files to put into the initramfs. I don't see a reason the files or directories would have a newline character though.
– E5ten
Sep 18 at 13:18
Do any of your file or directory names contain the newline character?
– meuh
Sep 18 at 10:33
Do any of your file or directory names contain the newline character?
– meuh
Sep 18 at 10:33
@meuh I don't know what files or directories it's finding, it's using it to find all the files to put into the initramfs. I don't see a reason the files or directories would have a newline character though.
– E5ten
Sep 18 at 13:18
@meuh I don't know what files or directories it's finding, it's using it to find all the files to put into the initramfs. I don't see a reason the files or directories would have a newline character though.
– E5ten
Sep 18 at 13:18
add a comment |
2 Answers
2
active
oldest
votes
%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'
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" "${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" "${pathname#$topdir/}"
done' sh "$topdir" {} +
add a comment |
In -printf '%P'
the %P
is just removing the initial ./
from the front of the filename. You can do the equivalent with sed 's|^./||'
. The part produces a null character instead of newline between each filename. You can convert newline to null with
tr 'n' ''
. So you can try
find . -mindepth 1 -print | sed 's|^./||' | tr 'n' ''
If any names contain a newline, this will corrupt them, translating that newline to null.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f469694%2fequivalent-to-gnu-find-printf-flag-in-other-find-implementations%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
%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'
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" "${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" "${pathname#$topdir/}"
done' sh "$topdir" {} +
add a comment |
%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'
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" "${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" "${pathname#$topdir/}"
done' sh "$topdir" {} +
add a comment |
%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'
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" "${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" "${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 as -printf '%P'
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" "${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" "${pathname#$topdir/}"
done' sh "$topdir" {} +
edited Dec 15 at 22:48
answered Sep 18 at 13:52
Kusalananda
121k16229372
121k16229372
add a comment |
add a comment |
In -printf '%P'
the %P
is just removing the initial ./
from the front of the filename. You can do the equivalent with sed 's|^./||'
. The part produces a null character instead of newline between each filename. You can convert newline to null with
tr 'n' ''
. So you can try
find . -mindepth 1 -print | sed 's|^./||' | tr 'n' ''
If any names contain a newline, this will corrupt them, translating that newline to null.
add a comment |
In -printf '%P'
the %P
is just removing the initial ./
from the front of the filename. You can do the equivalent with sed 's|^./||'
. The part produces a null character instead of newline between each filename. You can convert newline to null with
tr 'n' ''
. So you can try
find . -mindepth 1 -print | sed 's|^./||' | tr 'n' ''
If any names contain a newline, this will corrupt them, translating that newline to null.
add a comment |
In -printf '%P'
the %P
is just removing the initial ./
from the front of the filename. You can do the equivalent with sed 's|^./||'
. The part produces a null character instead of newline between each filename. You can convert newline to null with
tr 'n' ''
. So you can try
find . -mindepth 1 -print | sed 's|^./||' | tr 'n' ''
If any names contain a newline, this will corrupt them, translating that newline to null.
In -printf '%P'
the %P
is just removing the initial ./
from the front of the filename. You can do the equivalent with sed 's|^./||'
. The part produces a null character instead of newline between each filename. You can convert newline to null with
tr 'n' ''
. So you can try
find . -mindepth 1 -print | sed 's|^./||' | tr 'n' ''
If any names contain a newline, this will corrupt them, translating that newline to null.
answered Sep 18 at 13:33
meuh
31.4k11854
31.4k11854
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f469694%2fequivalent-to-gnu-find-printf-flag-in-other-find-implementations%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Do any of your file or directory names contain the newline character?
– meuh
Sep 18 at 10:33
@meuh I don't know what files or directories it's finding, it's using it to find all the files to put into the initramfs. I don't see a reason the files or directories would have a newline character though.
– E5ten
Sep 18 at 13:18