Thursday, June 2, 2022

[SOLVED] How to grep all files beside current dir, parent dir and one definded?

Issue

I have a folder with the following files / folders:

.test
README.md
/dist
/src

I want to grep all files beside dist. So the result should look like:

.test
README.md
/src

When I do

ls -a | grep -v dist

it will remove dist. But . and .. are present. However I require the -a to get files with dot prefix.

When I try to add ls -a | grep -v -e dist -e . -e .. there is no output.

Why will -e . remove all files? How to do it?


Solution

Better to use find with -not option instead of error prone ls | grep:

find . -maxdepth 1 -mindepth 1 -not -name dist

btw just for resolving your attempt, correct ls | grep would be:

ls -a | grep -Ev '^(dist|\.\.?)$'


Answered By - anubhava
Answer Checked By - Mary Flores (WPSolving Volunteer)