Friday, August 8, 2014

Change Directory and list contents in single command.

I find myself often changing to a directory and immediately performing an `ls`. This is partly habit and partly the fluid nature nature of the files I work on.  Anyway, you don't care about that...

Using an editor of your choosing open your .bashrc or .bash_profile and add the following lines:
# User defined functions
function cdl { cd $1 && echo $PWD && ll; }
Note: In the above I have aliased `ll` as ->  alias ll='ls -ASlh'
Note: You can name this whatever you choose, I just think `cdl` made sense.
Note: You can omit the echo $PWD if you don't care to see the directory path in the output. So you could do something as simple as:
# User defined functions
function cdl { cd $1 && ls; }
Save and close and then
bkarels@rev0:~$ source ~/.bashrc
Now you can use cdl (or whatever you've named it to change directory and immediately ls the contents.

Example:
bkarels@rev0:~$ cdl foo/
/home/bkarels/foo
total 328K
-rw-------   1 bkarels bkarels 156K Aug  8 09:39 sample0
-rw-------   1 bkarels bkarels 6.9K Aug  8 08:47 someFile27
-rw-r--r--   1 bkarels bkarels 4.1K Aug  7 12:07 sayWhat
drwx------   6 bkarels bkarels 4.0K Aug  8 07:37 killWindows.exe
The above example was done on Debian 7 (Wheezy) but should work on all flavors of *nix and OSX.

FIN

No comments:

Post a Comment