1

I am trying to figure out the command below;

find ./ -type f |
awk -F / -v OFS=/ '{$NF="";dir[0ドル]++} END {for (i in dir) print dir[i]i}'

the output is like:

6./Release_1_18_1_0_06_26/metadata/3_Control_Files/ 
5./Release_1_18_1_0_06_26/metadata/7_SAS_Code/ 
5./Release_1_18_1_0_06_26/others/1_content/ 
1./.cache/pip/selfcheck/ 
2./Release_1_18_1_0_06_26/metadata/5_Status/ 
1./Release_1_18_1_0_06_26/compute/2_packages/ 
1./sasuser.v94/ 
4./metadata/FR1_Release_1.17.1.1/3_Control_Files/ 
4./Release_1_18_1_0_06_26/metadata/6_Patches/ 

This command am counting the number of incode in the current path. However, I did not understand {$NF="";dir[0ドル]++} END {for (i in dir) print dir[i]i}, especially dir[0ドル]. anyone can explain that?

anubhava
790k67 gold badges603 silver badges671 bronze badges
asked Aug 24, 2020 at 16:04

2 Answers 2

3

Could you please go through following, detailed explanation of OP's code here.

find ./ -type f | ##Running find command to find files in current directory and passing output as input to awk command.
awk -F / -v OFS=/ ' ##Running awk command setting field separator and output field separator as /
{
 $NF="" ##Nullifying last field of current line here.
 dir[0ドル]++ ##creating array dir with current line and keep increasing its value with one here.
}
END{ ##starting END block of this code here.
 for(i in dir){ ##traversing through dir array here.
 print dir[i]i ##printing index of dir array and it's index here.
 }
}'

NOTE:END block of any awk code is executed when Input_file is being done with reading at last.

answered Aug 24, 2020 at 16:27
Sign up to request clarification or add additional context in comments.

1 Comment

Nice. dir[0ドル] can remember the last count for its line, That is why the array can increase the value.
3

The -F / tells awk to split the line by slashes. 0ドル is the whole line.

$NF="" replaces the last item on the line (in this case the filename) with blank.

Then dir[0ドル]++ takes the whole line (after the filename as been removed) and uses that as the index into a hash, incrementing that value by one. Effectively counting the number of items that had the same path.

The END block loops through all keys in the dir[] hash printing first the count, then the directory name.

answered Aug 24, 2020 at 16:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.