ls content of a directory ignoring symlinks
For the stated question you can use find
:
find . -mindepth 1 ! -type l
will list all files and directories in the current directory or any subdirectories that are not symlinks.
mindepth 1
is just to skip the .
current-directory entry. The meat of it is the combination of -type l
, which means "is a symbolic link", and !
, which means negate the following test. In combination they match every file that is not a symlink. This lists all files and directories recursively, but no symlinks.
If you just want regular files (and not directories):
find . -type f
To include only the direct children of this directory, and not all others recursively:
find . -mindepth 1 -maxdepth 1
You can combine those (and other) tests together to get the list of files you want.
To execute a particular grep
on every file matching the tests you're using, use -exec
:
find . -type f -exec grep -H 'some pattern' '{}' +
The '{}'
will be replaced with the files. The +
is necessary to tell find
your command is done. The option -H
forces grep to display a file name even if it happens to run with a single matching file.
Or, simpler:
ls -l | grep -v ^l
Explanation
ls -l
means to list in long form. When you do this, the first string of characters gives information about each file. The first character indicates what type each file is. If it's a symlink, then an l
is the first character.
grep -v ^l
means to filter out (-v
) the lines that start with (^
) an l
.
From version 2.12 onwards, the -r
option for GNU grep doesn’t dereference symbolic links unless you specify them by hand:
-r, --recursive
Read all files under each directory, recursively, following symbolic links only if they are on the command line. This is equivalent to the -d recurse option.
-R, --dereference-recursive
Read all files under each directory, recursively. Follow all symbolic links, unlike -r.