Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I just wondering myself if it would be possible to create massive amount of symbolic links using a for loop?

For exemple, I have a lot of directory on my /tmp directory (I know, not recommanded) and I do not want to move them out.

Instead I just want to have them on my root directory using symbolic links.

Here is what I did, but unfortunatly, do not work at all:

for directories in /tmp/; do ln -s /tmp/$directories /$directories; done

So if someone could bring me a hint, it would be wonderfull ;-) Thanks.

share|improve this question
up vote 1 down vote accepted

Try this.

cd /tmp && for i in `ls `; do ln -s $i /$i; done
share|improve this answer
this will symlink files and is dangerous because it does not handle names with spaces properly – Hrvoje Špoljar Oct 9 '12 at 12:54

Following block will create links as you explained.

If you want to filter out some names from /tmp ; use -name switch for find.

If you want greater depth symlinked adjust/remove -maxdepth switch

cd / 
find /tmp/ -maxdepth 1 -type d -exec ln -s {} \;

To make these links you need write permissions on /

share|improve this answer
for directories in `find /tmp/ -type d`
  ln -s /tmp/$directories /$directories;
share|improve this answer
Yep, your answer is right, you just forgot the ln -s command ;-) – Dr I Oct 9 '12 at 10:53
well the one above is quite nicer then mine! – Harrys Kavan Oct 9 '12 at 10:58
this is dangerous and wont work if directory name has spaces – Hrvoje Špoljar Oct 9 '12 at 12:21
Could you explain, why it's dangerous in your opinion? – Harrys Kavan Oct 10 '12 at 8:47
because it can be tricked; creating directory with space will lead to creating symlink which was not intended to be there. Code logic can be abused – Hrvoje Špoljar Oct 13 '14 at 11:52

You need to have write permissions to the directory where you want the symbolic link to be created. Bear in mind that /tmp is (usually) cleaned up at boot and may be cleaned periodically throughout the machine's uptime, so relying on things being around persistently in /tmp will bite you, sooner or later.

share|improve this answer
I think I didn't ask correctly my question. Indeed, I've all needed rights on this machine, I'm root on it. But now, what I'm looking for, is to create a symbolic link on my root for each directories contain on /tmp ;-) For exemple: I have: /tmp/d1 /tmp/d2 /tmp/d... /tmp/dn and I want to link these previous directories like this: /d1 /d2 /d... – Dr I Oct 9 '12 at 10:20

Ok, so, Job done thanks to everyone for your hints.

Here is my simple solution:

 for i in $(ls); do dir=$i; ln -s /tmp/$dir /$dir; done

Et voila :D

Thank you, StackExchange community once again ;-)

share|improve this answer
Note that this is likely to fail horribly on filenames that contain spaces. – nickgrim Oct 9 '12 at 10:53
Indeed, I do not have any directory with spaces in name on my case. But yep, ls will just interpret each part of the directory name as a unique directory, so the solution is just to add "" in this case X) Just tested and work fine on my RHEL :D. But thanks for this comment which could be usefull. – Dr I Oct 9 '12 at 10:58

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.