Solved

Get length of filenames in debian linux

Posted on 2009-05-14
8
397 Views
Last Modified: 2013-12-06
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
Comment
Question by:Terry Woods
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 11

Accepted Solution

by:
climbgunks earned 480 total points
ID: 24392364

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
 
LVL 84

Assisted Solution

by:ozo
ozo earned 20 total points
ID: 24392427
Do you want the length without the ./ ?
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24392472
How about :

find . -exec du {} \;
0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 11

Assisted Solution

by:climbgunks
climbgunks earned 480 total points
ID: 24392478


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
 
LVL 35

Author Comment

by:Terry Woods
ID: 24392533
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
 
LVL 35

Author Closing Comment

by:Terry Woods
ID: 31581798
Thanks very much!!
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24392546
@climbgunks:

du -b will use bytes instead so:

find . -exec du -b {} \;

prints the length as bytes :))
0
 
LVL 84

Expert Comment

by:ozo
ID: 24392607
an easier way to do
unless (/^.$/) {$a=substr($_,/^\.\//?2:0);
would be
s/^\.\///; $a=$_;
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you use Debian 6 Squeeze and you are tired of looking at the childish graphical GDM login screen that is used by default, here's an easy way to change it. If you've already tried to change it you've probably discovered that none of the old met…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question