Reputation: 1169
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);
Upvotes: 1
Views: 3232
Reputation: 98068
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.
Upvotes: 0
Reputation: 129481
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
dir1
. dir2
to point to dir1
. dir2
, but in reality it was created in dir1
dir1
. It was ALSO accessible via dir2
(symlink).Upvotes: 3