Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if a directory from "ls" is a symlink?

Tags:

I am using Mac OS X terminal. How can I tell if a directory returned by "ls" is a symlink or the actual directory? If it is a symlink, how can I inspect where it is linking to, or modify it?

I actually tried to research this one a fair amount, but everything I have found is about creating symlinks. The closest I've come is being able to set colors for several things in my terminal. I'm assuming there is an actual command for getting info about a directory or file.

like image 583
Don P Avatar asked Feb 27 '13 03:02

Don P


People also ask

How do you check if a directory is a symbolic link bash?

Your Bash script might need to determine if a file is a symlink or not. In Bash you can test this with the -L operator that returns true if the file exists and is a symlink.

How do you check if a symbolic link exists in Linux?

If you are working in a tcsh (often installed as csh on Linux), then if ( -l "$link") should work. The -l operator checks if a file is a symbolic link.

Can a directory be a symlink?

Symlink, also known as a symbolic link in Linux, creates a link to a file or a directory for easier access. To put it in another way, symlinks are links that points to another file or folder in your system, quite similar to the shortcuts in Windows.


2 Answers

Use ls -l

Example output:

-rwxr-xr-x 1 foo staff  642 Nov 22  2010 getCactiImages.sh lrwxr-xr-x 1 foo staff   36 Aug 29 15:29 imgopt -> ../Projects/imgopt/imgopt 

imgopt is a symlink, getCactiImages.sh is a normal file

You can also use stat filename

Example:

  File: ‘imgopt’ -> ‘../Projects/imgopt/imgopt’   Size: 36          Blocks: 8          IO Block: 4096   symbolic link Device: 1000005h/16777221d  Inode: 7743835     Links: 1 Access: (0755/lrwxr-xr-x)  Uid: (  501/  foo)   Gid: (   20/   staff) Access: 2012-08-29 15:29:19.000000000 -0700 Modify: 2012-08-29 15:29:19.000000000 -0700 Change: 2012-08-29 15:29:19.000000000 -0700  Birth: 2012-08-29 15:29:19.000000000 -0700 
like image 92
R. S. Avatar answered Oct 25 '22 10:10

R. S.


ls -al will tell you. Symlinks will be noted like: target -> source

like image 35
prodigitalson Avatar answered Oct 25 '22 09:10

prodigitalson