0

The working directories contains several subdirectories, and each subdirectory contains some hidden files that start with ..

How could I use the ls command to display all the files including the hidden file, but I want to exclude the files name as . and ..?

I tried ls -a | grep -v '^\.' but it does not show files in the subdirectories. And the option -ignore does not work, too.

0

2 Answers 2

1

If you want ls to show the sub-directories, you need to add the -R.

Your grep -v '^\.' will remove everything that starts with a ., so including the hidden files. A better pattern would be to explicitly remove just the . and .. with '^\.$|^\.\.$'. The more specific you make the patterns, the less problems you have that unwanted lines are removed.

Putting that all together gives:

 ls -aR  | egrep -v '^\.$|^\.\.$'

Note that, if it is just the implied . and .. that you want to remove, you can also use ls -AR. From man ls:

  -a, --all
         do not ignore entries starting with .

  -A, --almost-all
         do not list implied . and ..
3
  • Hi, thanks for your help. But is there any other way that not using the recursive option, cause the question also ask to not go through the third level subdirectories Commented Sep 19, 2022 at 12:29
  • I think, that instead of "how could I use the ls command" , you will want to use find . -maxdepth 2, possibly with -type f , so find instead of ls. Commented Sep 19, 2022 at 12:54
  • Thanks for your advice, find command is much more convenient, but this question explicitly says to us ls command;) Commented Sep 19, 2022 at 13:40
0

Can’t test this right now, but fairly certain this will work. Wasn’t clear if you wanted to list the non-directory files in the working dir, so skipped my those.

ls -d */* */.??*

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .