LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 06-06-2012, 01:47 AM   #1
mohan.1418
LQ Newbie
 
Registered: Jun 2012
Posts: 6

Rep: Reputation: Disabled
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
 
Old 06-06-2012, 02:58 AM   #2
mohan.1418
LQ Newbie
 
Registered: Jun 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
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 ?
 
Old 06-06-2012, 04:27 AM   #3
vp0619520
Member
 
Registered: Jan 2012
Posts: 55

Rep: Reputation: Disabled
Post

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!
 
Old 06-06-2012, 04:56 AM   #4
mohan.1418
LQ Newbie
 
Registered: Jun 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
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
 
Old 06-06-2012, 07:56 AM   #5
vp0619520
Member
 
Registered: Jan 2012
Posts: 55

Rep: Reputation: Disabled
Quote:
Originally Posted by mohan.1418 View Post
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.
 
Old 06-06-2012, 08:03 AM   #6
vp0619520
Member
 
Registered: Jan 2012
Posts: 55

Rep: Reputation: Disabled
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".
 
Old 06-06-2012, 08:41 AM   #7
mohan.1418
LQ Newbie
 
Registered: Jun 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
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!!!
 
Old 06-06-2012, 08:59 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,737

Rep: Reputation: 7558Reputation: 7558Reputation: 7558Reputation: 7558Reputation: 7558Reputation: 7558Reputation: 7558Reputation: 7558Reputation: 7558Reputation: 7558Reputation: 7558
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Need help sorting large number of files by timestamp and then greping those files scottjn Linux - Newbie 4 01-14-2011 10:42 AM
maximum number of hard links hydraMax Linux - Server 1 12-08-2010 01:48 AM
ext3 performance -- very large number of files, large filesystems, etc. td3201 Linux - Server 5 11-25-2008 09:28 AM
Hard links Number and Directories ahmedb72 Solaris / OpenSolaris 2 07-04-2007 01:54 PM
Large Number of files? mikeshn Linux - Security 2 01-10-2004 06:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:58 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