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 BETTER26 cd ..27 }2829 for dir in $@; do30 rename_recursive "$dir"31 done3233 cd "$cwd"34 exit 0Lines 01–07 do the setup and check for at least one argument (the directory to iterate over), line 09records your current directory so you can get back there at the end, and line 10 assigns the argument to anamed variable. Lines 12–27 are the part that does the work. You cd into the directory given by theargument; then in line 15 you get a listing of that directory and iterate over each item in the listing. Inlines 16 and 17, you check whether that item is a directory, and if it is, you run the rename_recursivefunction on it—this is the recursive bit! Otherwise, in lines 19–23, you check to see whether the filenameends in .html and rename it to .shtml if so. Line 25 loops you back to line 15, and once we’ve dealt withall the items in the directory, you jump back up a directory, and the function is finished.Lines 29–31 do the initial calling of the function on each argument in turn (so the function will berun, recursively, on each directory you name on the command line), and line 33 makes sure you’vewound up back where you started.8-3. Implementing Programmable Completion with bashAnother trick you’ll already be familiar with is hitting Tab to autocomplete filenames and commands.This is great and a massive time-saver, but there are a few ways to extend it.The first tip isn’t, in fact, exactly extending autocompletion, but it helps you locate directories moreeasily. $CDPATH does for cd what $PATH does for executables. In other words, it’ll allow you to type just thename of a directory, and if the parent of that directory is in your $CDPATH, it’ll take you straight there. Itsaves having to type the full path each time.So, say you regularly want to go to the research subdirectory of your website at/local/www/html/research. Set your $CDPATH like this to include the parent directory:export CDPATH="/home/jkemp:/local/www/html/:$CDPATH"Now just type cd research to get to /local/www/html/research.However, with that setting, if you’re in ~/book/ and want to cd into the research-notes subdirectoryof that directory, you need to be wary. If you type research, hit Tab, and then hit Enter without payingattention, you’ll end up in /local/www/html/research by accident. In other words, $CDPATH overrides thedefault completion settings, which would otherwise just look in your current directory. To fix this, add(the current directory) to the start of the path:export CDPATH=".:/home/jkemp:/local/www/html/:$CDPATH"Now the current directory will be searched first, and then the other directories in the $CDPATH will besearched.176Download at WoweBook.Com

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

Saved successfully!

Ooh no, something went wrong!