Linux – Finding the length of files and file path of directory structure in a Linux file system

filesystemslinux

I have a problem on a Linux OS running a version of SMB where if the absolute path to a directory within a Shared Folder is greater than 1024 bytes and the filename component is greater than 256 bytes the SMB service crashes and locks out all other services for network access like, SSH and FTP rendering the machine mute.

To keep the system for crashing I’ve temporarily moved a group of folders where I think the problem path may be located outside of Shared Folder. I need to find the file and file path that exceeded this limitation and rename them or remove them allowing me to return a bulk of the files to the Shared Folder.

I’ve tried the find and grep commands without success.
Is there a chain of commands or script that I can use to hunt down the offending files and directory?

Please advise.

Best Answer

This is written in Bash and uses features specific to it (but similar features are available in other shells). It's designed to be run from a parent directory common to all the files and directories you're interested in. It takes into account the length of the path from / to there and adds it to the length of each evaluated path. If you don't want to do that just use lenpwd=0 instead of lenpwd=${#PWD}. It will work if there are files with spaces in their names but not those that have newlines (which should be banished any way). It prints the lengths and filespec of anything it finds that meet the criteria.

lenpwd=${#PWD}; find | while read -r path; do file=${path##*/}; if (( ${#path} + lenpwd > 1024 || ${#file} > 256 )); then echo "$((${#path} + lenpwd)) ${#file} $path"; fi; done