File names on Linux techniques might be so long as 255 characters. Whereas figuring out which information in a listing have the longest names may not be probably the most thrilling activity at hand, doing this with a script poses some attention-grabbing challenges that invite equally attention-grabbing options.
To begin, contemplate passing the output of the ls command, which is used to record information, to a wc command that counts the characters like this:
$ ls myreport.txt | wc -c 13
When you counted the letters in “myreport.txt” by “myreport.txt”, you possible seen that there are 12, not 13 letters in that file title. It’s because, simply as within the command beneath, echo sends the requested textual content by way of the pipe together with a newline character on the finish.
$ echo hiya | wc -c 6
You’ll be able to see this subject extra clearly by passing the identical output to the od -bc command. It makes the inclusion of the newline very apparent.
$ echo hiya | od -bc 0000000 150 145 154 154 157 012 h e l l o n <=== There it's! 0000006
To keep away from the additional character, simply add a -n (take away newline) choice to the command.
$ echo -n hiya | wc -c 5
When you tried a command just like the one beneath, you’d shortly see that the interval is taken actually. The ensuing “.” adopted by a carriage return yields a size of two.
$ for file in . do echo $file | wc -c finished 2
The command beneath will generate an inventory with file names and lengths, but it surely has one significant issue. It’ll break file names together with blanks into various components and report the lengths of every half individually.
$ for file in `ls` do echo -n “$file “ echo -n $file | wc -c finished
Right here’s an instance:
$ for file in `ls Velocity*` do echo -n “$file “ echo -n $file | wc -c finished Dashing 8 up 2 scripts 7 utilizing 5 parallelization 15
In distinction, the command beneath will record the entire information within the present listing adopted by their lengths.
$ for file in * do echo -n "$file " echo -n $file | wc -c finished
The additional clean within the first echo command is used to go away an area between file names and lengths.
hiya 5
Make some small modifications and the command will type the information by filename size.
$ for file in *; do len=`echo -n $file | wc -c`; echo $len $file; finished | type -n
Including a tail command to the top will present the title and size of the file with the longest title solely.
$ for file in *; do len=`echo -n $file | wc -c`; echo $len $file; finished | type -n | tail -1 41 Dashing up scripts utilizing parallelization
The script beneath shows solely the file with the longest filename after prompting for the listing to be examined. It then finds the longest filename by retaining the longest filename encountered whereas looping by way of the information till it finds an extended one. The “for file in $dir/*” supplies the wanted looping with out breaking apart filenames on blanks.
It additionally ensures that the correct size of the longest file is included within the line following the “for file” command. It removes the title of the listing that it’s wanting by way of together with the next “/” by utilizing a sed command to scale back the string to simply the file title. Commas are used within the sed command to keep away from colliding with the backslash characters which might be usually used with sed.
#!/bin/bash # discover file with longest filename echo -n "dir> " learn dir longestname=0 for file in $dir/*; do file=`echo $file | sed s,$dir/,,` sz=`echo $file | wc -c` # get filename size if [ $sz -gt $longestname ]; then longestname=`expr $sz - 1` # scale back by 1 for carriage return longname=$file fi finished echo $longestname: $longname
Operating this script ought to look one thing like this:
$ ./LongFname dir> . 41: Dashing up scripts utilizing parallelization $ ./LongFname dir> ./bin 17: loop-days-of-week
Wrap-Up
Looping by way of an inventory of information to seek out these with the longest filenames requires an excellent understanding of how loops work and the way blanks in filenames can complicate the required instructions.
Copyright © 2022 IDG Communications, Inc.