0

I have the following folder structure:

├── longdirectorywithsillylengththatyouwouldntnormallyhave
│   ├── asdasdads9ads9asd9asd89asdh9asd9asdh9asd
│   └── sinlf
└── shrtdir
    ├── nowthisisalongfile0000000000000000000000000
    └── sfile

I need to find files and folders where their names length is longer is than x characters. I have been able to achieve this with:

find . -exec basename '{}' ';' | egrep '^.{20,}$'

longdirectorywithsillylengththatyouwouldntnormallyhave
asdasdads9ads9asd9asd89asdh9asd9asdh9asd
nowthisisalongfile0000000000000000000000000

However, This only outputs the name of the file or folder in question. How can I output the full path of resulting matches like this:

/home/user/Desktop/longdirectorywithsillylengththatyouwouldntnormallyhave
/home/user/Desktop/longdirectorywithsillylengththatyouwouldntnormallyhave/asdasdads9ads9asd9asd89asdh9asd9asdh9asd
/home/user/Desktop/shrtdir/nowthisisalongfile0000000000000000000000000
user unknown
  • 35,537
  • 11
  • 75
  • 121
harveyd
  • 35
  • 5

3 Answers3

1

If you use basename on your files, you lose the information about what file you are actually handling.

Therefore you have to change your regex to be able to recognize the length of the last path component.

The simplest way I could think of, would be:

find . | egrep '[^/]{20,}$' | xargs readlink -f

This makes use of the fact, that filenames cannot contain slashes.

As the result then contains path relative to you current cwd, readlink to can be used to give you the full path.

nlu
  • 1,903
  • 14
  • 18
  • Thanks, this worked well an seemed to do what I needed. Out of interest, do you know if there is something similar that would work on macOS? The `-i` flag on `xargs` doesn't appear to be supported? – harveyd Apr 12 '18 at 10:13
  • The `-i` is not necessary here - I edited the answer. This should be more portable. – nlu Apr 12 '18 at 12:55
0

I cant test it right now but this should do the job:

find $(pwd) -exec basename '{}' ';' | egrep '^.{20,}$'
Gaterde
  • 779
  • 1
  • 7
  • 25
0
find -name "????????????????????*"  -printf "$PWD/%P\n" 

The -printf option of find is very mighty. %P:

  %P     File's name with the name of the starting-point under which it was found removed. (%p starts with ./). 

So we add $PWD/ in front.

/home/stefan/proj/mini/forum/tmp/Mo/shrtdir/nowthisisalongfile0000000000000000000000000
/home/stefan/proj/mini/forum/tmp/Mo/longdirectorywithsillylengththatyouwouldntnormallyhave
/home/stefan/proj/mini/forum/tmp/Mo/longdirectorywithsillylengththatyouwouldntnormallyhave/asdasdads9ads9asd9asd89asdh9asd9asdh9asd

To prevent us from manually counting question marks, we use:

for i in {1..20}; do echo -n "?" ; done; echo
????????????????????
user unknown
  • 35,537
  • 11
  • 75
  • 121