Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - General
User Name
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.


  Search this Thread
Old 04-03-2011, 06:18 PM   #1
LQ Newbie
Registered: Dec 2009
Posts: 12

Rep: Reputation: 0
List of directories in a file

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
cat directories.dat | xargs ls
and I get a whole lot of these errors:
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?

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

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

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

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
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
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 07:12 PM. Reason: a thought
Old 04-03-2011, 07:52 PM   #4
LQ Newbie
Registered: Dec 2009
Posts: 12

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, 08:28 PM   #5
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 703

Rep: Reputation: 97
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:
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, 08:37 PM   #6
LQ Newbie
Registered: Dec 2009
Posts: 12

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

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


directory, grep, ls, xargs

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 01:00 AM
How to list the directories of a file system by their sizes? joaotelles Solaris / OpenSolaris 3 12-04-2008 03:11 PM
list sub-directories only CowboyJ Linux - Newbie 1 12-05-2003 12:42 AM
Asking LS for a list of directories only gary knott Linux - Newbie 1 11-12-2003 06:49 PM
list of directories greg108 Linux - Newbie 5 08-08-2003 04:11 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:12 AM.

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