13.07.2015 Views

Linux System Administration Recipes A Problem-Solution Approach

Linux System Administration Recipes A Problem-Solution Approach

Linux System Administration Recipes A Problem-Solution Approach

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 8 ■ USING THE COMMAND LINE BETTERThis is particularly useful if you’re using the -p option because you can confirm one file at a timerather than all at once.Filenames containing whitespace can cause problems, because the whitespace is usually taken todenote the end of a filename. xargs and find can deal with this, using GNU extensions to both in orderto break on the null character rather than on whitespace:find tmp -maxdepth 1 -name *.mp3 -print0 | xargs -0 rmYou must use these options (-print0 for find, -0 for xargs) on both find and xargs or on neither(don’t mix the two!), or you’ll get odd results.Another common use of xargs with find is to combine it with grep. For example, the following willsearch all the *.pl files in the current directory and subdirectories and print the names of any that don’thave a line starting with use strict.find . -name '*.pl' | xargs grep -L '^use strict'Enforce good practice in your scripting! (See recipe 1-6.)xargs and File ContentsIt can also be useful to pipe the contents of a file into xargs as input, rather than piping in the output of acommand. The following would take the arguments listed in the file diff-files in groups of two and rundiff on them:xargs -t -n2 diff < diff-filesSo, if the diff-files file consisted of this:sample1 alternate1sample2 alternate2then xargs would run the following:diff sample1 alternate1diff sample2 alternate2This can be a quick way of comparing large numbers of files. (Use -p instead of -t to get a pauseafter each diff as well as an echo of the command.)You can also use a listings file and xargs to concatenate the contents of those files:xargs cat < list-of-files > files-contentsThis will take input from list-of-files and cat each file in turn into files-contents.■ Note You can generate list-of-files using xargs as well! The following would get all of your LaTeX sourcefiles in the current directory into one list, ready to be stuck together:182Download at WoweBook.Com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!