

find is the Swiss army knife of Unix commands. You can’t be very selective about what files get processed this way. Both cp and rm, for example, offer a -r flag (“r” for “recursive”) that will descend into an arbitrary number of levels of subdirectories, but these are all-or-nothing selections. Some commands will try to help with this. But you cannot write a wildcard expression that will simultaneously describe files in a directory and in its subdirectories. You can write a wildcard expression to look at a variety of files in one directory, or at a variety of files in one or more subdirectories of that directory, or in one or more sub-subdirectories of that one. They can only describe one “level” of directories at a time. Wildcards give us a way to describe a number of files at once. Ls | grep -v '\.bak$' | grep -v '~$' | xargs -i cp ~/xargs (If you were going to do that, you would probably just have typed the whole command.) So, usually, you will run this with a list of files names that you have collected into a text file, or you may pipe into xargs the output of a command that lists the files you want.


Where do the file names come from? You’re not likely to type them directly at the keyboard. In this form xargs will read a list of file names (paths) from the standard input and will simply tack them on to the end of the partial command. In its simplest form, you can use xargs like this: xargs partial-command Xargs reads a list of file names from the standard input and fills those file names in to a command of your choosing. Ls oldProject/* | grep -v '\.o' | grep '\.'Ĭp `ls oldProject/* | grep -v '\.o' | grep '\.'` newProject/ Let’s see how the example works: ls oldProject/* Let’s set up a simple version of the example that we just went through.

That gives us the list of files we want to copy, so we can now feed that into a cp command: cp `ls oldProject/* | grep -v '\.o' | grep '\.'` newProject/ So’ we’d like to remove from our list any files that have no periods in them at all: ls oldProject/* | grep -v '\.o' | grep '\.' Now, executables in Unix usually have no extension at all. o files from the list if old project files. The -v option tells grep to select every line that does not match our pattern. The backslash in front of the period is needed because, normally, a period in a regular expression means “match any single character”, and we actually want to match only periods. We get a list of all the files in the old project. Now, if we could produce a listing of the files that we wanted, we could use backticks to feed that list into the appropriate spot in a cp command.
Exec grep wildcard code#
Now, you might start by copying the files from the old project to the new one: cp oldProject/* newProject/īut that would copy not only your program’s source code but also files that you don’t want carried over to the new project, the *.o files and executables produced when you compiled the old project. Let’s assume that the old project is in a directory named oldProject and that we have just created a directory newProject to hold the new one. dateĪs an example of where this might be useful, suppose that you had been working on a large programming project, and now wanted to start another, similar project. Give the following commands, noting the difference between the effects of the forward and backward quotes. Instead of treating the enclosed characters as plain text, the shell treats them as a command, runs that command, and replaces the whole `-surrounded string by the output of that command. If, however, we write something between a pair of backticks (`, often found just to the left of the “1” key), almost the opposite happens. In essence, quoting suppresses the normal activity of the command shell. We used double-quotes ( ") and single quotes ( ') to tell the shell to suppress its usual treatment of special characters, making them “normal” characters instead. 1 BackticksĮarlier, we looked at different forms of quoting in shell commands. First, though, we will look at a technique by which the output from one command can be supplied as part of the parameter list to a second command. In particular, we will look at two commands, xargs and find, that are interesting in part because they allow you to issue other commands from them. Next we will look at other ways to combine selective inputs and combinations of commands. Pipes allow you to combine commands in interesting ways. Redirection allows you to alter the behavior of individual commands by changing where they get their input and where they store their output.
