Linux Commands Examples

A great documentation place for Linux commands


make links between files

see also : link


ln [OPTION]... [-T] TARGET LINK_NAME (1st form)
[OPTION]... TARGET (2nd form)
[OPTION]... TARGET... DIRECTORY (3rd form)
[OPTION]... -t DIRECTORY TARGET... (4th form)

add an example, a script, a trick and tips

: email address (won't be displayed)
: name

Step 2

Thanks for this example ! - It will be moderated and published shortly.

Feel free to post other examples
Oops ! There is a tiny cockup. A damn 404 cockup. Please contact the loosy team who maintains and develops this wonderful site by clicking in the mighty feedback button on the side of the page. Say what happened. Thanks!


ln -s ../../net
ln -s ../../loax
ln .bashrc ~/.bashrc
ln -s NESpad ../NESpad
ln -s ../../texgz
ln -s ../../net
ln -s ../node_modules .

What is "ln -L" (--logical) for?

GNU utilities are primarily documented with info pages. From the GNU ln info page:

    If -s is not in effect, and the source file is a symbolic link,
    create the hard link to the file referred to by the symbolic link,
    rather than the symbolic link itself. 

So this simply dereferences symbolic links given as source arguments.


Does ln -s require you to be in a certain directory?

If you are not in the same directory as the target you must make sure you use the absolute path.

ln -s foo/bar baz/quux # fails

ln -s ~/foo/bar baz/quux # succeeds

Does the target in ln -s have to be a full path?

ln can take either absolute or relative pathnames; the difference matters for symbolic links, but since you asked to make a hard link, it will resolve to the same file either way.

Check the ls -li output on ~/abc and ~/download/abc -- you'll see that the link count is 2 and the inode number is the same for both files.


How would you link a number of files (e.g. *.so) by one-liner in Linux?

via shell-builtins (and globoperator)

from l in /from/*.so; do ln -s "$l"; done

via find

find /from/ -name "*.so" -exec ln -s '{}' ';'

doing it via find is better since it works better for huge amount of files.


Why doesn't ln -s prompt that it fails when creating a symlink to an existing symlinked directory?

It does not actually fail. It creates your link inside the given directory:

% mkdir dir_1 dir_2
% ln -s dir_1 symlink_dir
% ln -s dir_2 symlink_dir
% ls -l
total 0
drwxr-xr-x 2 user group 60 Oct 16 12:47 dir_1
drwxr-xr-x 2 user group 40 Oct 16 12:47 dir_2
lrwxrwxrwx 1 user group  5 Oct 16 12:47 symlink_dir -> dir_1
% ls -l dir_1
total 0
lrwxrwxrwx 1 user group  5 Oct 16 12:47 dir_2 -> dir_2

This behaviour is described in the manpage:

 ln [OPTION]... TARGET... DIRECTORY     (3rd form)
 In the 3rd and 4th forms, create links to each TARGET in DIRECTORY.

However, this link fails to link back to dir_2 as it is not set properly. This is also expected behaviour though, and not meant to fail. From the manpage:

Symbolic links can hold arbitrary text; if later resolved, a relative link is interpreted in relation to its parent directory.

By the way, it works the same way for me even with -sv. Maybe you are using a different implementation of ln. Are you sure you are not using -T? Maybe that is set in your ~/.bashrc/~/.zshrc/etc. Try which ln.


how to overwrite symblic link of directory?

The proper way to do this is to use the --no-dereference option like so.

$ ln -snf foo2 bar

This causes ln to treat the existing symlink as a file. Otherwise, it dereferences bar to foo1, descends into foo1 and uses the original TARGET name as the LINK_NAME and that's why you end up with a symlink to foo2 being created inside the foo1 directory. The manpage on ln states the following...

-n, --no-dereference
       treat  LINK_NAME  as a normal file if it is a symbolic link to a

Below is the shell output on my Arch Linux desktop with version 8.21 of ln with and without the --no-dereference option, I got the same results you did witout the --no-dereference option, but using the --no-dereference option it worked as expected.

$ mkdir foo1 foo2
$ ln -s foo1 bar
$ ls -l bar
  lrwxrwxrwx 1 drew users 4 Sep 17 12:51 bar -> foo1
$ ln -sf foo2 bar
$ ls -l bar
  lrwxrwxrwx 1 drew users 4 Sep 17 12:51 bar -> foo1
$ ls -l foo1
  total 0
  lrwxrwxrwx 1 drew users 4 Sep 17 12:51 foo2 -> foo2
$ ln -snf foo2 bar
$ ls -l bar
  lrwxrwxrwx 1 drew users 4 Sep 17 12:52 bar -> foo2

Symlink all files but copy certain extensions

From the /bar directory, to create all symlinks for everything but these extensions.

find ../foo/ -type f ! -name '*.txt' ! -name '*.baz' -exec ln -s '{}' \;

And then to copy all the extensions, same command for the most part.

find ../foo/ -type f \( -name "*.txt" -o -name "*.baz" \) -exec cp '{}' ./ \;

Edit: Copy entire directory structure, symlink some files, copy others. Wasn't sure if this was possible at first with a single command, but just learned some other handy tricks with find.

find foo/ -type d -printf "mkdir -vp 'bar/%p'\n" -o -type f ! -name "*.txt" ! -name "*.baz" -printf "ln -vs '../%p' 'bar/%p'\n" -o -type f \( -name "*.txt" -o -name "*.baz" \) -printf "cp -v %p bar/%p\n" | sh
mv bar/foo/* bar/ && rm -R bar/foo/

Only important thing to note is when making the symbolic link you give the actual path the links will take to relate to foo/. My example shows relative links when they are side by side (could also make absolute symbolic links as well).


Easiest way to restore symbolic links turned into text files?

Use this command:

ls *txt | xargs -I R1 sh -c "cat R1 | xargs -I R2 ln -sf R2 R1"

Replace ls *txt with something that will output the list of text files that should be symbolic links. For example, find . -name "*txt" would list all *txt files in or under the current directory.


Creating a symbolic link to a folder with ln command

A soft link is enough for your purpose. You simply need to omit the trailing slash, i.e.

ln -s ~/me

Linker (ln) error: can't find certain files

You should be able to edit the Makefile and accomplish this quickly.

NOTE - This assumes that the Makefile was made by GNU Autotools. According to the Cfengine tarball, they do.

First, open the Makefile and look for the flag LDFLAGS. It should look something like this:

LDFLAGS=-lfoo -lbar

Append to that listing -ldl. This adds a reference to the Dynamic Loading library.

Recompile, and the linker should be able to do it's job.

The LIBS environment variable is an environment variable that tells ./configure what extra libraries are needed, which it then sticks into the Makefile (so, to answer your question, ./configure doesn't actually run the linker - it makes the Makefile, which runs gcc with the parameters it got from ./configure).

So, use LIBS=-ldl && ./configure.


Using the link command to keep backups on another drive

Yes, it is true! To use /bigdata/backup as if it was /data/backup you can use these commands in a terminal.

First, as you already have a /data/backup directory, move everything in it to /bigdata/backup:

mv /data/backup/* /bigdata/backup

Then delete your old directory:

rm -r /data/backup

Now you can link the directories with:

ln -s /bigdata/backup /data/backup

This way, /data/backup is essentially just a redirect to /bigdata/backup; it's space on disk is negligible.


Listing All Linked Files

Try find / -samefile /file/to/compare.

There were a similar question: Finding all symbolic and hard links to a file on UNIX


Auto rename symbolic link

Add --backup=numbered to ln and .~n~ will be appended if a name already exist.


In the 1st form, create a link to TARGET with the name LINK_NAME. In the 2nd form, create a link to TARGET in the current directory. In the 3rd and 4th forms, create links to each TARGET in DIRECTORY. Create hard links by default, symbolic links with --symbolic. By default, each destination (name of new link) should not already exist. When creating hard links, each TARGET must exist. Symbolic links can hold arbitrary text; if later resolved, a relative link is interpreted in relation to its parent directory.

Mandatory arguments to long options are mandatory for short options too.

make a backup of each existing destination file


like --backup but does not accept an argument

-d, -F, --directory

allow the superuser to attempt to hard link directories (note: will probably fail due to system restrictions, even for the superuser)

-f, --force

remove existing destination files

-i, --interactive

prompt whether to remove destinations

-L, --logical

dereference TARGETs that are symbolic links

-n, --no-dereference

treat LINK_NAME as a normal file if it is a symbolic link to a directory

-P, --physical

make hard links directly to symbolic links

-r, --relative

create symbolic links relative to link location

-s, --symbolic

make symbolic links instead of hard links

-S, --suffix=SUFFIX

override the usual backup suffix

-t, --target-directory=DIRECTORY

specify the DIRECTORY in which to create the links

-T, --no-target-directory

treat LINK_NAME as a normal file always

-v, --verbose

print name of each linked file


display this help and exit


output version information and exit

The backup suffix is ’~’, unless set with --suffix or SIMPLE_BACKUP_SUFFIX. The version control method may be selected via the --backup option or through the VERSION_CONTROL environment variable. Here are the values:
none, off

never make backups (even if --backup is given)

numbered, t

make numbered backups

existing, nil

numbered if numbered backups exist, simple otherwise

simple, never

always make simple backups

Using -s ignores -L and -P. Otherwise, the last option specified controls behavior when a TARGET is a symbolic link, defaulting to -P.


Copyright © 2012 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <>.
This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

reporting bugs

Report ln bugs to bug-coreutils[:at:]gnu[:dot:]org
GNU coreutils home page: <>
General help using GNU software: <>
Report ln translation bugs to <>

see also

link , symlink

The full documentation for ln is maintained as a Texinfo manual. If the info and ln programs are properly installed at your site, the command

info coreutils 'ln invocation'

should give you access to the complete manual.


Written by Mike Parker and David MacKenzie.

How can this site be more helpful to YOU ?

give  feedback