-   Linux - General (
-   -   List of directories in a file (

TheBigH 04-03-2011 06:18 PM

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?


smoker 04-03-2011 06:49 PM

Check the contents of directories.dat

What is the working directory of the command ?

TheBigH 04-03-2011 07:10 PM

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?

TheBigH 04-03-2011 07:52 PM

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?

carltm 04-03-2011 08:28 PM

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.

TheBigH 04-03-2011 08:37 PM

Getting rid of the * fixed the problem. Thanks.

MTK358 04-04-2011 10:43 AM

Mark the thread as solved.

All times are GMT -5. The time now is 10:16 AM.