[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 403
  • Last Modified:

Get length of filenames in debian linux

I want to run a find command piped to something that will give me the length of each filename, including the folders that contain it

eg:

find . | getlength

outputs:
blah/foo 8
blah/barney 11
foo/abc 7
0
Terry Woods
Asked:
Terry Woods
  • 2
  • 2
  • 2
  • +1
3 Solutions
 
Todd MummertCommented:

something like

find . | perl -pe 'chomp; $_.=" ".length()."\n"'


example output:

user@host:~/puzzle# find . | perl -pe 'chomp; $_.=" ".length()."\n"'
. 1
./decrypt.pl~ 13
./input 7
./decrypt.pl 12
./foo 5
./foo/afile 11
0
 
ozoCommented:
Do you want the length without the ./ ?
0
 
Kerem ERSOYPresidentCommented:
How about :

find . -exec du {} \;
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Todd MummertCommented:


ozo:  that's a good question.   In the case of   find /tmp | ...      I would assume you'd want everything counted.   Perhaps, . is a special case....

In that case, this works:

find . | perl -ne 'chomp; unless (/^.$/) {$a=substr($_,/^\.\//?2:0); print "$a ".length($a)."\n"}'

same thing w/ a real dir or list of dirs:

find /etc /tmp | perl -ne 'chomp; unless (/^.$/) {$a=substr($_,/^\.\//?2:0); print "$a ".length($a)."\n"}'


KeremE:  du prints the disk usage (in blocks)... I think the OP wants character count
--t
0
 
Terry WoodsIT GuruAuthor Commented:
Thanks very much for the quick response - I ended up using a slightly modified version of the first answer:

find . | perl -pe 'chomp;$_=length()." $_\n"' | sort -n > /blah/filelength.txt

to get what I needed.

It turned out I was looking for slightly the wrong value - a (Windows) DVD write was being rejected because of a 106 char limit, but that was actually referring to the filename without the path included, thankfully. I only had to rename 1 file (instead of over 900 if that wasn't the case) to fix the issue.
0
 
Terry WoodsIT GuruAuthor Commented:
Thanks very much!!
0
 
Kerem ERSOYPresidentCommented:
@climbgunks:

du -b will use bytes instead so:

find . -exec du -b {} \;

prints the length as bytes :))
0
 
ozoCommented:
an easier way to do
unless (/^.$/) {$a=substr($_,/^\.\//?2:0);
would be
s/^\.\///; $a=$_;
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now