LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to treat the error code : name file too long (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-treat-the-error-code-name-file-too-long-675538/)

mland 10-10-2008 02:47 PM

How to treat the error code : name file too long
 
Hi team,


A problem with a bash shell.

Here is the context :

for rpt in `ls myfiles*.txt`
do

instruction 1 $rpt
instruction 2 $rpt
...
instruction n $rpt

done

When to much files an error message : name file too long appears.

How to treat correctly that problem ?

Any information will be appreciate.

Thank's in advance.

mland.

matthewg42 10-10-2008 06:58 PM

Is the error message translated? I would expect something more like "argument list too long".

First thing (although it won't help you with this exact problem - just a general tip) - you don't need to do this:
Code:

for rpt in `ls myfiles*.txt`
This will often work, but it's not the best way. It works by the backtick syntax running the command "ls myfiles*.txt", and taking the result of that an substituting it as the for loop list. The problem is that if you have a file with a space in the name, it will be split when the output of ls is read, and you will probably get two or more iterations of the loop on non-existing, or (worse) incorrect files.

The good news is, the shell will expand glob patterns for you, so just do this instead:
Code:

for rpt in myfiles*.txt
OK, back to the real problem. The shell has a limit to the number (and total length) of items in an argument list. This is most often encountered when you expend a glob pattern which matches a very large number of files. There are a few solutions. The simplest one when you have multiple commands to use with arguments (as you do) is to use find to identify the files, and then read the output of find into a while loop:
Code:

find . -name "myfiles*.txt" -maxdepth 1 -type f |while read rpt
do
  instruction 1 $rpt
  instruction 2 $rpt
  ...
  instruction n $rpt
done

The disadvantage here is that the code block of the while loop is executed in a sub-shell. This means that you cannot modify the value variables in your main script inside the loop, and then read the modified values once the while loop is complete.

[/CODE]#!/bin/bash

counter=0

seq 1 5 | while read n; do
let counter+=1
echo "random number $counter is $RANDOM"
done

echo "after loop, value of counter is: $counter"[/CODE]

I hope that helps.

mland 10-11-2008 04:32 AM

A complement to this topic : how to add a sort clause to the list of files ?
 
Thank's you matthewg42 for replys.

It was a great help.

A complement to this topic : how to add a sort clause to the list of files ?


Thank's in advance.

mland.

matthewg42 10-11-2008 05:41 AM

If you're piping the files into a while loop, just insert a sort in the pipeline:
Code:

find . -name "myfiles*.txt" -maxdepth 1 -type f | sort | while read rpt
...


mland 10-11-2008 10:37 AM

Thank's again,

a complement to this topic,

The command above :

find . -name "myfiles.txt" -maxdepth 1 -type f | while read rpt

return a list of filenames "myfiles.txt" with the path.


I try the command above to return the list of filenames without the path

find . -name "myfiles.txt" -maxdepth 1 -type f -printf %f | while read rpt


But it nothing is return.

What is wrong and how to do it ?

Thank's in advance for a return

mland.

chrism01 10-11-2008 10:45 PM

Code:

for fpath in `find . -name "myfiles*.txt" -maxdepth 1 -type f`
do
  rpt=`basename $fpath`    # strip dirs

  instruction 1 $rpt
  instruction 2 $rpt
  ...
  instruction n $rpt
done



All times are GMT -5. The time now is 09:58 AM.