Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

5 Answers 5

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
for directories in `find /tmp/ -type d`
do
  ln -s /tmp/$directories /$directories;
done
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! –  zwarag 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? –  zwarag Oct 10 '12 at 8:47

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

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

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

Your Answer

 
discard

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.