How to pass a list of file names to xargs after checking if the file exists?
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a command (command1
) that returns a list of file names like below
/consumer/a.txt
/consumer/b.txt
/consumer/doesnotexist.txt
When I pipe the output like command1 | xargs command2
command2
throws exception if one of the files does not exist.
How do I remove non-existant files before piping it to command2
. I am expecting something in the lines of
command1 | xargs remove_nonexistant_files | xargs command2
command2
should receive
/consumer/a.txt
/consumer/b.txt
as input.
bash files osx
New contributor
add a comment |Â
up vote
0
down vote
favorite
I have a command (command1
) that returns a list of file names like below
/consumer/a.txt
/consumer/b.txt
/consumer/doesnotexist.txt
When I pipe the output like command1 | xargs command2
command2
throws exception if one of the files does not exist.
How do I remove non-existant files before piping it to command2
. I am expecting something in the lines of
command1 | xargs remove_nonexistant_files | xargs command2
command2
should receive
/consumer/a.txt
/consumer/b.txt
as input.
bash files osx
New contributor
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a command (command1
) that returns a list of file names like below
/consumer/a.txt
/consumer/b.txt
/consumer/doesnotexist.txt
When I pipe the output like command1 | xargs command2
command2
throws exception if one of the files does not exist.
How do I remove non-existant files before piping it to command2
. I am expecting something in the lines of
command1 | xargs remove_nonexistant_files | xargs command2
command2
should receive
/consumer/a.txt
/consumer/b.txt
as input.
bash files osx
New contributor
I have a command (command1
) that returns a list of file names like below
/consumer/a.txt
/consumer/b.txt
/consumer/doesnotexist.txt
When I pipe the output like command1 | xargs command2
command2
throws exception if one of the files does not exist.
How do I remove non-existant files before piping it to command2
. I am expecting something in the lines of
command1 | xargs remove_nonexistant_files | xargs command2
command2
should receive
/consumer/a.txt
/consumer/b.txt
as input.
bash files osx
bash files osx
New contributor
New contributor
edited 6 mins ago
New contributor
asked 7 mins ago
Sathish
11
11
New contributor
New contributor
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
0
down vote
$ for file in $(command1); do if [[ -f "$file" ]]; then echo "$file"; done | xargs command2
add a comment |Â
up vote
0
down vote
command1 |
xargs sh -c 'for p do [ -f "$p" ] && printf "%sn" "$p"; done' sh |
xargs command 2
The extra bit in the middle is a another xargs
invocation of a short script that basically just loops over its given command line arguments and prints the pathnames that correspond to existing regular files (or symbolic links to regular files). These existing pathnames are then passed on to the last part of the pipeline.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
$ for file in $(command1); do if [[ -f "$file" ]]; then echo "$file"; done | xargs command2
add a comment |Â
up vote
0
down vote
$ for file in $(command1); do if [[ -f "$file" ]]; then echo "$file"; done | xargs command2
add a comment |Â
up vote
0
down vote
up vote
0
down vote
$ for file in $(command1); do if [[ -f "$file" ]]; then echo "$file"; done | xargs command2
$ for file in $(command1); do if [[ -f "$file" ]]; then echo "$file"; done | xargs command2
answered 6 mins ago
DopeGhoti
41.9k55180
41.9k55180
add a comment |Â
add a comment |Â
up vote
0
down vote
command1 |
xargs sh -c 'for p do [ -f "$p" ] && printf "%sn" "$p"; done' sh |
xargs command 2
The extra bit in the middle is a another xargs
invocation of a short script that basically just loops over its given command line arguments and prints the pathnames that correspond to existing regular files (or symbolic links to regular files). These existing pathnames are then passed on to the last part of the pipeline.
add a comment |Â
up vote
0
down vote
command1 |
xargs sh -c 'for p do [ -f "$p" ] && printf "%sn" "$p"; done' sh |
xargs command 2
The extra bit in the middle is a another xargs
invocation of a short script that basically just loops over its given command line arguments and prints the pathnames that correspond to existing regular files (or symbolic links to regular files). These existing pathnames are then passed on to the last part of the pipeline.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
command1 |
xargs sh -c 'for p do [ -f "$p" ] && printf "%sn" "$p"; done' sh |
xargs command 2
The extra bit in the middle is a another xargs
invocation of a short script that basically just loops over its given command line arguments and prints the pathnames that correspond to existing regular files (or symbolic links to regular files). These existing pathnames are then passed on to the last part of the pipeline.
command1 |
xargs sh -c 'for p do [ -f "$p" ] && printf "%sn" "$p"; done' sh |
xargs command 2
The extra bit in the middle is a another xargs
invocation of a short script that basically just loops over its given command line arguments and prints the pathnames that correspond to existing regular files (or symbolic links to regular files). These existing pathnames are then passed on to the last part of the pipeline.
answered 34 secs ago
Kusalananda
112k15216344
112k15216344
add a comment |Â
add a comment |Â
Sathish is a new contributor. Be nice, and check out our Code of Conduct.
Sathish is a new contributor. Be nice, and check out our Code of Conduct.
Sathish is a new contributor. Be nice, and check out our Code of Conduct.
Sathish 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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f478526%2fhow-to-pass-a-list-of-file-names-to-xargs-after-checking-if-the-file-exists%23new-answer', 'question_page');
);
Post as a guest
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
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
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