LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-10-2008, 02:47 PM   #1
mland
LQ Newbie
 
Registered: Oct 2008
Posts: 3

Rep: Reputation: 0
Smile 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.
 
Old 10-10-2008, 06:58 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
 
Old 10-11-2008, 04:32 AM   #3
mland
LQ Newbie
 
Registered: Oct 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Smile 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.
 
Old 10-11-2008, 05:41 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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
...
 
Old 10-11-2008, 10:37 AM   #5
mland
LQ Newbie
 
Registered: Oct 2008
Posts: 3

Original Poster
Rep: Reputation: 0
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.
 
Old 10-11-2008, 10:45 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
tftpd-hpa on Ubuntu 6.10 - Error code 1: File not found jim0112 Ubuntu 3 08-25-2009 06:19 PM
header file: linux/netfilter.h code error goforce Ubuntu 2 10-30-2007 11:12 PM
header file: linux/netfilter.h code error goforce Programming 1 10-30-2007 03:24 AM
Combines 16000 files into 1 single file > error tb: /bin/cat: Argument list too long guanyu Linux - General 4 02-09-2007 12:33 AM
Calling long BASH code within a C program Linh Programming 4 06-24-2003 01:41 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration