LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   List of directories in a file (http://www.linuxquestions.org/questions/linux-general-1/list-of-directories-in-a-file-872759/)

TheBigH 04-03-2011 05:18 PM

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.

smoker 04-03-2011 05:49 PM

Check the contents of directories.dat

What is the working directory of the command ?

TheBigH 04-03-2011 06: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
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?

TheBigH 04-03-2011 06: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 07: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:
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.

TheBigH 04-03-2011 07:37 PM

Getting rid of the * fixed the problem. Thanks.

MTK358 04-04-2011 09:43 AM

Mark the thread as solved.


All times are GMT -5. The time now is 05:44 PM.