LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-26-2017, 10:28 AM   #1
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311
Blog Entries: 2

Rep: Reputation: 16
Question 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?
 
Old 06-26-2017, 10:34 AM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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
 
1 members found this post helpful.
Old 06-26-2017, 10:46 AM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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

Last edited by BW-userx; 06-26-2017 at 11:24 AM.
 
1 members found this post helpful.
Old 06-26-2017, 11:06 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
@OP: What do you actually want to accomplish? What is your real goal?
 
1 members found this post helpful.
Old 06-26-2017, 12:22 PM   #5
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
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).
 
2 members found this post helpful.
Old 06-26-2017, 12:34 PM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by andrew.comly View Post
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?
 
Old 06-26-2017, 12:47 PM   #7
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by andrew.comly View Post
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.

Last edited by Habitual; 06-26-2017 at 12:51 PM.
 
1 members found this post helpful.
Old 06-26-2017, 12:48 PM   #8
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Quote:
Originally Posted by rtmistler View Post
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...
 
1 members found this post helpful.
Old 06-26-2017, 12:56 PM   #9
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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 ⚡>
 
2 members found this post helpful.
Old 06-27-2017, 09:17 PM   #10
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Wink Purpose

Quote:
Originally Posted by NevemTeve View Post
@OP: What do you actually want to accomplish? What is your real goal?
This post describes the purpose.
 
1 members found this post helpful.
Old 06-27-2017, 09:25 PM   #11
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Arrow previously attempted

Quote:
Originally Posted by rtmistler View Post
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).
 
Old 06-27-2017, 09:33 PM   #12
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Talking thanks!!

Quote:
Originally Posted by Laserbeak View Post
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.
 
Old 06-27-2017, 09:38 PM   #13
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Exclamation actually the spaces do effect something else

Quote:
Originally Posted by Laserbeak View Post
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.
 
Old 06-27-2017, 10:03 PM   #14
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,780

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
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.
 
2 members found this post helpful.
Old 06-27-2017, 11:45 PM   #15
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Quote:
Originally Posted by andrew.comly View Post
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
 
1 members found this post helpful.
  


Reply

Tags
directories, find, list, spaces



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] I want to extract names with spaces from a list of names! linustalman Linux - General 5 12-02-2011 09:42 AM
[SOLVED] Make list of directories which contain files other than subdirs zainka Programming 9 10-21-2009 04:20 AM
referencing directories with spaces in the names... bbneo Programming 7 06-23-2009 12:15 PM
Is there a way to make certain directories list on top of others with Konqueror? Cinematography Linux - Desktop 2 10-22-2007 10:22 AM
changing subject title names doesn't work on forums A6Quattro LQ Suggestions & Feedback 2 10-17-2005 12:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration