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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 9 ■ WORKING WITH TEXT IN FILES■ Note Many modern <strong>Linux</strong> systems substitute less for more automatically. In other words, more is eithersymlinked to the less binary, or the less binary is used directly.So, less is great for paging through text files and very flexible, but it won’t automatically deal withcompressed (zipped or gzipped) text files unless your system has already made the changes I suggesthere or something similar, which is the case in recent versions of Ubuntu. This can be a nuisance,particularly when many of the files in /usr/share/doc are gzipped. It’s a nuisance to have to run gunzipfirst, especially because this then leads to half the files being uncompressed and half still compressed.A straightforward alternative is zless, which will deal seamlessly with gzip, compress, or pack files,allowing you to page through them without having to unzip them:zless file.gzIt also handles uncompressed files, and contrary to what’s said on the man page, it appears to dealOK with input piped from stdin:ls | zlesszless can’t, however, handle files that have been tarred as well as zipped (file.tar.gz). Thiscommand will allow you to page through file.tar.gz without unpacking it beforehand and thuswithout leaving unzipped files lying around:tar --to-stdout -zxf file.tar.gz | lessHowever, that’s a bit of a mouthful (or keyboard full) to remember. Try this instead:export LESSOPEN="|tar --to-stdout -zxf %s"Now try this:less file.tar.gzYou should be able to page through your file without any problems. In fact, this will deal with bothuncompressed files and piped input, so you can set this variable in your .bashrc file. However, it won’twork on plain .gz files. For those, you should still use zless. (Since this doesn’t pick up the LESSOPENenvironment variable, it will work as normal.)If you regularly want to look at tar archives that aren’t also zipped (for example, file.tar), you canset this to work the same trick for these files:export LESSOPEN="|tar --to-stdout -xf %s"Unfortunately, you can’t have both variables set at the same time! To manage all the various sorts offiles, you can, however, define a set of functions in your ~/.bashrc:function tgless() { tar --to-stdout -zxf $1 | less; }function tless() { tar --to-stdout -xf $1 | less; }186Download at WoweBook.Com

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

Saved successfully!

Ooh no, something went wrong!