---
title: "Reading logs made easy with bash"
description: "Most of the web programmers would normally look into their applications/server error logs when they find issues, indeed."
url: "https://h3manth.com/scribe/reading-logs-made-easy-with-bash/"
canonical_url: "https://h3manth.com/scribe/reading-logs-made-easy-with-bash/"
markdown_url: "https://h3manth.com/scribe/blog/reading-logs-made-easy-with-bash.md"
publishDate: "2012-06-07T00:00:00.000Z"
author: "Hemanth HM"
category: "cli"
tags: ["bash","cli"]
word_count: 218
reading_time_minutes: 2
x_markdown_tokens: 390
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# Reading logs made easy with bash

> Most of the web programmers would normally look into their applications/server error logs when they find issues, indeed.

Most of the web programmers would normally look into their applications/server error logs when they find issues. Indeed it's a pain when the log path is filled with directories - it's sometimes annoying to do an `ls -l` and look for the latest directory and then `cd` it. A bit better case is when one tab-completes to change directories.

It would be very easy if we could **cdn** as in cd to the new dir ;)

So I tried some silly script that would make my life easier - do add it to your `~/.bashrc` if you like it!

**Here is the simple script to cdn:**

```bash
cdn() { 
    local file latest; 
    for file in "${1:-.}"/*; do 
        [[ $file -nt $latest ]] && latest=$file
    done; 
    printf '%s\n' "$latest"
    [[ -d $latest ]] && cd $latest || tailf $latest
}
```

**Usage:** `cdn [dir]`

**Digging into the code:**

* Get all the files and dirs in the specified path or consider "." as the default path: `"${1:-.}"/*`
* Using file and latest local vars: if `$file -nt $latest` is `True`, then $file is newer than $latest (according to modification date).
* Finally check if it's a dir or file - if dir cd to it, else tail it!

**Hope this helps you guys, as it's helping me. Happy hacking!**
