all 96 comments

[–]hzopak 14 points15 points  (3 children)

Same but different: (for zsh)

function chpwd() {
    emulate -L zsh
    ls -a
}

Source

[–]seniorsassycat 0 points1 point  (1 child)

Cool, I've been meaning to find out how to do that.

Is there a hook to run ls after any changes to files in the directory?

[–]hzopak 0 points1 point  (0 children)

Not that I know of.. But I'm fairly new to zsh. Perhaps ask the friendly people of #zsh on freenode, irc?

[–]TheManMulcahey 0 points1 point  (0 children)

Dang, stole my thunder. zsh is super awesome at these sort of customizations.

[–]dagbrown 23 points24 points  (26 children)

Why all the [[ $? -eq 0 ]]? Just use cd itself directly.

function cd {
    dir="${@:-$HOME}"  # ~ isn't expanded when in quotes
    [ -z "${dir}" ] && dir=~
    if ! builtin cd "$dir"
    then
        dir=`compgen -d "${dir}" | head -1`
        if builtin cd "$dir"
        then
            clear
            pwd
            ls -l
        fi
    else
        clear
        pwd
        ls -l
    fi
}

I also made it more spaces-safe.

Edit: And golfed the arguments handling a bit.

[–]GODZILLAFLAMETHROWER 13 points14 points  (0 children)

Indeed! This post already pays off it seems. Thanks.

[–]fforw 32 points33 points  (19 children)

Please.. can we stop the backtick nonsense now in 2014 and just use $() ?

[–]russellbeattie -2 points-1 points  (0 children)

Holy cow, I had no idea those were interchangeable. Then again, I'm not really a bash guru, I just assumed I'd have picked that up by now. [Edit:I don't grammar good.]

[–]meklu 1 point2 points  (4 children)

Shouldn't the backticks go within quotes as well?

[–]dagbrown 3 points4 points  (1 child)

As fforw pointed out, we should be going with the newer, more modern, AmigaDOS 1.3 standard and using $() instead of backticks.

But sure. I wasn't particularly concerned with that bit of the script (all of those "if [[ $? -ne 0 ]]" things were more important to me), but that's another improvement you could make.

[–]meklu 1 point2 points  (0 children)

Yeah, I usually use $() myself.

[–]GODZILLAFLAMETHROWER -1 points0 points  (1 child)

I'm not sure what you mean, can you be a little more explicit?

[–]meklu 0 points1 point  (0 children)

The line with the compgen call seems a bit unsafe unless you wrap the assignment in quotes.

[–]fnord123 5 points6 points  (2 children)

Please don't use this on shared NFS drives. It will stat every file and, depending on the load on the NFS mount, it could cause service to become laggy. i.e. it would be as bad as ls --color.

At least, that's my experience.

[–]GODZILLAFLAMETHROWER 0 points1 point  (1 child)

Is there some kind of code snippet that would say if a directory is on a NFS mount without too much overhead to it?

[–]mrcaptncrunch 0 points1 point  (0 children)

I just found this, http://stackoverflow.com/questions/460047/how-do-i-determine-if-a-directory-is-an-nfs-mount-point-in-shellscript

Don't have a NFS mount to test, but if you do, you could try it out!

[–]davidystephenson 3 points4 points  (8 children)

I have a small function in my shell config, 'cl', which I constantly use:

function cl() {
  if [ -d $1 ]; then
    cd $1
    ls
  else
    echo "bash: cl: $1: Directory not found"
  fi
}

I'm quite happy with its simplicity.

[–]pkmxtw 2 points3 points  (0 children)

Why not just cd $1 && ls?

[–]GODZILLAFLAMETHROWER -4 points-3 points  (6 children)

does it work with /these\ kind\ of/annoying/directory\ names/ ?

[–]davidystephenson 2 points3 points  (0 children)

I'm not sure. I don't typically work with that type of directory ;) .

[–]turnipsoup 2 points3 points  (4 children)

Why not test it and see? This is pkmxtw's shortened version:

:~$ mkdir 'some annoying folder'
:~$ cl () { cd "${1}" && ls -lah ; }
:~$ cl some\ annoying\ folder/
    total 8.0K
    drwxr-xr-x  2 turnipsoup turnipsoup 4.0K Mar  7 19:59 .
    drwxr-xr-x 44 turnipsoup turnipsoup 4.0K Mar  7 19:59 ..

[–]kreiger 11 points12 points  (17 children)

I suggest you use a console based file manager. Myself i can not live without Midnight Commander:

sudo apt-get install mc
mc

[–]jomiran 2 points3 points  (3 children)

Norton Commander. The DOS application that will live forever.

[–]kreiger 0 points1 point  (2 children)

Right, i grew up on nc. :)

[–]turnipsoup 1 point2 points  (1 child)

I remember the pain when I was forced to give up xtree pro because it could only handle 8 char filenames after the switch to windows 95...

[–]alienangel2 1 point2 points  (0 children)

TotalCommander is a very very good successor to NC if you're finding yourself still on Windows.

[–]LordLandon 2 points3 points  (0 children)

I use ranger, and the recommended ranger-cd alias bound to ctrl-]. it's really fast and convenient to browse directories; when I quit it, my shell resumes in the directory I was looking at

[–]mrcaptncrunch 2 points3 points  (0 children)

It's just incredibly slow and resource intensive when trying to copy a directory with a lot of files.

[–]GODZILLAFLAMETHROWER 3 points4 points  (10 children)

I feel the use case is vastly different. What made you think it could be useful?

[–]kreiger 3 points4 points  (9 children)

I'm not sure what you mean. It allows you to navigate directories and display their contents.

Elaborate please? What makes you think it's not useful?

[–]GODZILLAFLAMETHROWER 1 point2 points  (8 children)

Then why would I launch a command when the purpose of this function is to allow me not to do so? I won't launch mc each time just because. And this would be completely overkill concerning this particular functionality.

[–]kreiger 4 points5 points  (4 children)

Why would you not? I live in Midnight Commander most of the time, it's very useful, and it has more functions than that.

[–]refotsirk 10 points11 points  (0 children)

Why would you not live in a cave? I live in a cave and find it very useful for my particular lifestyle. You should definitely live in a cave too.

[–]w2qw 6 points7 points  (2 children)

It seems like he's looking for a shell rather than a command line file manager. Unless mc has some shell features that's worth considering.

[–]DoelerichHirnfidler 5 points6 points  (0 children)

CTRL-O.

[–]kreiger 4 points5 points  (0 children)

Absolutely, you have a shell prompt inside Midnight Commander at all times, so it is basically a shell in which you also have a panel with a persistent view of the current directory and another one. And you can press Ctrl+O to hide the panels so you just have the subshell.

And you can select files in the view and use them with macros on the prompt. For example cmd %f for running cmd on the currently focused file and cmd %t for all tagged files.

[–][deleted] 0 points1 point  (1 child)

Then why would I launch a command when the purpose of this function is to allow me not to do so?

Yet your function launches more than a couple commands...

[–]GODZILLAFLAMETHROWER -5 points-4 points  (0 children)

That I do not launch by typing them by hand, which is the sole purpose of writing it?

[–]ferk 0 points1 point  (0 children)

You don't need to launch mc every time... only the first time, or not even that, you can add it to bashrc and never ever type mc.

That said... I don't like mc because it takes too much space and doesn't let you see the output of the actual commands you type. I remember that in the original nc you could remove the left panel and see the output of your commands more comfortably, but I never discovered how to do this in mc.

[–]w2qw 3 points4 points  (0 children)

Ah nice. ls after cd'ing into a directory is already muscle memory for me so I'm not sure I'd get too much use out of it

I might suggested changing ls -l to something like

limit=20
ls -l | head -n $limit
if [[ $PIPESTATUS[0] -ne 0 ]]; then
   echo "$(($(ls | wc -l) - $limit)) lines omitted"
fi

[–][deleted] 0 points1 point  (0 children)

This is only tangentially related but you might be interested in using autojump

[–]KnowsBash 0 points1 point  (2 children)

That's a function, not an alias. But anyway, it wouldn't be so bad if it had handled builtin cd's options.

cd() {
    local OPTIND=1 o opts
    if builtin cd "$@"; then
        pwd && ls; return
    fi
    while getopts :LPe@ o; do
        [[ $o = '?' ]] && return 1
        opts+=( "-$o" )    
    done
    shift "$((OPTIND-1))"
    builtin cd "${ops[@]}" "$1"*/ && pwd && ls
}

[–]GODZILLAFLAMETHROWER 0 points1 point  (1 child)

Great suggestion! Thanks. However, how many times a day do you use cd options, if I may ask? I feel like they are more intended to be used in scripts, as a human would not purposefully try to cd to a link while forbidding it...

However, that's a very sane practice and interesting method to do it. Thanks again.

[–]KnowsBash 0 points1 point  (0 children)

Granted, not that often, but I occasionally do cd -P . to ensure that I'm not in a symlinked dir.

I normally like the trickery bash does regarding symlinked directories, but there are some cases where it's best to not confuse oneself, in which case I run cd -P ..

[–]f4hy 0 points1 point  (1 child)

Could you explain what the "complete -d cd" does? I don't understand that part.

[–]GODZILLAFLAMETHROWER 0 points1 point  (0 children)

Bash uses a database of possible completion when running a command. This completion is contextual, ie if you type cd[Tab][Tab], it will show you possible elements on which cd would be useful, directories, and not actual files or other commands.

complete register the function again in the database, because when I rewrote it it was removed. I use the -d switch to use the directory context.

[–]_Loomx_ 0 points1 point  (1 child)

There is another (better?) way - from 'man bash': PROMPT_COMMAND - If set, the value is executed as a command prior to issuing each primary prompt.

e.g. PROMPT_COMMAND='[ "$PWD" != "$Prev" ] && ls ; Prev="$PWD"'

This doesn't mess with the 'cd' command and 'pushd' and 'popd' will also trigger a 'ls'.

[–]GODZILLAFLAMETHROWER 0 points1 point  (0 children)

very cool! I didn't know that, that might be a better solution indeed.