2014-09-02 376 views
0

我有两个目录'dir1'和'dir2',我想输出这两个目录的所有内容。所以,我使用bash命令'xargs'仅通过一个命令行来创建它。命令是“echo'1 2'| xargs -d''-I @ -t sh -c'cat dir @/*'”,但结果很奇怪。关于xargs和通配符的技巧

[[email protected] /data1/walter]$ echo '1 2' | xargs -d ' ' -I @ -t sh -c 'cat [email protected]/*' 
sh -c cat dir1/* <<<< This output is normal 
1     <<<< This is all content in dir1 
sh -c cat dir2  
/*     <<<< these two lines are odd, it looks like xargs insert a '\n' between 'dir2' and '/*' 
cat: dir2: Is a directory <<<< as a result, 'cat' can't receive argument 'dir2' 
sh: line 1: /bin: is a directory <<<< and command line '/*' is error 
+0

尝试使用find dir *。你可以访问find的man页面来控制/格式化你想要的输出方式。 – Pratham 2014-09-02 12:48:57

回答

3

echo自动附加一个新行至其输出,因此xargs越来越两个值来替代@12\n。一个直接的解决方法是使用

echo -n '1 2' | xargs ... 

禁用换行或(更便携)

printf '1 2' | xargs ... 

然而,正如其他人所指出的那样,使用xargs这可能是更复杂得多,它需要的。

0

也许我错过了你的问题的东西,但你可以简单地通过这两个目录来ls

ls dir{1,2}/* 
0

你可以简单地指定:

ls dir1/* dir2/* 

这可以使用brace expansion简化两个目录:

ls dir1 dir2 

或许

find dir1 dir2 
如果你想要一个更复杂的解决方案

(即在多个目录中找到)