How to select unique hard link files?
Calculate the total sze of the files recursively from the current directory. Hard linked files are to be considered only once.
Please use awk also. |
Homework? We are willing to help, but we won't do it for you....
What have you tried this far and what are the problems you run into? |
Code:
find . -type f -ls | awk '{$7+= $7 END {print $7}' This may also includes the same hard-linked files. So, how to write a program that will fetch the size of all the files but the hard-linked ones have to be included only ones. I explain the same a bit more. Suppose 2 files are there having same inode number and both are located in the same directory tree from the current directory. So, I need that when the program calculates the total size of all the files, it should consider only 1 file among the 2 hard-linked files. |
Quote:
|
Hint: ls can show the amount of links; In case of hardlinks, this counter increases. If it's > 1, fetch the inode (ls can show that as well), store it somehow and if the inode is in your array (and thus already counted once) skip it. Otherwise add the size to the total size and store the inode in your array.
Somewhat abstract: Code:
ls -li # shows inode - accessbits - link count - user - group - size - date - time - filename |
It's not really a good idea to parse ls for filenames or metadata.
I suggest instead looping through the file names and use the -ef flag in the test construct, or extract the inode numbers with stat, to remove any that evaluat as the same. If this can be a bash script specifically (or ksh) then I also recommend using arrays to keep track of everything. |
Ramurd, I know the logic but I need codes.
|
If you know the logic coming up with the code should be the easy part. Try it and when you have problems with your code post it here, so that we can see possible errors in the code.
We won't deliver you ready-made code, doing your homework is not something we do on this forum. |
Mr. Moderator, please make it a welcoming forum for all. Don't take it as a homework. It's common to come up with such an issue.
Please see my earlier post where i have posted my code. Now let the viewers reply. Quote:
THANKS AND HOPE YOU WILL ALWAYS LOOK FORWARD TO HELP OTHERS AND ENCOURAGE OTHERS AND GIVE EVERYBODY A FEELING THAT THERE SOLUTION IS IN THIS FORUM. |
There is a difference between sharing knowledge and doing your work for you. Your initial question is clearly phrased in a way that it resembles a homework question, so we have to assume that it is one, and on this forum we won't do your work for you.
You were given pseudo-code pointing out what you have to do to make it work, Ramurd has done a good job with that. Try to make real code from it and when you have difficulties with that come back with what you have already and point out where it is not working or where you have difficulties. If you want to have ready-made code hire a programmer. |
Quote:
- "-b, --escape print C-style escapes for nongraphic characters". - " -Q, --quote-name enclose entry names in double quotes --quoting-style=WORD use quoting style WORD for entry names: literal, locale, shell, shell-always, c, escape" (both taken from ls --help :-) ) That said, the OP asked for a full 'awk' solution. I'm not (that) fluid in awk; so I'd work around that and use a bash or ksh solution instead. But that's not what was requested, so I offered a sort of workaround. There's a few things to keep in mind about this: One has to make sure ls provides consistent behavior, both regarding date and time and filenames. so this would be a first step approach around the problem, but it's not the solution that was requested; it's a solution to the problem. And if this is homework, then it really should be done in awk, but in that case I leave it to the student :-) Code:
declare -a INODES Also mind that there are implementations of ls that offer the possibility to provide a list separated by commas with the "-m" parameter. This may help you in your awk script. Mind that filenames might also contain commas, but those should be listed as escaped characters. I leave that exercise to someone else. |
Quote:
In fact, it looks like one of the many 'just cut and paste' questions, where the poster hasn't even re-phrased the question that they have been asked by their course tutor. And, by the way, what is your intention with respect to soft linked files? Quote:
Quote:
HTH |
Ramurd, I thank you very much
|
All times are GMT -5. The time now is 08:57 AM. |