taT4Nix | Read File Without Changing Directories..

These days, I am running few commands to see some frequently generated reports on Unix server, which follow some pattern. Then, it becomes tedious for me to change directory (cd) and read the contents of the file (cat), everytime.

Latest Updates

Replace “ls -ltr” with “ls -tr” and remove last command..
ls -tr <some_pattern> | tail -1

Trick 2 Read..

So, getting frustrated by doing this myself, I came up with a trick to fasten things up. Here we go;

cat `ls -ltr <dir_path>*<file_pattern>* | tail -1 | cut -d' ' -f11`

Assuming you want to read the latest report whose filename has pattern <file_pattern> and stored under directory <dir_path>. Generally, these kind of files might have some standard prefix and/or suffix mechanisms as well. So, I keep an asterix before and after <file_pattern> to get right set of matching files.

So, create a script which includes the above command and have parameters supplied through command line. Suppose, name the file read_file_wo_cd.ksh

cat `ls -ltr $1*$2* | tail -1 | cut -d' ' -f11`

$1 – Directory Path,  $2 – File Pattern

Now, you can run like this;

$ read_file_wo_cd.ksh /some/path/to/file file_pattern

Tips 2 Remember..

By the way, if you want to know the latest name of the file that has got updated/created.
Just run the following in any directory, preferably having frequent updates;

echo `ls -ltr | tail -1 | cut -d' ' -f11`

Try this and you are gonna enjoy..

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s