LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Subject: Can't make a list of Directories since some names contain spaces. (https://www.linuxquestions.org/questions/programming-9/subject-cant-make-a-list-of-directories-since-some-names-contain-spaces-4175608650/)

andrew.comly 06-26-2017 10:28 AM

Subject: Can't make a list of Directories since some names contain spaces.
 
I would like to make a list of all directories in a parent directory.


Code:

mkdir -p ee/{"unit 1","unit 2","unit 3","unit 4"}
$ tree -Ra ee
├── unit 1
├── unit 2
├── unit 3
└── unit 4
4 directories, 0 files

$ find ee/* -type d | wc -l
4

At this point everybody can see there are only 4 directories.

So I now attempt to make a list of these directories whose names contain spaces.

Attempt01:
Code:

$ DIRList=$(find ee/* -type d)

$ echo $DIRList
ee/unit 1 ee/unit 2 ee/unit 3 ee/unit 4

$ echo $DIRList | wc -w
8

$ echo $DIRList | wc -l
1

Unfortunately this list splits the directory names up by the spaces they contain.

Attempt02:
Code:

ls -1a ee
results in 6 directories, not 4, and "ee/*" results in 15 directories

Attempt03:
Code:

$ ls -d ee/*
ee/unit 1  ee/unit 2  ee/unit 3  ee/unit 4
$ echo $aList | wc -w
8

As you can see by the "8", same problem as attempt01

Attempt04:
Code:

ls -l ee/ | grep ^d
drwxrwxr-x 2 a a 4096  6月 26 22:19 unit 1
drwxrwxr-x 2 a a 4096  6月 26 22:19 unit 2
drwxrwxr-x 2 a a 4096  6月 26 22:19 unit 3
drwxrwxr-x 2 a a 4096  6月 26 22:19 unit 4
$ ls -l ee/ | grep ^d | awk '{for(i=9;i<NF;i++)printf$i}'
unitunitunitunit

Even awk cuts the parts of the DIR name off after the space.

Is there anyway of making a list of all DIR, including ones whose DIR names include spaces?

suicidaleggroll 06-26-2017 10:34 AM

Lots of options

1) Don't store the output of ls in an intermediate variable, just pipe it straight to wc
2) Use the "-1" flag in ls
3) Use the print0 flag in find to print out the names with null terminators, then use the files0-from flag in wc to read it properly

I'm sure there are many more

BW-userx 06-26-2017 10:46 AM

You know wc -w is times 2 because of split dir names so just do the math ;)
Code:

userx%voider ⚡ testDIR ⚡> echo $(($(ls ./ee | wc -w ) / 2))
4

or
Code:

userx%voider ⚡ testDIR ⚡> dirlist=$(($(find ./ee -type d | wc -w) / 2 ))
userx%voider ⚡ testDIR ⚡> echo $dirlist
4

or
Code:

userx%voider ⚡ testDIR ⚡> dirlist=$(find ./ee -type d)
userx%voider ⚡ testDIR ⚡> echo $(($(echo $dirlist | wc -w) / 2))
4

Code:

good morning userx
userx%voider ⚡ ~ ⚡> mkdir testDIR ; cd testDIR
userx%voider ⚡ testDIR ⚡> mkdir -p ee/{"unit 1","unit 2","unit 3","unit 4"}
userx%voider ⚡ testDIR ⚡> ls
ee
userx%voider ⚡ testDIR ⚡> ls ee
'unit 1'  'unit 2'  'unit 3'  'unit 4'
userx%voider ⚡ testDIR ⚡> ls -la ee
total 24
drwxr-xr-x 6 userx userx 4096 Jun 26 10:43  .
drwxr-xr-x 3 userx userx 4096 Jun 26 10:43  ..
drwxr-xr-x 2 userx userx 4096 Jun 26 10:43 'unit 1'
drwxr-xr-x 2 userx userx 4096 Jun 26 10:43 'unit 2'
drwxr-xr-x 2 userx userx 4096 Jun 26 10:43 'unit 3'
drwxr-xr-x 2 userx userx 4096 Jun 26 10:43 'unit 4'
userx%voider ⚡ testDIR ⚡> dirlist=$(find ./ee -type d)
userx%voider ⚡ testDIR ⚡> echo $dirlist
./ee ./ee/unit 1 ./ee/unit 2 ./ee/unit 3 ./ee/unit 4
userx%voider ⚡ testDIR ⚡>

this here giving 8
Code:

$ DIRList=$(find ee/* -type d)

$ echo $DIRList
ee/unit 1 ee/unit 2 ee/unit 3 ee/unit 4
  1    2    3  4    5    6    7  8

$ echo $DIRList | wc -w
8

$ echo $DIRList | wc -l
1

wc - l is one line = correct

NevemTeve 06-26-2017 11:06 AM

@OP: What do you actually want to accomplish? What is your real goal?

Laserbeak 06-26-2017 12:22 PM

If you want an answer in Perl, I'll write one. But I'm not going to waste time to make one if you're just going to say you don't like Perl or something (like so many have to me in the past).

rtmistler 06-26-2017 12:34 PM

Quote:

Originally Posted by andrew.comly (Post 5727357)
I would like to make a list of all directories in a parent directory.
...
Is there anyway of making a list of all DIR, including ones whose DIR names include spaces?

Sorry, but have you tried:
Code:

$ find . -type d
Or am I missing something here?

Habitual 06-26-2017 12:47 PM

Quote:

Originally Posted by andrew.comly (Post 5727357)
Is there anyway of making a list of all DIR, including ones whose DIR names include spaces?

a few actually.
Code:

mkdir -pv ee/{"unit 1","unit 2","unit 3","unit 4"}
is my setup.local to utilize
Code:

find `pwd` -type d -name "unit [1234]"
which spews
Code:

/home/jj/ee/unit 4
/home/jj/ee/unit 2
/home/jj/ee/unit 3
/home/jj/ee/unit 1

Code:

dirlist=$(find `pwd` -type d -name "unit [1234]")
and then
Code:

echo "$dirlist"
/home/jj/ee/unit 4
/home/jj/ee/unit 2
/home/jj/ee/unit 3
/home/jj/ee/unit 1

|wc -l
4 here.

See also http://mywiki.wooledge.org/BashPitfalls.

Laserbeak 06-26-2017 12:48 PM

Quote:

Originally Posted by rtmistler (Post 5727397)
Sorry, but have you tried:
Code:

$ find . -type d
Or am I missing something here?

LOL, yes that should work to just count the number of directories, just pipe that to wc -l:

Code:

find parentdir -type d | wc -l
Spaces should have no effect.

I get the feeling he may end up wanting more done though...

BW-userx 06-26-2017 12:56 PM

TO Expand on @Habitual line of thought,
if you do not know the names of the said dir.
Code:

userx%slackwhere ⚡ testDIR ⚡> DL=$(find `pwd` -type d -name "[A-Za-z]* [0-9]")
userx%slackwhere ⚡ testDIR ⚡> echo $DL
/home/userx/testDIR/ee/unit 1
/home/userx/testDIR/ee/unit 2
/home/userx/testDIR/ee/unit 3
/home/userx/testDIR/ee/unit 4
userx%slackwhere ⚡ testDIR ⚡>


andrew.comly 06-27-2017 09:17 PM

Purpose
 
Quote:

Originally Posted by NevemTeve (Post 5727373)
@OP: What do you actually want to accomplish? What is your real goal?

This post describes the purpose.

andrew.comly 06-27-2017 09:25 PM

previously attempted
 
Quote:

Originally Posted by rtmistler (Post 5727397)
Sorry, but have you tried:
Code:

$ find . -type d
Or am I missing something here?

Yes, find . -type d was previously attempted. Also List=$(find . -type d) was previously attempted, and due to some dir/file names containing spaces, the list is broken (application).

andrew.comly 06-27-2017 09:33 PM

thanks!!
 
Quote:

Originally Posted by Laserbeak (Post 5727395)
If you want an answer in Perl, I'll write one. But I'm not going to waste time to make one if you're just going to say you don't like Perl or something (like so many have to me in the past).

I really do appreciate your team spirit, and hope to be able to help you sometime in the future. The only problem is that I am just an independent bash programming student, and haven't even learned how a shellscript can call a perl script yet. This doesn't mean that I don't regard perl as an outstanding language to learn, it's just that it might distract me in my current stage of development. This specific question is no emergency.

andrew.comly 06-27-2017 09:38 PM

actually the spaces do effect something else
 
Quote:

Originally Posted by Laserbeak (Post 5727402)
LOL, yes that should work to just count the number of directories, just pipe that to wc -l:

Code:

find parentdir -type d | wc -l
Spaces should have no effect.

I get the feeling he may end up wanting more done though...

Your right when only counting the amount of lines(how many directories).

Unfortunately, when making a "list", the spaces for this purpose, the spaces end up breaking the list up.

rknichols 06-27-2017 10:03 PM

It's a lot easier just to let the find command do the work an not have to worry about how the shell will parse the resulting list.
Code:

find AC -type d -exec chmod 700 {} \;
find AC -type f -exec chmod 600 {} +

The first command tells find to execute the chmod command immediately on each directory it discovers since it can't descend into a directory until the appropriate permission has been set. The second command, ending with "+" instead of "\;" allows find to pass as many file names as it can to each invocation of chmod.

NevemTeve 06-27-2017 11:45 PM

Quote:

Originally Posted by andrew.comly (Post 5728097)
This post describes the purpose.

If you mean you want to perform chmod on more than one files/directories, then it is an XY-problem
use find(1): http://www.linuxquestions.org/questi...0/#post5707388


All times are GMT -5. The time now is 11:59 PM.