LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-03-2011, 05:18 PM   #1
TheBigH
LQ Newbie
 
Registered: Dec 2009
Posts: 14

Rep: Reputation: 0
List of directories in a file


Hi,
I have generated a list of directories that I would like to use ls and grep on, but it is not working. I am using the command
Code:
cat directories.dat | xargs ls
and I get a whole lot of these errors:
Code:
ls: cannot access ./foo/bar/baz/grault/*: No such file or directory
but when I try the directories manually one at a time I find that they all exist and all have files in them. Same thing if I try to grep anything. What is going wrong?

Cheers,
H.
 
Old 04-03-2011, 05:49 PM   #2
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
Check the contents of directories.dat

What is the working directory of the command ?
 
Old 04-03-2011, 06:10 PM   #3
TheBigH
LQ Newbie
 
Registered: Dec 2009
Posts: 14

Original Poster
Rep: Reputation: 0
The contents of the file and the working directory are fine. Just to clarify, if I run my xargs piped command I get
Code:
ls: cannot access ./foo/bar/baz/grault/*: No such file or directory
and lots more errors of the same sort, but then (without changing my working directory) if I type
Code:
ls ./foo/bar/baz/grault/*
it works just fine. I can't work out why. I've also tried changing the contents of the file so that the directories are given in full rather than with the ./ prefix, but that doesn't help.

(edit) I created the file using gawk. Could this have added some invisible delimiters or something that ls is choking on?

Last edited by TheBigH; 04-03-2011 at 06:12 PM. Reason: a thought
 
Old 04-03-2011, 06:52 PM   #4
TheBigH
LQ Newbie
 
Registered: Dec 2009
Posts: 14

Original Poster
Rep: Reputation: 0
Okay, I think I've figured out what it's doing.

xargs is returning the arguments with double quotes around them, and ls doesn't like that. How do I stop this from happening?
 
Old 04-03-2011, 07:28 PM   #5
carltm
Member
 
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 703

Rep: Reputation: 99
A couple of thoughts.

Why not use a loop instead of xargs? The xargs has some limitations
that you won't experience using a loop. For example:
Code:
cat directories.dat | while read Dir; do ls "$Dir"; done
Also you don't mention anything about special characters. The * is
interpreted and possibly expanded into a set of files. If you have
space characters, tabs, slashes, back-slashes, question marks or
ampersands in any of the paths they will also be interpreted.
 
1 members found this post helpful.
Old 04-03-2011, 07:37 PM   #6
TheBigH
LQ Newbie
 
Registered: Dec 2009
Posts: 14

Original Poster
Rep: Reputation: 0
Getting rid of the * fixed the problem. Thanks.
 
Old 04-04-2011, 09:43 AM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Mark the thread as solved.
 
  


Reply

Tags
directory, grep, ls, xargs



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to list directories only pinga123 Linux - Newbie 10 10-27-2009 12:00 AM
How to list the directories of a file system by their sizes? joaotelles Solaris / OpenSolaris 3 12-04-2008 02:11 PM
list sub-directories only CowboyJ Linux - Newbie 1 12-04-2003 11:42 PM
Asking LS for a list of directories only gary knott Linux - Newbie 1 11-12-2003 05:49 PM
list of directories greg108 Linux - Newbie 5 08-08-2003 03:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:43 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