Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
Unix for Mac OS X Users unlocks the powerful capabilities of Unix that underlie Mac OS X, teaching how to use command-line syntax to perform common tasks such as file management, data entry, and text manipulation. The course teaches Unix from the ground up, starting with the basics of the command line and graduating to powerful, advanced tools like grep, sed, and xargs. The course shows how to enter commands in Terminal to create, move, copy, and delete files and folders; change file ownership and permissions; view and stop command and application processes; find and edit data within files; and use command-line shortcuts to speed up workflow. Exercise files accompany the course.
In this movie we'll learn to use xargs to pass argument lists to commands. xargs is kind of a funny name. It's short for execute as arguments. What xargs does is it parses an input stream into items and then it loops through each item in that list and passes it to a command. I think it's easier to understand if we actually see it in action. So you remember that we had the wc command before for word count. So if we do word count on our lorem_ipsum.txt file that's inside our Unix files directory, you see that it comes back and it tells us the number of lines, words, and characters that are in lorem_ipsum.
Now, let's try a variation on this. Let's say if we want to echo lorem_ipsum .txt and we want to pipe that into wc. Now what it's doing is it's saying not the file lorem_ipsum.txt, but this string, this actual text lorem_ipsum.txt, how many characters does that have? There is 16. So that's what it's doing. It's not looking at the file. It's looking at the string. If we instead want to pass that string to wc as an argument, then we use xargs. That's what it does.
So now we get the exact same result as we did before. It's the exact same thing. It took the string and the input and it passed it to the command wc. The man pages refer to this as the utility commands. So it passes it to its utility command. xargs gives us a nice way to see what it's actually doing using the -t option. So now we can actually see the command right before it runs. It will output what its doing. So it's running wc lorem_ipsum.txt. Now, if we don't supply a utility command at all, let me just show you what it does.
It actually just echoes. it calls bin echo and then the file. So as a default setting, it will just echo that argument back to you. Now, that explains what xargs does. It passes its input as an argument to a utility command. But the power of xargs is in looping through a list of arguments. Let's imagine that instead of just this that now instead we are going to have lorem_ipsum.txt us_presidents.csv. And let's go ahead and put that again with the word count at the end. Now you can see that it passes both of these as arguments to word count.
So it's the same as if we had typed we lorem_ipsum.txt us_presidents.csv. But sometimes you don't want xargs to run its command with all of the arguments. What we want is for it to loop. And so we can do that by using the -n option to limit how many arguments it passes. So we'll just say 1 to start with. We can also take that space away to make it a little more clear that the 1 goes with the n, and now it will take one argument, pass it to wc, and then it will loop and it will take the next argument and pass it to wc. So n is the number of arguments that we use on each loop through.
So now that you see the difference between what it did before. Now it's actually calling wc two times, not once, but twice. And to see an example of this using higher numbers of n, let's say that we had echo 1 2 3 4, and let's pipe that into xargs, and then -n2, and we don't have to specify anything. We're just going to echo those back. Now you see what it does. 1 and 2 get passed in as an argument, then 3 and 4 get passed in as an argument. -t will make that really clear. It's passing in the first two, then it goes through again. A second loop and passes in the next two.
If there had been an uneven number, well, then that last one would just get passed in by itself. So for example, let's say that we have ls to list our directory. We can pipe that into xargs. That will be all of the file names in our directory, and we'll do -n 3 and echo, and now you see we get three file names and then we get a new line. We get three more file names, because it's just echoing each of those file names broken up. xargs also has a -L option which limits what it takes in. Let me show you that. If I just had echo 1 2 3 4 and I pipe that into xargs with the capital L 2 option, you don't really notice a difference, because it's all just one line.
But let's instead use a multiline document and it will become a lot clearer. Let's say we have head of lorem_ipsum.txt, pipe it into xargs, and we will use -L 2, and I won't specify the echo. That's the default. What we're seeing is the first two lines of the file echoing back to us. If I instead use -n, you'll see that we get the first two words of each line. Now, the n and L options are mutually exclusive, so you don't want to use both. And they can be a little bit tricky, because xargs can find its arguments by splitting on either spaces or on lines, and it depends a little bit on the format that the input was sent in and whether or not you've specified the n or L the option.
It can be a little tricky, so you may have to play with it a bit to get it to do exactly what you want. Let me show you another example. Let's say we have cat, our file fruit.txt, and we'll pipe that into xargs. So what xargs is going to do is just echo each one of those back in a line. But here is what I want to show you. We can actually instead of just saying we want to echo this, we can actually put a placeholder. We'll specify it with the capital I option, then we can use the two curly braces and then we can actually place this somewhere in position. So for example, I can have buy more: and then it will drop in the value where I've put this placeholder. See what it does? So now we have the ability to actually not just take that argument, but to take that argument and put it inside something, to put in a specific place rather than just forcing it to just use the argument by itself.
You don't have to use these curly braces. That's the most common one, but you can actually specify absolutely anything you want here. So I could have fruit. You just want to make it something that's unique. :FRUIT. Right? And it will drop it in there. So all you're doing is saying all right, with the -I option here is what I am going to declare as my placeholder and wherever you see that, that's where the argument ought to go. There is one last catch to using xargs. And that's that xargs, when it tries to figure out where the arguments are, it will split things up based on either spaces or control characters.
This can cause some problems. So for example, let's say I do ls of my Library directory. So inside Library, let's take a look at what's in there. Let's just grab the first few of those by piping this through grep. So in grep I'll look for A, followed by anything that comes after it, and so it will just show me those first four. Now, let's take that list and let's pipe that into xargs and let's just have it echo it back to us. That's all we're going to do. We'll use the n1 option, so it will break it. Each item that it finds, each argument, will get sent through to echo one time.
See what happened? Application Support got broken in half. Autosave Information got broken in half. It thought they were each different arguments, because it split it up on the space. So you see why this causes the problem. In order to get around this what we need do is the -0 option and that will tell it to split on null characters as separators instead of on spaces and new lines. So now when I hit Return, now it correctly does break them up. The main place that you are going to use that -0 option is with file names.
If you're working with file names, you probably want to use that -0 option to be safe. So it's a bit of a quirk, but it is something that you have to keep in mind as you're passing things to it is, is there a chance that there will be a space in the data that I'm passing in that would screw things up? That gives you a good explanation of what xargs does. Next, let's try some examples of how we can put it into action and make it work for us.
Find answers to the most frequently asked questions about Unix for Mac OS X Users.
Here are the FAQs that matched your search "":
Sorry, there are no matches for your search ""—to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.