Search code examples
awkls

Printing hidden files and directories using awk


I have the following command:

ls -l /tmp | awk '$3=="'$USER'" {print $NF}'

which is supposed to print the name of files and directories under /tmp which belongs to the $USER. It works fine for the normal files and directories but the output doesn't include hidden files and directories which belong to the $USER.

What is needed to fix it?


Solution

  • Correct way to do this:

    ls -al /tmp | awk '$3==u {print $NF}' u="$USER"
    

    or

    ls -al /tmp | awk -v u="$USER" '$3==u {print $NF}'