LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Delete large number files along with hard links (https://www.linuxquestions.org/questions/linux-newbie-8/delete-large-number-files-along-with-hard-links-948753/)

mohan.1418 06-06-2012 01:47 AM

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

mohan.1418 06-06-2012 02:58 AM

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 ?

vp0619520 06-06-2012 04:27 AM

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!

mohan.1418 06-06-2012 04:56 AM

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

vp0619520 06-06-2012 07:56 AM

Quote:

Originally Posted by mohan.1418 (Post 4696696)
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.:)

vp0619520 06-06-2012 08:03 AM

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".

mohan.1418 06-06-2012 08:41 AM

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!!!

pan64 06-06-2012 08:59 AM

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 11:16 PM.