LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-26-2010, 05:49 AM   #1
isaaclw
Member
 
Registered: Dec 2009
Posts: 36

Rep: Reputation: 15
how do you "hard link" a whole directory?


I would like to mirror a folder with hardlinks.
Then I can go through and change some of the files, while keeping most the same.

The script I have so far is this:
Code:
#!/bin/bash
old="jaunty"
new="lucid"
dir="/conf"
cd $dir
find . -type d -exec mkdir -p $dir/$new/{} \;
find . -type f -exec ln $dir/$old/{} $dir/$new/{} \;
The problem with this is since I make the folders again, this doesn't preserve their permissions.
Is there any way to do this better? I feel like rsync should have an option for this, but I can't find it...
 
Old 05-26-2010, 06:29 AM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
Are you aware that hard links point to the same file and that changing a hard linked file under one name will change it under all names that refer to it?
 
1 members found this post helpful.
Old 05-26-2010, 06:38 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
How about using chmod and chown with the --reference option, something like find . -type d -exec mkdir -p $dir/$new/{} \; -exec chmod --reference {} $dir/$new/{} \; -exec chown --reference {} $dir/$new/{} \;
 
1 members found this post helpful.
Old 05-26-2010, 06:52 AM   #4
vorbote
Member
 
Registered: Aug 2009
Location: 2,625 m of paranoia above sea level
Distribution: Arch Linux, Debian GNU/Linux, Fedora, Void Linux, FreeBSD, OpenBSD and Win10
Posts: 77

Rep: Reputation: Disabled
You cannot hardlink a directory, at least when using filesystems that use traditional POSIX semantics; I bet BtrFS will let you do it with a judicious use of subvolumes sometime in the future.

Now, you can create directory trees with hardlinked files easily if using GNU coreutils and rsync. Use "cp -Hlr <fromdir> <targetdir>" to create the initial copy and "rsync -aHP --delete <fromdir> <targetdir>" for keep the target directory clean of cruft from time to time, else just fuggit about the latter. :-)
 
Old 05-26-2010, 07:24 AM   #5
isaaclw
Member
 
Registered: Dec 2009
Posts: 36

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by vorbote View Post
Now, you can create directory trees with hardlinked files easily if using GNU coreutils and rsync. Use "cp -Hlr <fromdir> <targetdir>" to create the initial copy and "rsync -aHP --delete <fromdir> <targetdir>" for keep the target directory clean of cruft from time to time, else just fuggit about the latter. :-)
Actually, I'm pretty sure that these commands only keep Hard Links if there are any.
They don't make any new Hard links...

But the "find" command did it.
 
Old 05-27-2010, 06:39 PM   #6
vorbote
Member
 
Registered: Aug 2009
Location: 2,625 m of paranoia above sea level
Distribution: Arch Linux, Debian GNU/Linux, Fedora, Void Linux, FreeBSD, OpenBSD and Win10
Posts: 77

Rep: Reputation: Disabled
Quote:
Originally Posted by isaaclw View Post
Actually, I'm pretty sure that these commands only keep Hard Links if there are any.
They don't make any new Hard links...

But the "find" command did it.
You are wrong on both counts. Try them first, critique second. :-)
 
Old 05-28-2010, 05:50 AM   #7
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
lndir

Although probably not hard links. But a common tool when building sources so as to not step on the source.
 
Old 06-09-2010, 10:06 AM   #8
isaaclw
Member
 
Registered: Dec 2009
Posts: 36

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by vorbote View Post
You are wrong on both counts. Try them first, critique second. :-)
Ok, I admit, I didn't try them first, but I'm still right:
From the man pages of rsync:
Code:
        -H, --hard-links            preserve hard links
From the man pages of cp:
Code:
       -H     follow command-line symbolic links in SOURCE
