Xargs Should Be In Your Command Line Toolbag

Поделиться
HTML-код
  • Опубликовано: 17 июл 2021
  • Xargs is an important shell utility to know because it allows you to execute to pass standard input as an argument to another command. While many command line programs allow standard input to be used as a parameter (things like grep, sed, awk etc.), many other programs do not allow for this (such as echo and rm). This is where Xargs comes in handy.
    REFERENCED:
    ► man7.org/linux/man-pages/man1...
    WANT TO SUPPORT THE CHANNEL?
    💰 Patreon: / distrotube
    💳 Paypal: www.paypal.com/cgi-bin/webscr...
    🛍️ Amazon: amzn.to/2RotFFi
    👕 Teespring: teespring.com/stores/distrotube
    DONATE CRYPTO:
    💰 Bitcoin: 1Mp6ebz5bNcjNFW7XWHVht36SkiLoxPKoX
    🐶 Dogecoin: D5fpRD1JRoBFPDXSBocRTp8W9uKzfwLFAu
    📕 LBC: bMfA2c3zmcLxPCpyPcrykLvMhZ7A5mQuhJ
    SOCIAL PLATFORMS:
    🗨️ Mastodon: distrotoot.com/@derek
    👫 Reddit: / distrotube
    📽️ LBRY/Odysee: odysee.com/$/invite/@DistroTu...
    DT ON THE WEB:
    🕸️ Website: distrotube.com/
    🐿️ Gemini Capsule: gemini://distro.tube
    📁 GitLab: gitlab.com/dwt1
    FREE AND OPEN SOURCE SOFTWARE THAT I USE:
    🌐 Brave Browser - brave.com/dis872
    📽️ Open Broadcaster Software: obsproject.com/
    🎬 Kdenlive: kdenlive.org
    🎨 GIMP: www.gimp.org/
    🎵 Ardour: ardour.org/
    💻 VirtualBox: www.virtualbox.org/
    🗒️ Doom Emacs: github.com/hlissner/doom-emacs
    Your support is very much appreciated. Thanks, guys!
  • НаукаНаука

Комментарии • 209

  • @first-thoughtgiver-of-will2456
    @first-thoughtgiver-of-will2456 2 года назад +133

    I've been writing bash scripts for years now and never bothered to thoroughly read the documentation. I have always used an inferior work around for this thank you for showing me the formal solution.

    • @thecashewtrader3328
      @thecashewtrader3328 2 года назад

      Yay uwu

    • @swindlesmccoop
      @swindlesmccoop 2 года назад

      yes i always would just set whatever i needed as a variable and use that but sometimes it just wouldnt work

  • @sasakanjuh7660
    @sasakanjuh7660 2 года назад +30

    Just a tip, if you are trying to create a bunch of sequentially numerated files with touch, you can use bash ranges {} syntax, like "touch {1..1000}".. It's really convenient, and it works with other types of sequences (a..z), and you can even pass a step in which next item will be incremented :)
    Nice video, btw! :)

  • @emiliadaria
    @emiliadaria 2 года назад +64

    Hey DT, your comparison of "-exec" and "xargs" wasn't entirely fair.
    Using -exec with the semicolon is more similar to calling xargs with the "-L 1" option. A fair comparison would be to use "find ... -exec rm {} +". The "+" tries to fit as many arguments to one command as it can just like xargs by default.

    • @sorrowfulevening8201
      @sorrowfulevening8201 2 года назад +1

      @Terminalforlife (LL) rm -f *.txt will do the job, no need for find.

    • @amx2311
      @amx2311 2 года назад +1

      @@sorrowfulevening8201 except the find results are not necesarily always in the same folder, nor will you know in what folders the results will pop up.

    • @sorrowfulevening8201
      @sorrowfulevening8201 2 года назад

      @@amx2311 agree.

    • @fagcinsk
      @fagcinsk 2 года назад

      @@amx2311 ... or if you have too many files to fit to arguments length.

  • @InfiniteQuest86
    @InfiniteQuest86 2 года назад +22

    xargs is a true lifesaver when it's needed. One key example is when you have a tool that needs to run on every file in a directory. Using ls and xargs with -n 1 can really blast out the results compared to trying to bash for loop it or something. The problem is people don't even know about it to consider it as an option.

    • @vikingthedude
      @vikingthedude 2 года назад

      Yeah I've always struggled to write bash for-loops because 1) They look kinda noisy when written inline and 2) I don't usually write my code in bash. With 'ls -1 | xargs' (or with -n 1 like you said) the command looks so much cleaner and is more declarative

  • @fennecbesixdouze1794
    @fennecbesixdouze1794 Год назад +1

    This was an incredibly well written and produced video. Really spectacular job running down this command.

  • @stephenflee7989
    @stephenflee7989 Год назад +1

    Hands down the best rundown of xargs I've ever seen, you rock my dude!

  • @phylwx
    @phylwx 2 года назад +20

    You're content is getting way better DT, congrats! I love this vid, really nice info.

  • @Mr76Pontiac
    @Mr76Pontiac 2 года назад +11

    XARGS is one of those insanely useful tools. It's something in my toolbox that I use EVERY day when I manage any one of the hundreds of servers I could be dealing with each day.
    Just for clarification, XARGS doesn't make FIND any more or less efficient. FIND will execute the RM statement (Or whatever it's internal equivalent is) for each result it returns. XARGS will pass as many parameters to RM (or whatever command) as it can if you don't set a limit on the number of parameters to pass in. That means that the RM command may be run multiple times against a large number of files/parameters. RM is intelligent with file transactions, just the same as relational databases are intelligent with their ACID transactions.
    As an example to really put this to the test:
    for c in `seq 100`
    do
    echo "----"
    echo "${c} step"
    echo "Creating Files"
    time seq 1000 | xargs -i touch {}.txt
    echo "Running RM with ${c} parameters"
    time find . -type f -name '*.txt' | xargs -n ${c} rm
    done
    This will start a loop. In each loop, it'll create 1000 files, then promptly delete them passing a number of files/parameters to be deleted each loop. So on loop one, a single file/parameter will be passed to RM for it to be deleted. On the second pass, two files will be passed to RM, etc.
    Note that `seq 100` is wrapped in the BACK-TICKS (Unshifted-Tilde key on standard US keyboards, at least), it's not a standard single quote.

  • @s4degh
    @s4degh Год назад +1

    These command line utility videos are amazing. Thank you for the content.

  • @GonzaloOviedoLambert
    @GonzaloOviedoLambert 2 года назад

    Thank you, very clear explanation and tested and documented. After 17 years, finally I will use xargs

  • @locatemarbles
    @locatemarbles 2 года назад +2

    Great timing DT. Was experimenting yesterday with fzf -m in combination with xargs and couldn't get the multiple inputs working. Them damn curly braces. Thank you.

  • @Hifonics78
    @Hifonics78 2 года назад +55

    Thank you DT for all what you do.

  • @ryanporidge1517
    @ryanporidge1517 2 года назад +5

    Love the new bash tuts! Looking forward to more

  • @matthewlandry1352
    @matthewlandry1352 2 года назад +1

    Never knew about xargs! Neither did my spellchecker. Now we both do! Thanks DT

  • @leroyonlinux283
    @leroyonlinux283 2 года назад

    I love this videos explaining those basic CLI tools. Because if you don't know they exist, its hard to find them. ESpecially for new users.

  • @filipemarques9144
    @filipemarques9144 2 года назад

    Awesome!
    This is one of my favorite channels!
    Congrats!

  • @alexb3617
    @alexb3617 Год назад +1

    very informative video, it literally answered all my questions. meaning all important points were considered and answered, like different use cases and comparisons with other similar tools, which is preferable in which scenario and so on. very good work indeed

  • @gonzalooviedo5435
    @gonzalooviedo5435 Год назад

    From Chile, thanks for your dedication and your team also, you are doing fantastic work every day, like the format of your videos and your english is very good, thanks for not talking fast!. I'm addicted to linux.

  • @guyboisvert66
    @guyboisvert66 2 года назад +1

    This is really useful! I made a script that processed several million files, output from the find command using -print0 and xargs with the -0 argument (for using paths / filenames with spaces) and launching another script, 16 of them in parallel with -n1 and -P16 arguments to xargs. The performance was phenomenal: the Dual Xeon E5-2660v2 (2 x 8 cores) usage was up to 95%, averaging about 80%. This is multi-threading bash scripting!

  • @johnarleevillarivera286
    @johnarleevillarivera286 2 года назад

    Nice explanation DT. Just found out about this channel what a gem.

  • @ziangtian
    @ziangtian 7 месяцев назад

    This is truly much better than reading the manual pages! loved the accent btw :)

  • @oneanime5551
    @oneanime5551 2 года назад +1

    Thank you DT for this amazing video!!!

  • @kGetMail
    @kGetMail 2 года назад

    Another great video from DT. Many thanks.

  • @kennyray1640
    @kennyray1640 2 года назад

    What a great video! I look forward to watching more!

  • @davidh.4944
    @davidh.4944 Год назад +2

    Rather than xargs, I've recently been learning how to use GNU parallel, and I love it. Its syntax is more cumbersome to work with than xargs, but it's also much more powerful. You can interleave multiple inputs (e.g. from stdin and files at the same time), position the incoming strings in any order you want in your command, and modify them before use (e.g. to remove file paths). You can even distribute jobs over multiple machines via ssh.
    I've updated a few of my scripts to use it and the results have been exceptional. For example I have one script for converting flac files to vorbis, ensuring that the filename, tags, and art are all kept intact, and the time to process one album of tracks dropped from about 3 minutes to under 20 seconds. Sweet.

  • @crazychicken0378
    @crazychicken0378 2 года назад +3

    Thanks again for these command line tutorials. I might honestly start scripting soon for really simple actions. Your videos are definitely where I look for information on all that. I might honestly learn posix shell script or bash or something haha

    • @paulstaf
      @paulstaf 2 года назад

      I use bash and expect scripting exclusively to manage Cisco devices in an enterprise environment. Don't have to worry about libraries from other programming languages being installed on whatever system I am using at the time.

  • @JustinBumpusBarnett
    @JustinBumpusBarnett 2 года назад +2

    Good stuff my dude. I had to open up a terminal and try this out myself.

  • @gibraanjafar1669
    @gibraanjafar1669 2 года назад +1

    Lovely explanation. Thanks a ton DT

  • @phamh79
    @phamh79 2 года назад +2

    thank you DT, love your content.

  • @Linux.Learner
    @Linux.Learner 2 года назад +1

    AWESOME SIRE! Much respect.

  • @charliekim2939
    @charliekim2939 Год назад

    I am learning a lot of command line tips and tricks from you. Thanks. By the way, you frequently use 'clear' to clear screen. You could use ctrl-l (el, not ai) to do the same, which saves a few finger movements.

  • @bhaveshverma8629
    @bhaveshverma8629 2 года назад +1

    Very Very awesome video. So much to learn.

  • @pavelsapehin4308
    @pavelsapehin4308 3 месяца назад

    Great structured intro into xargs, thanks DT!
    Here is an approximate ToC for this video:
    00:48 basic xargs usage + echo
    02:03 xargs -t flag = show expanded output
    02:55 xargs + ls = recursive ls
    03:36 example: /etc/passwd
    04:00 passwd + cut + sort + xargs
    04:41 xargs -I (capital i) flag = alias for arguments
    06:19 example: make 1000 txt files: seq + xargs + touch
    07:21 example: mv *.txt to *.text with ls + cut + xargs
    08:35 xargs -n flag (number of args) = multiline output with
    09:39 example: seq + xargs + bash + sleep
    11:34 example: xargs + find = remove all *.text
    13:50 performance: find --exec vs xargs rm
    15:18 summary
    This table of content was created using "Smart Bookmarks for RUclips" chrome extension.

  • @qball8up1968
    @qball8up1968 2 года назад

    Nicely done DT.

  • @fintarabg
    @fintarabg 2 года назад +1

    So useful, and so well explained!

  • @smit17xp
    @smit17xp 2 года назад +2

    Thanks. This will be very time saving

  • @combsmsteven
    @combsmsteven 2 года назад

    Just discovered your channel, love it

  • @minhthuan4479
    @minhthuan4479 2 года назад +1

    That's such a cool command. I like it.

  • @janekmachnicki2593
    @janekmachnicki2593 14 дней назад

    Thanks for another briliant tutorial

  • @subtitles1492
    @subtitles1492 2 года назад

    very useful and perfectly demonstrated. 👍🏻

  • @SylvesterInk
    @SylvesterInk 2 года назад +4

    Correct me if I'm wrong, but isn't the -exec functionality of find slower because it's executing the command for each individual find result, much like running `xargs -n 1`? I tested the find command using `-exec rm {} \+"`, which appends the results to a single command, and got the same time results as using xarg.
    So if you're using find, it's pretty much comparable to use -exec or xargs interchangeably, for basic tasks, though xargs is obviously more versatile for tasks that aren't related exclusively to the results of find.

    • @SylvesterInk
      @SylvesterInk 2 года назад +2

      @Terminalforlife (LL) That's where I went to confirm the use of -exec vs xargs, and it's exactly as I described. (The man file even makes the comparison to xargs for `+`.) I tested the 4 commands myself. find using `;` and `xargs -n 1` have nearly the same execution time. find using `+` and `xargs` also have nearly the same execution time (in fact find is just slightly faster.)
      I'm not trying to say that one method should always be used over the other, as it really depends on the situation. The point is that -exec is not slower than xargs when making an equivalent comparison.

  • @DiegoArcega1
    @DiegoArcega1 2 года назад

    Awesome video, I learned something new, thank you

  • @oleksiizatkhei7178
    @oleksiizatkhei7178 2 года назад +1

    Wow, cool explanation. Thank you

  • @jezbon
    @jezbon 10 месяцев назад +1

    It's still amazing that decades after shell and all these little tools were created for Unix that they're still insanely awesome. Still. That's just mindblowing. They were designed as the best operating system/logic building blocks of all time. Just a wonder of human ability to have nailed this.

  • @robertszakats148
    @robertszakats148 2 года назад

    Very useful video! Thank you!

  • @szevaa97
    @szevaa97 2 года назад +1

    Thank you very much for this video

  • @greob
    @greob 2 года назад

    Nice demonstration, thanks!

  • @amanwehib8367
    @amanwehib8367 Год назад +1

    Very good explanation!!!!!!

  • @James-ej4lb
    @James-ej4lb 2 года назад

    Great video DT thanks

  • @alisafapour9380
    @alisafapour9380 10 месяцев назад

    Thank you. Very helpful.

  • @thanhtungvu3216
    @thanhtungvu3216 2 года назад

    It's so cool xargs. Thank you!

  • @isoEH
    @isoEH 2 года назад

    Thanks! Learned several things here.

  • @n0kodoko143
    @n0kodoko143 2 года назад +1

    Love ya DT!! thank you!

  • @teejaded
    @teejaded 2 года назад

    Another fun tip for xargs subshells is you can export a function with export -f functionname and call it from the subshell. Makes it easy to write a parallel executed pipeline of bash functions.

    • @aqtmeto
      @aqtmeto Год назад

      I'm interested in this, can you elaborate? Maybe a quick example, thanks!!

  • @jaimeseuma6969
    @jaimeseuma6969 Год назад

    That's a nice tutorial! thanks a lot for it

  • @AngelFelizF
    @AngelFelizF Год назад

    Thanks, excellent video

  • @Chiqc
    @Chiqc Год назад

    beautiful video, thank you sir

  •  9 месяцев назад

    Thank you, very much, my friend!

  • @pewolo
    @pewolo Год назад +2

    Good video!
    I have one more command in my arsenal.
    But for the files creations, you can easily do that with bash brace expansion. The syntax looks like this:
    touch {1..1000}.txt

  • @YannMetalhead
    @YannMetalhead Год назад +1

    Good video, thank you!

  • @hupa1a
    @hupa1a 2 года назад +1

    Great video!

  • @genkiferal7178
    @genkiferal7178 4 месяца назад

    The lighting in this video is flattering for your blue or green eyes.

  • @stefanocardarelli9201
    @stefanocardarelli9201 2 года назад

    A pearl as usual. Keep it up dt

  • @markmcdonnell
    @markmcdonnell 2 года назад

    I'm also impressed by perf of xargs

  • @childfs6865
    @childfs6865 2 года назад

    Thank you!

  • @rc4652
    @rc4652 2 года назад +2

    This is very very cool.

  • @wusticality
    @wusticality 2 года назад

    Thanks for this

  • @ozzman530
    @ozzman530 2 года назад

    I use -a option to read from a file of hosts, and xargs can ping every host in the file. Using the -P option lets me ping multiple hosts at a time, and I can find the status of hundreds of hosts in a matter of seconds using ping. Good for identifying network problems.

  • @archishabanerjee9418
    @archishabanerjee9418 2 года назад

    Hey! This was very helpful, lucid and comprehensive.
    I wanted to ask .. how do we set the time taken to be displayed once a process is executed - Like it showed " took 5s" for you. Please do make a video for the same.
    Thanks in advance!

  • @bitti1975
    @bitti1975 2 года назад

    Nice, learned a few new things. Regarding 'printf': it's not the quotes wich where missing, but that the first argument of printf should be a format string (e.g. "%s " to get the same behaviour as 'echo'). Regarding 'find' --exec: 'rm' isn't the best example here, since 'find' has a '-delete' flag which does the same but as fast (or faster) as your xargs solution. But I can see how this trick can be useful in general.
    Another thing which might have been nice to show: I often use xargs without any input pipe, so it just takes input from stdin which I find useful if I just want to process a list I've in my clipboard.

  • @gorsama-2190
    @gorsama-2190 2 года назад +2

    Beautiful

  • @Rmly
    @Rmly 2 года назад

    Amazing content!

  • @philtoa334
    @philtoa334 2 года назад

    Really nice.

  • @Cyborg9799
    @Cyborg9799 2 года назад

    Thanks I have been using exec forever. As you said it works but the timing shows xargs is more efficient.

    • @dapodix
      @dapodix Год назад

      find -exec ... {} + makes it faster and safer than piping to xargs.. As opposed to find -exec ... {} \; which loops through each arg instead of processing multiple args at a time.

  • @sabitkondakc9147
    @sabitkondakc9147 2 года назад

    Greate Share! cool expanation, -exec is always a huge mess.

  • @EscapeePrisoner
    @EscapeePrisoner 2 года назад

    XXXX is a brand of beer in my country. People use it as standard input ALL THE TIME.

  • @dvr2alarm
    @dvr2alarm 2 года назад

    a count of processes in "-P" would keep the system responsive to other commands and functions. usually a quarter of the "ulimit -u"

  • @kshatriya5
    @kshatriya5 Год назад

    i love your time cmd very much

  • @greenmanreddog
    @greenmanreddog 2 года назад +1

    I'm sure 'time' measures the execution time of only the find command, this includes the file deletion with -exec, but not with |xargs, so is this a fair comparison?

  • @The_Penguin_City
    @The_Penguin_City 2 года назад

    Excelente video, felicidades.

  • @wliaputs
    @wliaputs 2 года назад

    Wow I wished I took time to learn xargs earlier!

  • @RodrigoPolo
    @RodrigoPolo Год назад

    ¡THANK YOU!

    • @RodrigoPolo
      @RodrigoPolo Год назад

      BTW, with ChatGPT I found a couple of interesting commands:
      find "$(pwd)" -type f -iname "*.md" -print0 | xargs -0 grep -l -i "keyword"
      find "$(pwd)" -type f -iname "*.md" -print0 | xargs -0 grep -zi "keyword" --files-with-matches
      and
      find "$(pwd)" -type f -iname "*.md" -print0 | xargs -0 grep -n -H -i "keyword"

  • @preetham524
    @preetham524 2 года назад +1

    please do a tutorial on shell expansion

  • @portlyoldman
    @portlyoldman 2 года назад

    Drove my Alexa totally mad! I thought having the wake up word as Echo was a good idea 🥺

  • @tadaoterwiel9656
    @tadaoterwiel9656 Год назад

    Toinen hieno video DT

  • @MrLeoStief
    @MrLeoStief 2 года назад +1

    Nice video, but recommending against using `find -exec` doesn't make sense. The reason it took so long is because you instructed it to run 1,000 individual rm commands by putting a semicolon at the end; replace that with a plus sign (or just use the -delete option in this case) and find will bulk delete those files in no time with no need for xargs.

  • @darthcabs
    @darthcabs 2 года назад

    Superb!

  • @volodymyrsen
    @volodymyrsen 2 года назад

    How did you implement the "~/ took {time} >" in your command line?

  • @shinbart7554
    @shinbart7554 2 года назад

    Thanks for video so helpful to me (a beginner for linux)
    BTW what's going on with caption it's set to Vietnamese

  • @a13xhackintosh
    @a13xhackintosh 2 года назад

    Clear explianation

  • @pushkarmadan5455
    @pushkarmadan5455 2 года назад

    Although I know a bash substitutes for some of the things shown in video there is no harm learning new methods of doing things.

  • @rdg8268
    @rdg8268 2 года назад

    Can I use xargs with cut command through a comma delimited file, and pass each position as next command arguments?

  • @mubba1992
    @mubba1992 2 года назад

    I Love it!!!

  • @_yuri
    @_yuri 2 года назад +1

    thAnks dt

  • @mohammednawaz7501
    @mohammednawaz7501 2 года назад +1

    Hey DT, please share your colorscript which comes when you do a clear command

    • @DistroTube
      @DistroTube  2 года назад +1

      Check my dotfiles repo on my GitLab (in show description). The clear color thingy is function in my config.fish.

  • @sundarbe
    @sundarbe 2 года назад +1

    I never knew about xargs command, I get basic xargs like behavior by using $() in bash. For example I'll do "echo $(seq 5)" to get the sequence in the same line. Advanced replacements like "-I" option you showed is going to be very useful. Thanks.

  • @podroznik2214
    @podroznik2214 2 года назад

    Are you running your own YaCy?

  • @salomonchambi
    @salomonchambi 2 года назад

    12:13 What about using the -delete flag with find command?: find . -type f -name "*.text" -delete Is there any advantage over -exec flag?

  • @MrSasukeUchihaEMS
    @MrSasukeUchihaEMS 2 года назад +1

    thaaaaaaaaaanks

  • @jonnykopp
    @jonnykopp 2 года назад

    I've recently run into issues trying to use xargs in a shell script calling functions declared in the same script. Still working through it. I think it has something to do with the new child terminal xargs opens needing to source the functions. I'm sure I'll get it, just giving a heads up.

    • @jonnykopp
      @jonnykopp 2 года назад

      @Terminalforlife (LL) I appreciate the offer but I enjoy the challenge. Take care.