LinuxQuestions.org
Visit Jeremy's Blog.
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 11-02-2006, 05:18 AM   #1
venusian
LQ Newbie
 
Registered: Mar 2005
Distribution: RedHat
Posts: 10

Rep: Reputation: 0
Arguments too long with ls command


hi,

i'm using this command

ls xyz*

to print all files starting with xyz. but i get the message "Arguments too long".

Can any one tell me the reason i'm getting it and how can i get my directory listing displayed.

Waiting for reply
Thanking in Advance
 
Old 11-02-2006, 05:56 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Due to shell globbing, the command line is expanded with all the files matching the pattern, such as "ls xyz1 xyz2 xyz3 xyz4 ..."
If there are too many files matching the pattern, the shell runs out of memory.
As an alternative you can use:
find ./ -maxdepth 1 -name "xyz*"

The same problem can occur in a for loop like this:
for file in xyz*; do
command(s)
done

For this you can use find and xargs:
find ./ -maxdepth 1 -name "xyz*" -print0 | xargs -0 -L 200 <command>
 
Old 11-02-2006, 06:21 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
When you enter this command, the shell tries to expand the pattern xyz*, and then pass the resulting expanded list to ls as arguments. There is a limit to the length of the list of parameters which may be passed to a program, although it is pretty big. See here for more information.

I suspect you have a very large number of files which match the pattern xyz*, exceeding this maximum.

The limitation is in bash itself. Don't worry though, you can use other programs to work around this problem. For example, this command should do what you want:

Code:
find . -maxdepth 1 -name 'xyz*' -print0 |xargs -r -0 ls
It's ugly, but it works.
 
Old 04-27-2007, 06:09 AM   #4
venusian
LQ Newbie
 
Registered: Mar 2005
Distribution: RedHat
Posts: 10

Original Poster
Rep: Reputation: 0
Hi,

I'm trying to compress the files starting with the String "xyz", when i gave the following command, the system again gave me the message "Arguments Too Long" .

tar -cvf My_Files.tar xyz*

So i used the following command instead

tar -cvf My_Files.tar find ./ -name "xyz"

The problem with this is that it copies all the files present in the current directory and compresses them.

Can anyone please tell me how i can modify the command so that it only includes those files which match the string.

Regards,
 
Old 04-27-2007, 02:20 PM   #5
indeliblestamp
Member
 
Registered: Feb 2006
Distribution: Fedora
Posts: 341
Blog Entries: 3

Rep: Reputation: 40
Quote:
Originally Posted by venusian
So i used the following command instead

tar -cvf My_Files.tar find ./ -name "xyz"

The problem with this is that it copies all the files present in the current directory and compresses them.
Hi,
You need to enclose the find command in backticks. Its output will then be passed to the tar command. Try this:
Code:
tar cvf My_Files.tar `find . -maxdepth 1 -name "xyz*"`
 
Old 04-27-2007, 05:48 PM   #6
Tap-Out
Member
 
Registered: Oct 2002
Location: Halifax, NS
Distribution: Ubuntu, Mepis, Debian
Posts: 130

Rep: Reputation: 15
would doing a
Code:
ls | grep xyz
not do the same thing?
 
  


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
List of arguments too long, need to delete 59,000 files stefaandk Linux - General 4 07-12-2006 02:14 AM
command line arguments containing ( Lotharster Linux - Newbie 3 01-05-2006 08:43 AM
command arguments in perl djgerbavore Programming 1 12-30-2004 08:06 AM
see what arguments are available for a command naijaguy Linux - Newbie 3 08-12-2004 10:05 PM
Help with command line arguments ? synapse Linux - Newbie 2 02-23-2004 02:25 AM

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

All times are GMT -5. The time now is 04:28 AM.

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