0

I've been trying for hours to figure this out but cant seems to do it

My question Is I have a file name easy and there's 4 different sub directories in it enter image description here

now what I have to do is delete everything under foo (including hard links) and reclaim all the diskspace of foo.

I tried removing all the file by typing.

rm foo/*

but it does not remove the hard links that are in the other sub directories.

Then I tried to see if the Matching inodes

find foo -type f -ls | sort

and this is what I got with this command

enter image description here

this command doesn't show the others sub directories only foo/

can someone please help me out. Thank you.

3
  • 2
    PSA: Please don't post images of text
    – Wildcard
    Commented Nov 4, 2016 at 6:29
  • may be I didn't get your question right, but if you're trying to remove everything under foo, cant you just do -- rm -fr foo/* ?
    – jai_s
    Commented Nov 4, 2016 at 6:48
  • but it doesn't remove the hard links in other sub directories Commented Nov 4, 2016 at 6:55

2 Answers 2

0

If you want to delete all the files that are hard linked to any of the files found via descending foo, with GNU tools, you could do:

dir=foo
mountpoint=$(df --output=target -- "$dir" | tail -n +2)

awk -v RS='\0' -v ORS='\0' '
  ARGIND == 1 {inum[$0]; next}
  {i=$0; getline}
  i in inum
 ' <(find -- "$dir" -xdev ! -type d -links +1 -printf '%i\0') \
   <(find -- "$mountpoint" -xdev ! -type d -printf '%i\0%p\0') |
  xargs -r0 rm -f

rm -rf -- "$dir"

That assumes GNU df, GNU awk, GNU find and a shell like the GNU shell (bash) that supports process substitution (<(...)).

That also assumes $dir doesn't start with - or doesn't happen to be a find predicate (like !, (...).

Above we look for files on the whole file system $dir belongs to. You can replace $mountpoint with just the parent directory of $dir (. in this case) or the other subdirs (123 ABC def here) if you know all the hardlinks are only in there.

In the code above, we record the inode numbers of all the non-directory files with a link count greater than 1 that are found via descending $dir. And look for those inode numbers in the whole file system (at least sections of it that are not masked by other file systems and that you have permissions to traverse). inode numbers are only unique per-filesystem, so we use -xdev to make sure we only scan the one file system $dir is found on.

1
  • thank you for this answer but i don't think its this complicated because its an assignment which suppose to be like one or two line commands Commented Nov 4, 2016 at 8:33
0
rm -rf foo

rm - remove files or directories.

  • -f ignore nonexistent files and arguments, never prompt.
  • -r remove directories and their contents recursively.
1
  • yes but this doesn't remove the hard linked file that are in other sub directories right? Commented Nov 4, 2016 at 8:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .