开发者

Pipe ls output to get path of all directories

开发者 https://www.devze.com 2023-04-12 15:18 出处:网络
I want to list all directories ls -d * in the current directory and list out all their full paths. I know I need to pipe the output to something, but just not sure what. I don\'t know if I can pipe th

I want to list all directories ls -d * in the current directory and list out all their full paths. I know I need to pipe the output to something, but just not sure what. I don't know if I can pipe the output to a 开发者_StackOverflow中文版pwd or something.

The desired result would be the following.

$ cd /home/
$ ls -d *|<unknown>
/home/Directory 1
/home/Directory 2
/home/Directory 3

<unknown> being the part which needs to pipe to pwd or something.

My overall goal is to create a script which will allow to me construct a command for each full path supplied to it. I'll type build and internally it will run the following command for each.

cd <full directory path>; JAVA_HOME=jdk/Contents/Home "/maven/bin/mvn" clean install


Try simply:

$ ls -d $PWD/*/

Or

$ ls -d /your/path/*/


find `pwd` -type d -maxdepth 1 -name [^.]\*

Note: The above command works in bash or sh, not in csh. (Bash is the default shell in linux and MacOSX.)


ls -d $PWD/* | xargs -I{} echo 'cd {} JAVA_HOME=jdk/Contents/Home "/maven/bin/mvn" clean install' >> /foo/bar/buildscript.sh

will generate the script for u.


Might I also suggest using -- within your ls construct, so that ls -d $PWD/*/ becomes ls -d -- $PWD/*/ (with an extra -- inserted)? This will help with those instances where a directory or filename starts with the - character:

/home/-dir_with_leading_hyphen/
/home/normal_dir/

In this instance, ls -d */ results in:

ls: illegal option -- -
usage: ls [-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file ...]

However, ls -d -- */ will result in:

-dir_with_leading_hyphen/      normal_dir/

And then, of course, you can use the script indicated above (so long as you include the -- any time you call ls).


No piping neccessary:

find $(pwd) -maxdepth 1 -type d -printf "%H/%f\n"

to my surprise, a command in the print statement works too:

find -maxdepth 1 -type d -printf "$(pwd)/%f\n"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号