1

I need help for a bash script that counts files and folders in a specified directory on a Linux system (Debian), but I want to exclude a specified folder.

I have a main directory named workdir with different script files and folders. Inside workdir, I have a directory named mysshfs. I use fuse/sshfs to mount an external folder in the mysshfs folder.

Now I start some commands to get information about file/directory count and file/directory size, but I want to exclude the directoy mysshfs.

My bash commands that work:

  1. get the full size of workdir | no fuse/sshfs in use

    $ du -hs workdir
    
  2. get the full size of workdir, excluding mysshfs | fuse/sshfs in use

    $ du -hs --exclude=mysshf workdir
    
  3. count files in workdir | no fuse/sshfs in use

    $ find workdir -type f | wc -l
    
  4. count folders in workdir | no fuse/sshfs in use

    $ find workdir -type d | wc -l
    
  5. count files in workdir, excluding mysshfs | no fuse/sshfs in use

    $ find workdir -type f -not -path "*mysshfs*" | wc -l
    
  6. count folders in workdir, excluding mysshfs | no fuse/sshfs in use

    $ find workdir -type d -not -path "*mysshfs*" | wc -l
    

When I use commands 5 & 6 and the remote directory is mounted under the mysshfs directory, the commands hang.

The commands eventually works and show the correct output, but it looks like the commands are still looking inside the excluded directory even though they shouldn't be, so it takes a long time to display the result.

Where is my error or did I forget something in my commands 5 & 6? Or can I use other commands for my results?

I need to count files and directories using 2 separate commands and exclude a specified folder that is mounted over fuse/sshfs to get a fast result.

0

2 Answers 2

5

You can use -prune to avoid descending into subdirectories. Try these commands instead:

find workdir -path "*/mysshfs/*" -prune -o \( -type f -print \) | wc -l
find workdir -path "*/mysshfs/*" -prune \( -type d -print \)  | wc -l
1
  • -name mysshfs would make more sense and be more reliable than -path "*/mysshfs/*". I think you're missing a -o in the second one. Commented May 12, 2023 at 11:01
1

To skip files on fuse.sshfs FS types, use:

find workdir//. -fstype fuse.sshfs -prune -o -type f -print | grep -c //

Or use -xdev to not traverse to any other file system (of any type).

In any case, you can't use wc -l to count files as the newline character is as valid as any in a file type. Here we count the number of occurrences of // which is guaranteed to occur only once, though with GNU find, you could also do:

find workdir -fstype fuse.sshfs -prune -o -type f -printf . | wc -c

Or to get a break down of number of files per type (f for regular, d for directory, l for symlink, p for named pipe, etc):

find workdir -fstype fuse.sshfs -prune -o -type f -printf '%y\n' |
  sort |
  uniq -c
0

You must log in to answer this question.

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