[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 405
  • 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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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