Search code examples
bashcygwinls

Display Only Files And Dotfiles In The Default "ls" Command Format?


EDIT 2: For those with the same question, please be sure to read the comments on the accepted answer, as they go more in depth about how to properly use the command. I will post the final script once it is done with my own explanation of what is going on.

I'm trying to write a simple bash function that clears the terminal screen, then performs multiple ls commands to show the different content types of the current directory in different colors.

I've managed to write something in Python which kind of does the same thing, but it has some big drawbacks (specifically, the behavior for special characters in filenames is "iffy" on Cygwin, and it's a pain to make it fit properly on the screen). I want it to look something like this:

Python Example

*With non-dotfiles in green (haven't include them in the screenshot for privacy reasons).

I've managed to list both hidden directories and visible directories, but the files are giving me trouble. Every method of using ls to get all files I've tried uses the -l argument, or something like find or grep, all of which output the files in a single column (not what I want).

Is it possible to use the ls command (or something else) to output only the files or dotfiles of a directory while keeping ls's default output format?

EDIT 1: Here's what the script currently looks like (not much yet, but some people want to see it)

function test() {
    clear;

    GOLD=229;
    RED=203;
    BLUE=39;
    WHITE=15;
    GREEN=121;

    # Colored legend.
    tput sgr0;
    tput setaf bold
    # echo's "-n" suppresses the new line at the end.
    tput setaf $GOLD;
    echo -n "Working Directory ";
    tput setaf $RED;
    echo -n "Hidden Directories ";
    tput setaf $BLUE;
    echo -n "Visible Directories ";
    tput setaf $WHITE;
    echo -n "Hidden Files ";
    tput setaf $GREEN;
    echo "Visible Files";

    pwd;

    ls -d .*/; # List hidden directories.
    ls -d */; # List visible directories.
    # List dotfiles.
    # List files.
}

Solution

  • to list only the dot files in the current directory

    find .  -maxdepth 1 -type f -name ".*" | xargs ls --color=tty
    

    to list only the other files

    find .  -maxdepth 1 -type f -not -name ".*" | xargs ls --color=tty