Pipe each batch of xargs trough wc -l2019 Community Moderator Electionfind arcana: can't get pipe to work in -exec lineIs there any way to use xargs across a pipe?'Tar' the result of a 'find', preserving the directory structureSearch a String in a directory- Get output without filenamePiping commands after a piped xargsSending a list (text file) of files and pathnames to xargsxargs and line-by-line pipeCan a pipe be used instead of exec in - find / -name “.txt” -exec cp /junk ;Skip first line from output of each iteration of XARGSSave in a file first 7 file in the directory /bin that starts with c
What's the name of the logical fallacy where a debater extends a statement far beyond the original statement to make it true?
Is it allowed to activate the ability of multiple planeswalkers in a single turn?
I found an audio circuit and I built it just fine, but I find it a bit too quiet. How do I amplify the output so that it is a bit louder?
How much of a Devil Fruit must be consumed to gain the power?
Were Persian-Median kings illiterate?
Multiplicative persistence
When were female captains banned from Starfleet?
What fields between the rationals and the reals allow a good notion of 2D distance?
How to explain what's wrong with this application of the chain rule?
Will number of steps recorded on FitBit/any fitness tracker add up distance in PokemonGo?
Mimic lecturing on blackboard, facing audience
Biological Blimps: Propulsion
Fantasy comedy romance novel, set in medieval times, involving a siege, a nun, and enchanted pickles
Why does this expression simplify as such?
What is the English pronunciation of "pain au chocolat"?
Does an advisor owe his/her student anything? Will an advisor keep a PhD student only out of pity?
How to make money from a browser who sees 5 seconds into the future of any web page?
Can I say "fingers" when referring to toes?
US tourist/student visa
Doesn't the system of the Supreme Court oppose justice?
How do I fix the group tension caused by my character stealing and possibly killing without provocation?
How can I write humor as character trait?
Make a Bowl of Alphabet Soup
Giving feedback to someone without sounding prejudiced
Pipe each batch of xargs trough wc -l
2019 Community Moderator Electionfind arcana: can't get pipe to work in -exec lineIs there any way to use xargs across a pipe?'Tar' the result of a 'find', preserving the directory structureSearch a String in a directory- Get output without filenamePiping commands after a piped xargsSending a list (text file) of files and pathnames to xargsxargs and line-by-line pipeCan a pipe be used instead of exec in - find / -name “.txt” -exec cp /junk ;Skip first line from output of each iteration of XARGSSave in a file first 7 file in the directory /bin that starts with c
So my task is to find the file with the most hardlinks in a directory.
So far i have :
find . -name "file*" | xargs -I -n 1 find . -samefile
which gives me:
./hardlinkFIle245
./hardlinkFIle23
./hardlinkFIle2
./file2.txt
./hardlinkFIle1234
./hardlinkFIle123
./hardlinkFIle12
./hardlinkFIle1
./file1.txt
Now when I pipe it in with |wc -l
, I get the total number of lines 9:
find . -name "file*" | xargs -I -n 1 find . -samefile | wc -l
What I want is for each xargs batch -n 1 to give me the count :
so i want:
4
5
linux find pipe xargs
New contributor
|
show 1 more comment
So my task is to find the file with the most hardlinks in a directory.
So far i have :
find . -name "file*" | xargs -I -n 1 find . -samefile
which gives me:
./hardlinkFIle245
./hardlinkFIle23
./hardlinkFIle2
./file2.txt
./hardlinkFIle1234
./hardlinkFIle123
./hardlinkFIle12
./hardlinkFIle1
./file1.txt
Now when I pipe it in with |wc -l
, I get the total number of lines 9:
find . -name "file*" | xargs -I -n 1 find . -samefile | wc -l
What I want is for each xargs batch -n 1 to give me the count :
so i want:
4
5
linux find pipe xargs
New contributor
Doesn't yourfind
command's-printf
have a%n
specifier for the number of hardlinks? You could simplysort
andtail
that
– steeldriver
yesterday
Thanks man that is the right answer, but the sort is not working as expected after print f: find . -printf '%n' | sort -n How to to sort the answer and also print the file name and sort them by num,ber of hardlinks
– Goking
16 hours ago
Something likefind . -printf '%nt%fn' | sort -n | tail -n 1
should work, I think? if there is more than one file with the same number of links, it will return the one whose name is "numerically first"
– steeldriver
15 hours ago
The way they are sorted are in order 5 5 5 5 5 5 4 4 4 4 and so on and not: just 5 and 4 so tail -n 3 gives me basically 3 5's, since they all point to the same file. But what i want is the unique files ordered by number by hardlinks sorted so tail - n 2 would give me 5 4 not 5 5.
– Goking
14 hours ago
Just use uniq command: find . -printf '%nn' | uniq | sort -n -r
– Goking
14 hours ago
|
show 1 more comment
So my task is to find the file with the most hardlinks in a directory.
So far i have :
find . -name "file*" | xargs -I -n 1 find . -samefile
which gives me:
./hardlinkFIle245
./hardlinkFIle23
./hardlinkFIle2
./file2.txt
./hardlinkFIle1234
./hardlinkFIle123
./hardlinkFIle12
./hardlinkFIle1
./file1.txt
Now when I pipe it in with |wc -l
, I get the total number of lines 9:
find . -name "file*" | xargs -I -n 1 find . -samefile | wc -l
What I want is for each xargs batch -n 1 to give me the count :
so i want:
4
5
linux find pipe xargs
New contributor
So my task is to find the file with the most hardlinks in a directory.
So far i have :
find . -name "file*" | xargs -I -n 1 find . -samefile
which gives me:
./hardlinkFIle245
./hardlinkFIle23
./hardlinkFIle2
./file2.txt
./hardlinkFIle1234
./hardlinkFIle123
./hardlinkFIle12
./hardlinkFIle1
./file1.txt
Now when I pipe it in with |wc -l
, I get the total number of lines 9:
find . -name "file*" | xargs -I -n 1 find . -samefile | wc -l
What I want is for each xargs batch -n 1 to give me the count :
so i want:
4
5
linux find pipe xargs
linux find pipe xargs
New contributor
New contributor
edited 10 hours ago
Stéphane Chazelas
311k57586945
311k57586945
New contributor
asked yesterday
GokingGoking
1
1
New contributor
New contributor
Doesn't yourfind
command's-printf
have a%n
specifier for the number of hardlinks? You could simplysort
andtail
that
– steeldriver
yesterday
Thanks man that is the right answer, but the sort is not working as expected after print f: find . -printf '%n' | sort -n How to to sort the answer and also print the file name and sort them by num,ber of hardlinks
– Goking
16 hours ago
Something likefind . -printf '%nt%fn' | sort -n | tail -n 1
should work, I think? if there is more than one file with the same number of links, it will return the one whose name is "numerically first"
– steeldriver
15 hours ago
The way they are sorted are in order 5 5 5 5 5 5 4 4 4 4 and so on and not: just 5 and 4 so tail -n 3 gives me basically 3 5's, since they all point to the same file. But what i want is the unique files ordered by number by hardlinks sorted so tail - n 2 would give me 5 4 not 5 5.
– Goking
14 hours ago
Just use uniq command: find . -printf '%nn' | uniq | sort -n -r
– Goking
14 hours ago
|
show 1 more comment
Doesn't yourfind
command's-printf
have a%n
specifier for the number of hardlinks? You could simplysort
andtail
that
– steeldriver
yesterday
Thanks man that is the right answer, but the sort is not working as expected after print f: find . -printf '%n' | sort -n How to to sort the answer and also print the file name and sort them by num,ber of hardlinks
– Goking
16 hours ago
Something likefind . -printf '%nt%fn' | sort -n | tail -n 1
should work, I think? if there is more than one file with the same number of links, it will return the one whose name is "numerically first"
– steeldriver
15 hours ago
The way they are sorted are in order 5 5 5 5 5 5 4 4 4 4 and so on and not: just 5 and 4 so tail -n 3 gives me basically 3 5's, since they all point to the same file. But what i want is the unique files ordered by number by hardlinks sorted so tail - n 2 would give me 5 4 not 5 5.
– Goking
14 hours ago
Just use uniq command: find . -printf '%nn' | uniq | sort -n -r
– Goking
14 hours ago
Doesn't your
find
command's -printf
have a %n
specifier for the number of hardlinks? You could simply sort
and tail
that– steeldriver
yesterday
Doesn't your
find
command's -printf
have a %n
specifier for the number of hardlinks? You could simply sort
and tail
that– steeldriver
yesterday
Thanks man that is the right answer, but the sort is not working as expected after print f: find . -printf '%n' | sort -n How to to sort the answer and also print the file name and sort them by num,ber of hardlinks
– Goking
16 hours ago
Thanks man that is the right answer, but the sort is not working as expected after print f: find . -printf '%n' | sort -n How to to sort the answer and also print the file name and sort them by num,ber of hardlinks
– Goking
16 hours ago
Something like
find . -printf '%nt%fn' | sort -n | tail -n 1
should work, I think? if there is more than one file with the same number of links, it will return the one whose name is "numerically first"– steeldriver
15 hours ago
Something like
find . -printf '%nt%fn' | sort -n | tail -n 1
should work, I think? if there is more than one file with the same number of links, it will return the one whose name is "numerically first"– steeldriver
15 hours ago
The way they are sorted are in order 5 5 5 5 5 5 4 4 4 4 and so on and not: just 5 and 4 so tail -n 3 gives me basically 3 5's, since they all point to the same file. But what i want is the unique files ordered by number by hardlinks sorted so tail - n 2 would give me 5 4 not 5 5.
– Goking
14 hours ago
The way they are sorted are in order 5 5 5 5 5 5 4 4 4 4 and so on and not: just 5 and 4 so tail -n 3 gives me basically 3 5's, since they all point to the same file. But what i want is the unique files ordered by number by hardlinks sorted so tail - n 2 would give me 5 4 not 5 5.
– Goking
14 hours ago
Just use uniq command: find . -printf '%nn' | uniq | sort -n -r
– Goking
14 hours ago
Just use uniq command: find . -printf '%nn' | uniq | sort -n -r
– Goking
14 hours ago
|
show 1 more comment
2 Answers
2
active
oldest
votes
If using GNU find
, you can have it report the number of links with -printf %n
. So you can get the maximum value with:
find . -name 'file*' -printf '%nn' | sort -rn | head -n1
Note however that that number may include links that are not found under .
or for entries that don't match the file*
pattern.
If you only want to count hardlinks named file*
found under .
, and see the paths for those, you could do:
find . -name 'file*' -printf '%i%p' | gawk -v RS='' '
inode = $0
getline file
++count[inode] >= max
files[inode] = files[inode] " - " file ORS
max = count[inode]
max_inode = inode
END
printf "%s", "File with most links ("max"):n" files[max_inode]
'
Which would still run just one find
invocation instead of one per file.
add a comment |
You can just spawn a new shell on each xargs:
find . -name "file*" | xargs -n 1 sh -c 'echo "$1"; find . -samefile "$1" | wc -l' xargs-sh
Though using xargs
is a bad idea here as it would break if file paths contain whitespace of quoting characters.
Here, using wc -l
is also brittle as it breaks if file paths contain newline characters.
You could use the standard find -exec cmd +
syntax and save having to run one sh
per file by using a loop:
find . -name "file*" -exec sh -c '
for file do
printf "%sn" "$file"
find .//. -samefile "$file" | grep -c //
done' find-sh +
New contributor
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
);
);
Goking is a new contributor. Be nice, and check out our Code of Conduct.
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%2f507553%2fpipe-each-batch-of-xargs-trough-wc-l%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
If using GNU find
, you can have it report the number of links with -printf %n
. So you can get the maximum value with:
find . -name 'file*' -printf '%nn' | sort -rn | head -n1
Note however that that number may include links that are not found under .
or for entries that don't match the file*
pattern.
If you only want to count hardlinks named file*
found under .
, and see the paths for those, you could do:
find . -name 'file*' -printf '%i%p' | gawk -v RS='' '
inode = $0
getline file
++count[inode] >= max
files[inode] = files[inode] " - " file ORS
max = count[inode]
max_inode = inode
END
printf "%s", "File with most links ("max"):n" files[max_inode]
'
Which would still run just one find
invocation instead of one per file.
add a comment |
If using GNU find
, you can have it report the number of links with -printf %n
. So you can get the maximum value with:
find . -name 'file*' -printf '%nn' | sort -rn | head -n1
Note however that that number may include links that are not found under .
or for entries that don't match the file*
pattern.
If you only want to count hardlinks named file*
found under .
, and see the paths for those, you could do:
find . -name 'file*' -printf '%i%p' | gawk -v RS='' '
inode = $0
getline file
++count[inode] >= max
files[inode] = files[inode] " - " file ORS
max = count[inode]
max_inode = inode
END
printf "%s", "File with most links ("max"):n" files[max_inode]
'
Which would still run just one find
invocation instead of one per file.
add a comment |
If using GNU find
, you can have it report the number of links with -printf %n
. So you can get the maximum value with:
find . -name 'file*' -printf '%nn' | sort -rn | head -n1
Note however that that number may include links that are not found under .
or for entries that don't match the file*
pattern.
If you only want to count hardlinks named file*
found under .
, and see the paths for those, you could do:
find . -name 'file*' -printf '%i%p' | gawk -v RS='' '
inode = $0
getline file
++count[inode] >= max
files[inode] = files[inode] " - " file ORS
max = count[inode]
max_inode = inode
END
printf "%s", "File with most links ("max"):n" files[max_inode]
'
Which would still run just one find
invocation instead of one per file.
If using GNU find
, you can have it report the number of links with -printf %n
. So you can get the maximum value with:
find . -name 'file*' -printf '%nn' | sort -rn | head -n1
Note however that that number may include links that are not found under .
or for entries that don't match the file*
pattern.
If you only want to count hardlinks named file*
found under .
, and see the paths for those, you could do:
find . -name 'file*' -printf '%i%p' | gawk -v RS='' '
inode = $0
getline file
++count[inode] >= max
files[inode] = files[inode] " - " file ORS
max = count[inode]
max_inode = inode
END
printf "%s", "File with most links ("max"):n" files[max_inode]
'
Which would still run just one find
invocation instead of one per file.
edited 11 hours ago
answered 11 hours ago
Stéphane ChazelasStéphane Chazelas
311k57586945
311k57586945
add a comment |
add a comment |
You can just spawn a new shell on each xargs:
find . -name "file*" | xargs -n 1 sh -c 'echo "$1"; find . -samefile "$1" | wc -l' xargs-sh
Though using xargs
is a bad idea here as it would break if file paths contain whitespace of quoting characters.
Here, using wc -l
is also brittle as it breaks if file paths contain newline characters.
You could use the standard find -exec cmd +
syntax and save having to run one sh
per file by using a loop:
find . -name "file*" -exec sh -c '
for file do
printf "%sn" "$file"
find .//. -samefile "$file" | grep -c //
done' find-sh +
New contributor
add a comment |
You can just spawn a new shell on each xargs:
find . -name "file*" | xargs -n 1 sh -c 'echo "$1"; find . -samefile "$1" | wc -l' xargs-sh
Though using xargs
is a bad idea here as it would break if file paths contain whitespace of quoting characters.
Here, using wc -l
is also brittle as it breaks if file paths contain newline characters.
You could use the standard find -exec cmd +
syntax and save having to run one sh
per file by using a loop:
find . -name "file*" -exec sh -c '
for file do
printf "%sn" "$file"
find .//. -samefile "$file" | grep -c //
done' find-sh +
New contributor
add a comment |
You can just spawn a new shell on each xargs:
find . -name "file*" | xargs -n 1 sh -c 'echo "$1"; find . -samefile "$1" | wc -l' xargs-sh
Though using xargs
is a bad idea here as it would break if file paths contain whitespace of quoting characters.
Here, using wc -l
is also brittle as it breaks if file paths contain newline characters.
You could use the standard find -exec cmd +
syntax and save having to run one sh
per file by using a loop:
find . -name "file*" -exec sh -c '
for file do
printf "%sn" "$file"
find .//. -samefile "$file" | grep -c //
done' find-sh +
New contributor
You can just spawn a new shell on each xargs:
find . -name "file*" | xargs -n 1 sh -c 'echo "$1"; find . -samefile "$1" | wc -l' xargs-sh
Though using xargs
is a bad idea here as it would break if file paths contain whitespace of quoting characters.
Here, using wc -l
is also brittle as it breaks if file paths contain newline characters.
You could use the standard find -exec cmd +
syntax and save having to run one sh
per file by using a loop:
find . -name "file*" -exec sh -c '
for file do
printf "%sn" "$file"
find .//. -samefile "$file" | grep -c //
done' find-sh +
New contributor
edited 10 hours ago
Stéphane Chazelas
311k57586945
311k57586945
New contributor
answered yesterday
Entropy0Entropy0
562
562
New contributor
New contributor
add a comment |
add a comment |
Goking is a new contributor. Be nice, and check out our Code of Conduct.
Goking is a new contributor. Be nice, and check out our Code of Conduct.
Goking is a new contributor. Be nice, and check out our Code of Conduct.
Goking is a new contributor. Be nice, and check out our Code of Conduct.
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.
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%2f507553%2fpipe-each-batch-of-xargs-trough-wc-l%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
Doesn't your
find
command's-printf
have a%n
specifier for the number of hardlinks? You could simplysort
andtail
that– steeldriver
yesterday
Thanks man that is the right answer, but the sort is not working as expected after print f: find . -printf '%n' | sort -n How to to sort the answer and also print the file name and sort them by num,ber of hardlinks
– Goking
16 hours ago
Something like
find . -printf '%nt%fn' | sort -n | tail -n 1
should work, I think? if there is more than one file with the same number of links, it will return the one whose name is "numerically first"– steeldriver
15 hours ago
The way they are sorted are in order 5 5 5 5 5 5 4 4 4 4 and so on and not: just 5 and 4 so tail -n 3 gives me basically 3 5's, since they all point to the same file. But what i want is the unique files ordered by number by hardlinks sorted so tail - n 2 would give me 5 4 not 5 5.
– Goking
14 hours ago
Just use uniq command: find . -printf '%nn' | uniq | sort -n -r
– Goking
14 hours ago