Results 1 to 9 of 9

Thread: [SOLVED]cat shows directory names alongside file names

  1. #1
    Join Date
    Apr 2019
    Beans
    4

    [SOLVED]cat shows directory names alongside file names

    Hi,
    When I want to see the content of a file using cat command after typing cat and pressing TAB key then the directory names appear beside the file name in the current path. Is it possible showing only file names when pressing TAB in this case? Logically, cat is only be able to work with files not directories.
    Last edited by eminent2; April 8th, 2020 at 11:00 AM.

  2. #2
    Join Date
    Feb 2008
    Location
    Brazil
    Beans
    77

    Re: cat shows directory names alongside file names

    If I understood correctly, you want cat to only display the file names in the current path you are?

    If so, I guess that would be up to cat + bash-completion: it shows both directory names and files because you might want to choose a file inside a directory in the current path.

  3. #3
    Join Date
    Apr 2019
    Beans
    4

    Re: cat shows directory names alongside file names

    Quote Originally Posted by owbizi View Post
    If I understood correctly, you want cat to only display the file names in the current path you are?
    Exactly, How can I achieve this?

  4. #4
    Join Date
    Feb 2008
    Location
    Brazil
    Beans
    77

    Re: cat shows directory names alongside file names

    My guess is that you would have to compile your own version of cat and use that binary instead of the system provided.

    As it is now, it is included in the GNU coreutils among other tools commonly available as binaries from the get-go in Linux systems.

  5. #5
    Join Date
    Jan 2017
    Beans
    235

    Re: cat shows directory names alongside file names

    You can achieve what you want by modifying the bash-completion for the cat command.

    Here's a bash function that does what you want:
    Code:
    $ cat cat_no_dir
    
    _cat_no_dirs ()
    {
        COMPREPLY=()
        cur=($(compgen -o plusdirs -f -- "${COMP_WORDS[COMP_CWORD]}"))
        for ((i=0; i < ${#cur[@]}; i++)); do
            [ -d "${cur[$i]}" ] && continue
            COMPREPLY+=(${cur[$i]})   
        done
        return 0
    }
    
    
    complete -F _cat_no_dirs cat
    A test:
    Code:
    $ ls -lgG
    -rw-r--r-- 1  377 Apr  6 18:19 cat_no_dir
    drwxr-xr-x 2 4096 Apr  6 18:32 dir_1
    drwxr-xr-x 2 4096 Apr  6 18:32 dir_2
    drwxr-xr-x 2 4096 Apr  6 18:32 dir_3
    -rw-r--r-- 1    0 Apr  6 18:32 file_1
    -rw-r--r-- 1    0 Apr  6 18:32 file_2
    -rw-r--r-- 1    0 Apr  6 18:32 file_3
    
    
    $ source cat_no_dir
    
    
    $ cat    # tab tab
    cat_no_dir  file_1      file_2      file_3
    If you want this action to be permanent copy the file containing the function (cat_no_dir) to "/etc/bash_completion.d" and it will be sourced when you start a shell. For occasional use just source the file as I did here.

  6. #6
    Join Date
    Apr 2020
    Beans
    70

    Re: cat shows directory names alongside file names

    Where's the LIKE button when you need it?? Nice solution @norobro

  7. #7
    Join Date
    Apr 2019
    Beans
    4

    Re: cat shows directory names alongside file names

    You are my champion !!! I love you ������ Thank you very much bro ��������

  8. #8
    Join Date
    Apr 2019
    Beans
    4

    Re: [SOLVED]cat shows directory names alongside file names

    @norobro
    Brilliant. I got it working.

  9. #9
    Join Date
    May 2013
    Location
    Galiza
    Beans
    4,009
    Distro
    Ubuntu

    Re: cat shows directory names alongside file names

    Quote Originally Posted by kneutron View Post
    Where's the LIKE button when you need it?? Nice solution @norobro
    There's "Solved" in the thread tools to indicate just that.
    Last edited by CelticWarrior; April 8th, 2020 at 02:27 PM.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •