This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new Linux.com!

Linux.com

Feature: Tools & Utilities

CLI Magic: A little script to customize directory listings

By Sergio Gonzalez Duran on December 11, 2007 (9:00:00 AM)

Share    Print    Comments   

Sometimes when I run ls to get a directory listing, I am looking for a specific file, but I want to see the whole context where the file resides. While you can pipe the output of ls to grep, that doesn't show you the whole directory with the matched files highlighted in a different color. I create a small script to do just what I want.

 For example, suppose that in /proc/sys/net/ipv4 I want to see files that have the pattern "max" as part of the name. I could run ls /proc/sys/net/ipv4 | grep max. grep changes the color to red where the pattern is found, but it doesn't display the files I want in the context of the entire directory listing. The following script does:

#!/bin/bash # name of the script 'l' just the letter l # a little bit of help in case of misuse if [ $# -lt 2 ] then echo "usage: l /directory pattern" echo "example: l /etc config" exit fi # $DIR is the first argument, the path to the directory DIR=$1 # $SEARCH is what you are looking for, and it's the second argument SEARCH=$2 # $RED sets the text to inverted red, changing the capabilities of the terminal via terminfo parameters # setaf 1 = red, smso = inverted RED=`tput setaf 1; tput smso` # $NORMAL returns text to normal attributes NORMAL=`tput sgr0` # ls -C $DIR | sed s/"$SEARCH"/"$RED$SEARCH$NORMAL"/

tput is a command that allows you to change the capabilities of the terminal, not just set the colors; it's based on the terminfo database. I use ls –C to put the directory listing in columns even if it's piped or redirected, so I can see more on a single screen. Then a simple sed substitution statement does the job; it finds whatever $SEARCH holds and substitutes for the same pattern with a change of color.

To make the script available to all users, copy it to /usr/bin and change the permissions (chmod 4755 /usr/bin/l). You can then run it like this:

l /etc conf

Customizing a directory listing may not be a very important issue, but thanks to the flexibility of Linux, you can increase your productivity with little tweaks.

Sergio Gonzalez Duran is a Linux administrator, systems developer, and network security counselor who also teaches Linux courses and publishes the Spanish-oriented Linux and open source Web site linuxtotal.com.mx.

Share    Print    Comments   

Comments

on CLI Magic: A little script to customize directory listings

Note: Comments are owned by the poster. We are not responsible for their content.

CLI Magic: A little script to customize directory listings

Posted by: Anonymous [ip: 62.161.100.113] on December 11, 2007 09:46 AM
What about:
ls -C $DIR | grep --color=auto
?

...
[Modified by: Anonymous on December 11, 2007 04:12 PM]

#

Re: CLI Magic: A little script to customize directory listings

Posted by: Anonymous [ip: 192.33.238.6] on December 11, 2007 03:36 PM
What about: ls -C $DIR | grep --color=auto ?
it doesn't display the files I want in the context of the entire directory listing
[Modified by: Anonymous on December 11, 2007 03:38 PM]

#

CLI Magic: A little script to customize directory listings- added support for wide terminal windows

Posted by: Anonymous [ip: 204.176.13.6] on December 11, 2007 03:13 PM
I often use very wide terminal windows. I found that your script did not use the full width of my window so I modified your last line as follows:

ls -C --width=$(tput cols) $DIR | sed s/"$SEARCH"/"$RED$SEARCH$NORMAL"/

#

CLI Magic: A little script to customize directory listings

Posted by: Anonymous [ip: 84.240.48.164] on December 11, 2007 03:20 PM
I was searching all over the web for something like "|grep --color=auto", but without the grep part. No luck.

So i wrote this:



#!/bin/sh

#

while read line

do

echo -e `echo $line | sed 's/'"$1"'/\\\E[31m'"&"'\\\E[37m/g'`

done



And saved it as /usr/local/bin/clr

Try to pipe something through it :)
[Modified by: Anonymous on December 11, 2007 03:24 PM]

#

CLI Magic: A little script to customize directory listings

Posted by: Anonymous [ip: 58.110.211.241] on December 11, 2007 04:53 PM
chmod 4755 ?

Why do you set it suid ?

Seems to work fine without that, and setuid root is not a good idea unless absolutely necessary...

I think chmod 755 is adequate. Also, it would be more usual to put things like this in /usr/local/bin , but that's a quibble.

#

CLI Magic: A little script to customize directory listings

Posted by: Anonymous [ip: 203.30.81.130] on December 12, 2007 12:22 AM
What about simple command line expansion:



ls /proc/sys/net/ipv4/*max*



There's no need to grep/sed to search for only files containing a certain string.

#

CLI Magic: A little script to customize directory listings

Posted by: Anonymous [ip: 192.0.1.127] on December 12, 2007 12:23 AM
This is nice, but not quite what I would like.
I'd like to be able to search with the -r (recursive) and see the file path as well.

#

CLI Magic: A little script to customize directory listings

Posted by: Anonymous [ip: 62.24.65.155] on December 12, 2007 08:56 AM
what about something like


ls -C $DIR --color=always | grep -A 1000 -B 1000 --color=auto $SEARCH
?

#

CLI Magic: A little script to customize directory listings

Posted by: Anonymous [ip: 196.14.186.131] on December 12, 2007 09:09 AM
I felt it more logical to reverse the arguments and default to the current directory if there is no second argument. I already use ''l' (ls -CF --color=auto'), so I call it 'lg' (ls grep) which is a good mnemonic what it does.

#

CLI Magic: A little script to customize directory listings

Posted by: Anonymous [ip: 196.14.186.131] on December 12, 2007 09:12 AM
The regular expression on the last line needs to use 'g' option to match more than one string per line!

#

Re: CLI Magic: A little script to customize directory listings

Posted by: Anonymous [ip: 189.166.254.187] on December 12, 2007 02:47 PM
yes, with the 'g' option works even better, nice job with this script, is really useful and is one of those things that I didnt think before. It should be added as a regular option to the ls command.

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya