When I attempt to tab complete a filename in the bash prompt, and the output would be many (>100) filenames, the shell asks Display all <num> possibilities? (y or n)Can I replicate this behaviour with the normal ls command?I have a directory with >400k files. Accidentally lsing this directory leaves me with an unresponsive shell for many minutes while it spews output. Ctrl-C doesn't stop the flood.
Display all  possibilities? (y or n) for ls command
ls
My solution might be a little heavy but well, who knows.The problem is that, in order to do that, you would need to count files... which is usually done with:$ ls | wc -lNow, because the content is written to a pipe, and not to stdout, this will take a little less time to complete (writing to a terminal takes a little bit of time, which can become significant with 400k calls to write(2)). It might still take some time, but nevertheless, you could implement this with a simple test:#!/bin/bashLIMIT=1000n=$(ls $@ 2>/dev/null | head -n$LIMIT | wc -l)if [ $n -ge $LIMIT ]; then    echo This directory contains more than $LIMIT entries.    echo -n Are you sure want to continue? (y/N)     read confirm    if [ $confirm = y -o $confirm = Y ]; then        exec ls $@    fielse    exec ls $@fiThen, just set an alias in your .bashrc file, to call this script instead of /bin/ls:alias ls='/path/to/my_ls.sh --color=auto'Also make sure it is properly chmoded:$ chmod +x /path/to/my_ls.shEdit: I added a call to head so that ls is terminated after $LIMIT lines of output. This should save some time (and actually make it a better solution that the timeout one) ;)Another approach I would suggest is to use a timeout. If ls doesn't complete in say 5s, it should stop and ask you if you're sure about it. Here's a example script (valid for several commands, not just ls) :#!/bin/bash                                                                                                                                                                                TIMEOUT=5if [ $# -le 0 ]; then    echo Usage: $0 [command]    exit 1fi# Try it silently for a few seconds...timeout $TIMEOUT $@ >/dev/null 2>&1if [ $? -eq 124 ]; then    echo -n Your command failed to run under $TIMEOUTs.     echo -n Retry without a timeout? (y/N)     read confirm    if [ $confirm = y -o $confirm = Y ]; then        exec $@    fielse    exec $@fiThen you could set a few aliases, such as:alias ls='/path/to/script.sh ls --color=auto'alias grep='/path/to/script.sh grep --color=auto'alias cat='/path/to/script.sh cat'Note that I'm testing $? -eq 124 because...If the command times out, and --preserve-status is not set, then exit with status 124.See timeout(1) for more information.