Sunday, September 5, 2010

Command line might: useful aliases; BASH tips

I'm a command-line-guy I guess. I use a terminal quite often (mostly with BASH), either for file management, or while working on my projects, just to launch proper application, and optionally to make it read some contents from a given file.

I found myself using quite often two particular functions:
  1. searching for a file which name contains a keyword, and
  2. figuring out what files I recently modified in the current directory.
These two functions can be easily written down as:

ls -l | grep -i some_keyword
ls -lt | head

Right, but since I use them quite often and being rather lazy I wouldn't want to type these every time... So here come aliases.
You can define an alias for a command you often type and use it instead.  So I decided to define two aliases: lsg and lst for searching for a file name containing a keyword and displaying the recently modified files respectively.

So if you type at your command prompt:

alias lsg='ls -l | grep -i'
alias lst='ls -lt | head'

From now one you can use lsg and lst freely.

If you want to make the change permanent you could add the above lines to your ~/.bashrc file, so the aliases would be accessible any time you log in.

Oh one more thing... how to use it.
If I want to find out what files I modified recently in the current directory I just type:

$ lst

and it gives me a list of recntly modified files. If I'm looking for a file which name contains a keyword "book" I issue:

$ lsg book

And I get a list of files which names contain "book" (case insensitive). It's quite useful - at least for me.

PS. Dont forget about: cd -
It will bring you up to the directory you were previously in.

No comments:

Post a Comment