Hi there,
I want to check whether a file is a link or not.
I have a directory structure as follows:

mkdir -p abc/bcd/cde/def
touch abc/bcd/cde/def/file
cd abc/
ln -s bcd link

Now, abc/link/cde/def/file is actually not a file. Its a link

Now I want to remove this file entry while listing the files in the directory "abc".

Is there any command/script that says "abc/link/cde/def/file" file is a link.

I need this because, I have a huge list of files and in that list, I have this kind of links as well. I need to filter out the links.
Please help me.
Thanks in advance.

Recommended Answers

All 5 Replies


test -L file && echo "file is a link"

I have tried this earlier as well.
No output:

test $ test -L abc/link/cde/def/file && echo "file is link"
test $

$ >myFile
$ ln -s myFile myLink
$ test -L myLink && echo "myLink is a link"
myLink is a link


Thats work with the direct link.
But If I created a link for directory and I want to check for the file in that directory.

like in my example,
abc/link/cde/def/file , link is the symbolic link. In that case, I need a script pr a command that says abc/link/cde/def/file is a link not a regular file.

even the system doesn't see such files as links

$ mkdir directory
$ >directory/file
$ ln -s directory linkdir
$ >linkdir/fileinlink
$ ls -l linkdir/*
-rw-r--r-- 1 watael watael 0  7 nov.  15:28 linkdir/file
-rw-r--r-- 1 watael watael 0  7 nov.  15:29 linkdir/fileinlink
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.