1

I am quite new to Perl and have figured out how to symlink files. Can you do this with directories as well? I would like to make a symlink between a certain directory and one or more other directories so if I edit a file in the original directory, changes will be updated in the symlinked directories. Is this possible?

edit: my Perl code for creating symlinks for files in a given directory is below.

Thanks

#!/usr/bin/perl

$fileDirectory = "/home/Alan/Perl/Files/";
$symLinkDirectory = "/home/Alan/Perl/SymFiles/";

opendir ( DIR, $fileDirectory ) || die "Error in opening directory $fileDirectory\n";

while( ($fileName = readdir(DIR))){
    my $filePath = $fileDirectory.$fileName;
    symlink("$filePath","$symLinkDirectory".$fileName);
}

closedir(DIR); 
DVK
  • 126,886
  • 32
  • 213
  • 327
Alan Smith
  • 1,069
  • 4
  • 19
  • 23

2 Answers2

3

Yes. On a Unix filesystem, when you manipulate a file whose directory path starts with a prefix that is actually a symlink to another directory, the file operation will automatically happen within a directory that is a target of a symlink.

This includes write operations that are done "on a symlink path" that actually take effect on a real file; as well as write operations that are done on a file in original directory path that will be visible from symlinked paths.

This has nothing to do with Perl really, but obviously works in Perl as well:

$ perl5.8 -e 'symlink("/HOME/dir1","/HOME/dir2"); 
              use File::Slurp qw(write_file); 
              write_file("/HOME/dir2/myfile", "File lives in dir1\n"); 
              write_file("/HOME/dir1/myfile2","File visible in dir2 too\n");'

$ ls -l /HOME/dir1
total 1
-rw-rw-r--+ 1 USER users 4 Aug 15 09:56 myfile
-rw-rw-r--+ 1 USER users 4 Aug 15 09:56 myfile2

$ ls -l /HOME/dir2
lrwxrwxrwx 1 USER users 22 Aug 15 09:56 /HOME/dir2 -> /HOME/dir1/

$ ls -l /HOME/dir2/myfile2
-rw-rw-r--+ 1 USER users 4 Aug 15 09:56 /HOME/dir2/myfile2

$ ls -l /HOME/dir1/myfile
-rw-rw-r--+ 1 USER users 4 Aug 15 09:56 /HOME/dir1/myfile
  • I had a real directory dir1.
  • I symlinked dir2 to point to dir1.
  • I created a file in dir2, but in reality it was created in dir1
  • I created another file in dir1. It was ALSO accessible via dir2 (symlink).
DVK
  • 126,886
  • 32
  • 213
  • 327
0

Symbolic links are file system dependent. In Linux file systems generally you can have symbolic links for directories. Symbolic links are not copies/backups, they are only shortcuts for one real file/directory.

daxim
  • 39,270
  • 4
  • 65
  • 132
perreal
  • 94,503
  • 21
  • 155
  • 181
  • Ok. Well I am on Linux. I'm moving away from Windows :) Sorry I probably should have worded it better... instead of having to go into a directory and then symlink the files in it to another directory, can I not just symlink the whole directory and all its contents to another directory? – Alan Smith Aug 15 '12 at 13:40
  • Yes, this is what happens when you create a symlink to a directory. You can access all sub files/directories through this link. – perreal Aug 15 '12 at 13:43