Last Updated: January 28, 2019
· saml

Change directory, list files and print current directory path in one command

If you put this bash function in your .bashrc or .bash_aliases file in your home folder (provided you use a unix like operating system like linux or Mac OS):

c() {
    cd $1; 
    ls -l;
    echo " "

... then you can change directory, list the current files in the directory, and see in which directory you are, just by using "c" instead of "cd" when you change folder in the terminal!

For example, if I want to change directory from my home folder (denoted by ~ in the terminal) to one of my Go code projects, it looks like this:

[samuel ~]$ c code/go/src/basecompl_blow/
total 118M
-rwxrwxr-x 1 samuel samuel 1,9M nov  7 18:47 basecompl_blow
-rw-rw-r-- 1 samuel samuel 2,0K aug 12 15:51 basecompl_blow.go
-rw-rw-r-- 1 samuel samuel  12K aug 11 02:13 bc1.log
-rw-rw-r-- 1 samuel samuel   24 aug 11 02:33 breakpoints.txt
-rw-rw-r-- 1 samuel samuel  66K aug 12 15:14 dbg.txt
lrwxrwxrwx 1 samuel samuel   50 nov  7 18:46 Homo_sapiens.GRCh37.67.dna_rm.chromosome.Y.fa -> Homo_sapiens.GRCh37.67.dna_rm.chromosome.Y.fa.orig
-rw-rw-r-- 1 samuel samuel  58M aug  8 15:36 Homo_sapiens.GRCh37.67.dna_rm.chromosome.Y.fa.orig
-rw-rw-r-- 1 samuel samuel 7,8K aug  8 15:36 Homo_sapiens.GRCh37.67.dna_rm.chromosome.Y.fa.short
-rw-rw-r-- 1 samuel samuel 8,1K aug 11 01:50 Homo_sapiens.GRCh37.67.dna_rm.chromosome.Y.fa.short.linenum
-rw-rw-r-- 1 samuel samuel  58M nov  7 18:47 out.txt

[samuel basecompl_blow]$ 

I find that this saves tons of keystrokes, since 90% of the commands I type are cd, ll and often I would like to see the output of pwd as well.

With this c-command, I can navigate around and see the context of where I am, more like in a typical graphical file browser, while still using the power of the terminal.

1 Response
Add your response


A few advices:
- remove the « ; » at the end of the lines: they are redundant with NL
- add quotes around « $1 »: cd "$1"
- remove « " " » after echo: just « echo » is enough to print an empty line

over 1 year ago ·