But most importantly, when running the commands it takes up disk space:
Code:
cp -H /var/cache/apt-cacher/packages/* _import/
rsync -Ha /var/cache/apt-cacher/packages/ _import/
So... I guess back to the find command.
 
Old 06-10-2010, 06:32 AM   #9
vorbote
Member
 
Registered: Aug 2009
Location: 2,625 m of paranoia above sea level
Distribution: Arch Linux, Debian GNU/Linux, Fedora, Void Linux, FreeBSD, OpenBSD and Win10
Posts: 77

Rep: Reputation: Disabled
Once again you are jumping to conclusions without even trying it youself.

Well, if you want to live like if it were 1985 and to discard information on what a poorly written man page says instead of trying it yourself, be my guest. :-)
 
Old 06-11-2010, 06:07 AM   #10
isaaclw
Member
 
Registered: Dec 2009
Posts: 36

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by vorbote View Post
Once again you are jumping to conclusions without even trying it youself.

Well, if you want to live like if it were 1985 and to discard information on what a poorly written man page says instead of trying it yourself, be my guest. :-)
I'm going to ignore you insulted me, and ignore the fact that 1) you didn't notice that at the end I tried it and 2) you're apparently not trying yourself.

Instead, since we're two perfectly reasonable people, I'm going to preform a test that you can try yourself.

1) df -h:
Code:
Filesystem            Size  Used Avail Use% Mounted on
/dev/sdd2             9.9G  8.2G  1.3G  87% /media/gamma
This is the drive I'm working in.

2) cd /media/gamma
du -h --max-depth=1
Code:
root@lari-slow:/media/gamma# du -h --max-depth=1
16K     ./lost+found
4.7G    ./music
1.5G    ./comics
1.8G    ./pictures
163M    ./videos
8.0G    .
It seeems that Comics is just bigger then the space to fill the drive. No matter, if it's hard links it shouldn't take up space at all:
As you say, this should hard link the whole folder:
Code:
root@lari-slow:/media/gamma# rsync -aH comics/ comics2/
an hour later it finishes. No errors
and the drive is full:
Code:
Filesystem            Size  Used Avail Use% Mounted on
/dev/sdd2             9.9G  9.7G     0 100% /media/gamma
Now, I'll admit that I'm surprised that this 1.5 GB turned into 1.3 GB... however I'm not convinced that any hardlinks were made.

I would expect an apology towards anyone else who would have (no doubt) explained their situation better then I did. However, since I did a terrible job of explaining myself, I'll take the blame.
 
Old 06-11-2010, 07:21 AM   #11
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
vorbote and isaaclw;

No more personal jabs---OK?
 
Old 04-06-2011, 11:25 AM   #12
wallbunny
LQ Newbie
 
Registered: Jul 2008
Posts: 6

Rep: Reputation: 0
Solution

I just found this thread since I had a similar problem, and I figured I should post how I solved it.
My problem was that I wanted to keep file and folder structure intact in case i messed up when renaming all my music files and folders.

cd /mnt/
rsync -a Music/ OldMusicDir/ --link-dest=/mnt/Music/
$ find Music/|wc -l
47732
$ find OldMusicDir/|wc -l
47732
$ du -h -s OldMusicDir/
1003G OldMusicDir/
$ du -h -s Music/
1003G Music/
$ du -h -s Music/ OldMusicDir/
1003G Music/
15M OldMusicDir/

Enjoy!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
"creating symbolic link" "operation not permitted" wheni Linux - Newbie 3 05-08-2011 01:36 AM
"Permission denied" and "recursive directory loop" when searching for string in files mack1e Linux - Newbie 5 06-12-2008 07:38 AM
How to link "free space on Hard Drive to LINUX" navin_karnam Linux - Desktop 2 06-21-2007 03:14 AM
Creating a link from "." to other directory on SCP bujecas Linux - Security 4 02-01-2007 10:54 AM
Error when using 'find' -- "WARNING: Hard link count is wrong..." xGreenmanx Linux - Software 6 03-11-2006 06:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration