Sunday, April 28, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 603  / 3 Years ago, thu, may 27, 2021, 1:27:38

For a university project, I have to count all the files in a folder. I have used the command:



find ./dirName | wc -l


Although when I compare this to the file count provided by Nautilus it is considerably more. See the screenshot below:



enter image description here



./dirName is actually a directory of files from a repository (SVN/GIT) and I need to find out how many files make up the system.



Could anyone explain why these differences occur and maybe tell me which one is more reliable?


More From » nautilus

 Answers
3

Nautilus doesn't count hidden files.



Files and directories starting with a dot (.) are hidden in Linux.



Steps to reproduce:



mkdir somedir && cd somedir
touch .hidden .hidden2 regular regular2 # 4 files, 2 hidden
find . | wc -l # outputs 5 (4 files + dir itself)


Nautilus reports: Contents: 2 items, totalling 0 bytes



Using Git



Here's a quick demonstration on the amount of files for the metadata used in Git, all in the .git directory.



git init myrepo                              # Initialized [...] in myrepo/.git/
cd myrepo/
find . | wc -l # outputs 23! for an empty repository
tree -a # outputs 10 directories, 12 files

echo "have to add something for git ls-tree" > somefile
git add somefile && git commit -m "Initial commit"
find . | wc -l # outputs 38 (!)
git ls-tree -r HEAD | wc -l # outputs 1


And also Nautilus reports 1 there.



My suggestion: use tree



As Gilles pointed out in his answer, using find and piping it to wc isn't overly reliable if the file names contain special characters.



It seems that tree is capable of doing this right:



tree -a
.
├── dir
│   └── regular3
├── dir2
├── .hidden
├── .hidden2
├── regular
└── regular2

2 directories, 5 files

[#33072] Friday, May 28, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
darpose

Total Points: 424
Total Questions: 99
Total Answers: 121

Location: Jersey
Member since Fri, Oct 1, 2021
3 Years ago
;