Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
06-06-2012, 01:47 AM
|
#1
|
LQ Newbie
Registered: Jun 2012
Posts: 6
Rep:
|
Delete large number files along with hard links
How to delete large number of files (almost all the files having hard links) at once? Is there any script available to do this.
Here is actual situation:
I have copied the backups of BackupPC to an external drive using "cpio" command preserving all hard links.
Now I want to delete the backup of one host.Deleting the directory of Host from "/pc" doesn't empty any space because all the files in /pc/Host have hard links in /cpool directory
|
|
|
06-06-2012, 02:58 AM
|
#2
|
LQ Newbie
Registered: Jun 2012
Posts: 6
Original Poster
Rep:
|
The command used to copy the files from Backuppc directory is:
find . -mount -print | cpio -pdm <destination>
The PWD is source directory
Can "find" be used to find hard links to files ?
|
|
|
06-06-2012, 04:27 AM
|
#3
|
Member
Registered: Jan 2012
Posts: 55
Rep:
|
Hi
I just writed a script for this .And I also had a test in my own laptop.Maybe it can help you!
Code:
#!/bin/bash
sourcepath=/pc
linkpath=/cpool
find $sourcepath -type f |ls -li |awk '{if(NR!=1) print $1}'>./inodetmp
for i in $(cat ./inodetmp)
do
find $linkpath -type f -inum $i -exec rm -f {} \;
done
rm -f ./inodetmp
This can help you to remove the hard link in the path of /cpool as you mentioned.Then you can manually remove all files in the path of /pc or add a statement into the end of this script.
Hope this can be useful for you!
|
|
|
06-06-2012, 04:56 AM
|
#4
|
LQ Newbie
Registered: Jun 2012
Posts: 6
Original Poster
Rep:
|
Hi vp0619520,
Thanks Very much for you script.
I need to delete both files in /BackupPC/pc/<Hostname> and their hard links in /BackupPC/cpool/
So will this script delete the files in /cpool or just remove the hard link and keep files in /cpool.
Thanks,
Mohan
|
|
|
06-06-2012, 07:56 AM
|
#5
|
Member
Registered: Jan 2012
Posts: 55
Rep:
|
Quote:
Originally Posted by mohan.1418
Hi vp0619520,
Thanks Very much for you script.
I need to delete both files in /BackupPC/pc/<Hostname> and their hard links in /BackupPC/cpool/
So will this script delete the files in /cpool or just remove the hard link and keep files in /cpool.
Thanks,
Mohan
|
Hi
Actually, if you want to delete the files in order to release some space ,you have to delete both the /pc and /cpool .I think you should read some info about hard link.That will make you more plentiful to understand this .This script will delete the hard link in the path of /cpool,then as I say before,you can manually remove all files in the path of /pc/.So there is no record in the inode info link the space.Hope this is useful.
|
|
|
06-06-2012, 08:03 AM
|
#6
|
Member
Registered: Jan 2012
Posts: 55
Rep:
|
And you should replace the variable "sourcepath" "linkpath" to your own path,like"/BackupPC/pc/<Hostname>" maybe the sourcepath,and"/BackupPC/cpool/" is the "linkpath".
|
|
|
06-06-2012, 08:41 AM
|
#7
|
LQ Newbie
Registered: Jun 2012
Posts: 6
Original Poster
Rep:
|
So you are saying only hard links are removed , but corresponding files are still present in /cpool?
I know about hard links theoretically, but I am not good at scripting so I didn't understand what this line does:
find $linkpath -type f -inum $i -exec rm -f {} \;
Can modify the script so that it will delete the files in "cpool". Deleting file is main concern, removing just hard link and keeping files is of not much use for me.
I have used actual directory paths in the script.
Thanks very much!!!
|
|
|
06-06-2012, 08:59 AM
|
#8
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,737
|
mohan, you mixed some things I think:
1. cpio cannot make hard links, but probably I missed some options related to it.
2. if you make a new directory structure with cpio and you removes it with rm -rf <target directory> everything will be destroyed what was created by cpio.
3. "removing just hard link and keeping file" means you do not really know what does hard link really means. So please read this first: http://www.cyberciti.biz/tips/unders...ard-links.html
|
|
|
All times are GMT -5. The time now is 12:58 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|