 One of the most important shell utilities is the xargs command. What is xargs? It allows you to take standard input and pass that along as an argument into another command. You guys know that many shell commands actually allow you to take standard input and use that as a parameter for that command. Things like grip, sed, awk, cut, you know, they allow for that. But many shell utilities, shell programs, they don't actually allow you to take standard input and use that as a parameter for those commands. So think about the ls command, the list command, or the remove command, or, you know, more echo, you know, all those kinds of utilities. You can't just, you know, pipe something into them, right? You can't just take standard input and put that directly into those commands. To do that, you actually need another program to help with this, and that is what the xargs command does. So let me switch over to a terminal and show you the basic format of an xargs command. So typically how you would run xargs as you would do some command, I'm going to call it command one, and then you would pipe that into xargs space and then command two. And what this is doing is the output from command one is used as an argument for command two. I hope that makes sense. So let me give you a real world example. I'm going to do a sequence of numbers one through five. So I'm going to do seq space five. That gives me one through five each on their own line. But maybe I don't want one through five each on their own line. Maybe I want to pipe that into echo because echo would take that one through five and give me one through five all on a single line. Well, the problem is you can't pipe into echo echoes. One of those commands that just doesn't take standard input like that as an argument. But what you could do is pipe sequence five into xargs space echo, and then you get one through five all on one line. Now, I actually did not need to add echo to this command here because by default xargs if you don't give it a second command to use it automatically defaults to echo. So if I run that without the word echo, it returns the same output. One of the flags you need to know about with xargs is the dash t flag. So let me run that a sequence five again and then pipe that into xargs. And what I'm going to do this time is give it the dash t flag and that will say, hey, tell us the command that is being used. What are the arguments being passed into? And that's going to print out two lines now. You still get your standard output, which was the one, two, three, four, five. But it also prints out the actual command it runs to get that output. And of course, the command it's running is echo one, two, three, four. Because by default xargs uses echo unless you specify something else. Maybe instead of echo, we'll do printf. So it's printf one, two, three, four, five. It actually can't run that command because of excessive arguments. We probably needed that being wrapped in quotes. And of course it's not wrapped in quotes. One of the most common shell commands you run is the ls command. The problem with the ls command is it also can't take standard input as a parameter. But what you could do is you could pass a command into xargs and then pass along ls. So if I ran ls, pipe that output into xargs and run another ls, it's actually going to run ls on every single directory and file on this directory. So it's almost like a recursive ls, which ls has the ability to do recursive listing anyway, so it's not really that useful of a command. But that was just an example of how you could pipe something into ls using xargs. Now on Linux systems, there is a file called slash etsy slash pass wd, which lists all the users on your system, every single user on your system. And the problem with this particular file, if I cat it out, you see it's kind of a mess to read because there's really no spaces. It's a bunch of information and columns separated by colons. But we could actually clean this up a little bit. So what I could do is I could run this command here. This is the cut command dash d using the delimiter, the colon. I did a video just a couple of weeks back on the cut command. So check out that video if you're unsure about what cut does. But what it's going to do is it's going to go and use all the colons as delimiters. And it's going to grab the first field from each line. So I'm going to grab the first column from each line of the slash etsy slash pass wd file. And then it's going to sort it. So it's going to give me an alphabetical sorting. And then we're going to pass it into xargs, which xargs, of course, is going to use echo. And it's going to spit out a list of all the users on the system sorted alphabetically on one line. The next flag I want to show you is the dash capital I flag. So if I run an LS command here in my home directory, then I'm going to pipe that into xargs. And I'm going to give it the dash capital I flag. And what this does, it allows us to specify a placeholder, basically an alias for the arguments. So the output from the LS command in this case. So I'm going to specify in this case that the curly braces are basically a placeholder. They signify the output of the LS command. And then we'll give it our second command. In this case, I'm going to give it echo. And what I'm going to do is I'm going to do a slash home slash dt slash. Then I'm going to do the curly braces. And you see what that's going to do there? Again, the curly braces are the argument. It's the output from the LS command. And what it's going to do is it's going to echo slash home slash dt slash the name of the file or the directory because that's the output of the LS command. And you see how that would work if I ran a regular LS. Of course, you get just the file names themselves and not the full path. But what I did with this is I took XORGs and I echoed the full path slash home slash dt. And then the name of the file or the directory. Let me clear the screen. I'm going to up arrow back to that argument. Now you could specify anything as the placeholder, as the alias for the input. So if you didn't want to use the braces, I mean, you could use anything. I mean, I could use for capital X's here, right? It's not likely that that would ever, you know, occur naturally in standard input anyway. So I could use that and I would get the exact same output from running that command. Now this is where things really start getting interesting. So I made a test directory here called foo on my system. I did an LS, it's an empty directory, but let's make something. Let's make some files. Say I wanted to make 1,000 files in this directory. Maybe I want to do them sequential numbers. So I could do a sequence 1,000, you know, if I just ran it, you know, it just outputs one through a thousand here. But what I could do is then pipe that into XORGs. And again, let's do the dash capital I flag. I'm going to do the curly braces. And then what I want to do is I want to run the touch command. So touch is a command that creates an empty file for you. It's going to create a file named one through a thousand. So what we're going to do is we need to specify the curly braces again. So basically take everything from the sequence arguments. And then I also want you to add .txt to the names. So if I ran that and did an LS, you know, we created a one through 1,000.txt. Now imagine I made a mistake. I didn't want all those to have a .txt extension. I wanted them to have a .txt extension. Well, how would you go about fixing that mistake? Well, it's actually pretty easy. What I could do is I could do an LS. So I would LS one through a thousand .txt. Then I could pipe that. You know what? Let's pipe it into cut because we did a video about cut the other day. Let's use the period as a delimiter. So it's going to grab one through a thousand without the .txt because I specified stop at the period. Right? Period signifies the field separators. And then I only want you to return field one. So dash F1. So it's just going to give me a list one through a thousand, those numbers. Then let's pipe that into XORGs and give it the dash capital I flag one more time. Let's do the squirrely braces. And we're going to do the move command this time. I want you to move all of the arguments .txt. And I want you to move them to all of the arguments .txt. And if I did this correctly, run a LS. Now all the files have been renamed essentially. So now I have one through a thousand .txt. Now sometimes you don't want the output to be all on one line. For example, if I ran a LS and pipe that into XORGs and remember the dash t flag, if I did that, you know, it's going to be a massive amount of output. But at the very beginning, remember what it's actually running is echo and then all of the output. And then of course at the end it actually gives the output. So it's a very massive list of stuff here. But you don't have to have it treat every argument like that. You can specify a max number of arguments. So I could do dash in one, for example. And if I hit enter there, you see it takes each argument separately. It runs echo one and then you get the output echo two. Then you get the output echo three, output et cetera, et cetera. Instead of echo one through a thousand and then get the output for the whole thing. I hope that makes sense. So dash in is a number of arguments. If I did dash in two, what it's going to do is it's going to echo them two at a time. So it's going to echo one, two, and then it's going to echo three, four, et cetera, et cetera. Another example of a dash in flag, let me do a sequence five one more time. So we're going to do dash in one. So just the max arguments is one. And then the second command we're going to pipe this into, I'm going to pipe it into bash. We're just going to do some shell here. We're going to run the echo command dollar symbol zero. If I ran that, I get an error because I obviously did not add the S to XORGs. But there is that. Now, why did I run that? Because obviously that's just a very long and convoluted way of just doing sequence five piped into XORGs without all the extra stuff. It's because I want to also add the dash P flag for the number of processes. So in is the max number of arguments at a time. Dash capital P is the max number of processes at a time. I'm also going to do one process at a time. And then the command bash echo and then run that. It's still the same thing, but you will see it's actually not because what I'm going to do is I'm going to do echo dollar sign zero. And then I'm going to do a semicolon. I'm also going to tell it sleep one second. So what this is going to do is it's going to take one argument and one process at a time and in between it's going to sleep for one second. Now watch what happens. One, two, three, four, five. So that's kind of cool. So you have the dash in flag. Again, that's the max number of arguments at a time and the dash capital P flag. That's the max number of processes at a time. And just to make sure no one's confused thinking, well, max number of arguments, max number of processes, are they the same thing? No, they're not because if I did dash P two for two processes at a time, you see now I get two processes at a time. It did one and two together at first and then it did three and four together on the next process and then it did five by itself because there wasn't another argument to go along with five to make that two. Now probably the most common command people use with XARGs is the find command because the find command is really powerful but you can't really pipe anything into find but of course you can use XARGs with find. So I'm going to paste this command here. So this is a find command. What this is going to do is we're in that foo directory that has one through a thousand dot txt. Remember that? Well, what we're going to do is I'm going to find in foo, this directory, dash type F, find all the files, dash name that have dot txt as an extension and then we're going to pass this dot exec flag that is execute this command, remove RM, all the files that have dot txt at the end. Now why would I use XARGs if find has this execute flag? Well, the execute flag for find actually is kind of slow. I mean, it works but it's kind of slow. I could run that command by the way and we're already in foo. Let me CD back up into home and then from here specify find in foo type file name dot txt execute the remove command on all of the arguments. And if I CD back into foo and did an LS that is empty directory, we deleted those 1000 files. Now let me create those 1000 files. One more time, I'll rerun that sequence command, sequence 1000, pipe it into XARGs with a dash capital I flag and then run touch on that. So we get all of those created. Now other than using find with the execute flag a better way to do this is actually to just use XARGs. So if I go back up here to that last command, what I would do is let's remove all the dash exec stuff and just pipe that into XARGs and then remove. That's all we would need to do. Again, I'm actually in foo already. So instead of find foo, let's just find period. So we're in the directory foo. And then what this should do is remove all 1000 of those files for us and that's exactly what that did. Now one thing you may have noticed when I ran the find exec command to remove those 1000 files, it hung for a second. It took about a second for that command to execute before I got the shell prompt again. When I did it passing it into XARGs, I got another shell prompt immediately like it was lightning fast. We could actually put that to the test. I could time it. Why don't we do that? Let me go ahead. I'm gonna create 1000.txt files again and let's actually time this. So I'm gonna time the find command running the exec flag for find and then removing the arguments. And you see what the time is. So the real time is 0.708. The user time is 0.543. The system time is 0.148. Now let's do the same thing. Except this time, let's use XARGs to remove those files. So we're gonna use find to find the .txt files but we're gonna pipe it into XARGs RM. And you see how lightning fast that was? That was almost instantaneous. So if those of you worried about speed, especially with your scripting, you would never really wanna use the exec flag with find, especially when you're dealing with large amounts of files. You always wanna just pipe that into XARGs. So in a real world example, you can see XARGs is much quicker. Typically people would tell you that XARGs is just like six times, seven times more efficient than using the exec flag with find. So that was just a very brief overview of the XARGs command. It's really cool utility and it's one you need to know about because it's one you're gonna have to use because sometimes you're gonna want to pipe information into some of the shell utilities that normally can't be piped into. How do you get that information to be piped into them? Well, you have to use XARGs. Now before I go, I need to thank the producers of this episode, Absi Gabe, James, Mitchell, Paul, Wes, Akami Allen, Chuck, Kurt, David, Dylan, Gregory, Haco, Erion, Alexander, Peace, Arch, and Fedor, Polytech raver Scott, Steven, and Willie. These guys, they're my highest tiered patrons over on Patreon without these guys. This episode about the XARGs command would not have been possible. The show is also brought to you by each and every one of these ladies and gentlemen as well. All these names you're seeing on the screen. These are all my supporters over on Patreon because the DistroTube channel is supported by you guys, the community. If you like my work and you wanna support me, please consider subscribing to DistroTube over on Patreon. All right, guys, peace.