| Post Reply | RetroFed Forum Forum Index » Linux |
| Author | Message | ||
|---|---|---|---|
thingsiplay
lemmy.ml
thingsiplay@lemmy.mlJoined: 2026-01-09 |
Just wanted to share an alias I have in use and found it useful again. It's a simple wrapper around The arguments are interpreted as the command to execute. The only thing to remember is using the
Useful for quickly operating on each line of a file (in example to download from list of urls) or do something with any stdout output line by line. Without remembering or typing a for loop in terminal. |
||
| 34 Original | |||
|
hades
feddit.uk
hades@feddit.uk Joined: 2025-06-04 |
Nice! I used to do something like this, which avoids xargs altogether:
|
||
| 13 | |||
thingsiplay
lemmy.ml
thingsiplay@lemmy.mlJoined: 2026-01-09 |
Nice! I used to do something like this, which avoids xargs altogether: cat urls.txt | while read url; do echo download $url; done
You should use |
||
| 9 | |||
davel
lemmy.ml
davel@lemmy.mlJoined: 2023-07-08 |
Nice! I used to do something like this, which avoids xargs altogether: cat urls.txt | while read url; do echo download $url; done
Usually this is the way. Once you enter xargs’ world, you lose access to your shell aliases, functions, and un-exported variables, which will often bite you in the ass. |
||
| 7 | |||
Static_Rocket
lemmy.world
Static_Rocket@lemmy.worldJoined: 2023-06-12 |
Nice! I used to do something like this, which avoids xargs altogether: cat urls.txt | while read url; do echo download $url; done
You can also avoid cat since you aren't actually concatenating files (depending on file size this can be much faster):
|
||
| 11 | |||
|
just_another_person
lemmy.world
just_another_person@lemmy.world Joined: 2023-07-07 |
Vibe coded? You know how this works? Good luck with that. I look forward to your future posts about it. 🤣 |
||
| -15 | |||
|
bizdelnick
lemmy.ml
bizdelnick@lemmy.ml Joined: 2023-06-26 |
I almost never use |
||
| 2 | |||
TrickDacy
lemmy.world
TrickDacy@lemmy.worldJoined: 2023-07-27 |
Vibe coded? You know how this works? Good luck with that. I look forward to your future posts about it. 🤣
Literally no indication of that |
||
| 3 | |||
thingsiplay
lemmy.ml
thingsiplay@lemmy.mlJoined: 2026-01-09 |
I almost never use xargs. The most common case for it is find, but it is easier to use its -exec option. Also, with find your example is incorrect. You forgot that file names can contain special characters, the newline character in particular. That's why you need to pass -print0 option to find and -0 option to xargs.
The example itself is not incorrect. It is just an example to show how the foreach works, not meant to be a full command on itself. Usually I don't have newline characters in files either, so that is not a concern for myself. If I would want to be sure, then yes I would use zero option. But its good to point that out. |
||
| 1 | |||
|
thevoidzero
lemmy.world
thevoidzero@lemmy.world Joined: 2024-01-25 |
I recommend you gnu parallel. It does similar things, but runs the commands in parallel. And it's way easier to pipe than xargs. If you really need it to run one command at a time you can give number of cores to 1. And it also has progress bars, colors to differentiate stdout fo different commands, etc. Basic example: to echo each line parallel echo < somefile.txt To download all links, number of jobs 4, show progress
You can do lot more stuffs with inputs, like placing them wherever with |
||
| 3 | |||
|
non_burglar
lemmy.world
non_burglar@lemmy.world Joined: 2023-06-16 |
Be careful. Because it only formats stdin streams to into string(s), xargs can be very dangerous, depending on the command to which the arguments are being passed. Xargs used to be a practical way to get around bash globbing issues and parenthetical clause behavior, but most commands have alternate and safer ways of handling passed arguments. find -exec is preferable to xargs to avoid file expansion "bombs", plus find doesn't involve the shell, so it doesn't care about whitespace problems. |
||
| 3 | |||
thingsiplay
lemmy.ml
thingsiplay@lemmy.mlJoined: 2026-01-09 |
I recommend you gnu parallel. It does similar things, but runs the commands in parallel. And it's way easier to pipe than xargs. If you really need it to run one command at a time you can give number of cores to 1. And it also has progress bars, colors to differentiate stdout fo different commands, etc. Basic example: to echo each line parallel echo < somefile.txt To download all links, numb...
I am actually aware of parallel and use it for a different tool / script I built. The purpose of parallel is different than xargs, right? I mean xargs works on each line of a stdout string, which is what I was using it for. I never thought parallel as an alternative to xargs and need to investigate into this idea more. Thanks. |
||
| 2 | |||
cravl
slrpnk.net
cravl@slrpnk.netJoined: 2023-06-10 |
You can also avoid cat since you aren't actually concatenating files (depending on file size this can be much faster): while read -r url; do echo "download $url"; done < urls.txt
Pro tip: you can also put the |
||
| 1 | |||
Static_Rocket
lemmy.world
Static_Rocket@lemmy.worldJoined: 2023-06-12 |
Pro tip: you can also put the < urls.txt at the start for readability. The arrow doesn't have to point at the command.
Out of curiosity, how?
Is a syntax error.
Result in an infinite loop. |
||
| 2 | |||
cravl
slrpnk.net
cravl@slrpnk.netJoined: 2023-06-10 |
Out of curiosity, how? < urls.txt while read -r url; ... Is a syntax error. while read -r url < urls.txt; ... Result in an infinite loop.
It's weird with a multi-command string (i.e. when semicolons get involved), shortest answer is to fiddle with it. I think it may work at the beginning if you put the rest of the command in a (subshell)? |
||
| 1 | |||
| Post Reply |
Back to top |