LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-11-2020, 06:55 PM   #1
rac8006
Member
 
Registered: Oct 2014
Posts: 39

Rep: Reputation: Disabled
Handling space in parameters


I've searched every where I can think of and can't find an answer. I'm trying to find all of the directories in a partition and count the files. The problem that I'm having is that some of the directory names have a space in them. I did the following:
find . -maxdepth 1 -type d -print > dirs
then I wanted to do for i in `cat dirs`
do
echo -n $i
find "$i" -print|wc
done

I tried several different things but none of the worked.

What I finally got to work was
find . -maxdepth 1 -type d -exec sh myscript.sh {} \;

myscript.sh
echo -n "$1"
find "$@" -print|wc

Is there a better way to do this?

Thanks
 
Old 11-11-2020, 09:29 PM   #2
sgosnell
Senior Member
 
Registered: Jan 2008
Location: Baja Oklahoma
Distribution: Debian Stable and Unstable
Posts: 1,943

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
Have you tried just
Code:
find . -type f | wc -l
replacing the . with the path you want to search?

Or do you need the number in each directory? The above only gives a grand total. If you want the number of files and directories, use
Code:
tree -a
which is probably faster and easier. It gives a graphic tree structure if you need it, with a total number of directories and files at the end. If you really need the number of files in each individual directory, I've never tried that.

Last edited by sgosnell; 11-11-2020 at 09:40 PM.
 
Old 11-11-2020, 11:46 PM   #3
rac8006
Member
 
Registered: Oct 2014
Posts: 39

Original Poster
Rep: Reputation: Disabled
I want the number of files in the top level directories. If I had a partition with a b c d directories. The output would look like this
a 3
b 10
c 20
d 15

I could do this with a command like
for i in `find . -maxdepth 1 -type d -print`
echo -n "$i"
find "$i" -print|wc
done

The problem comes when the directories on the partition are like this "My backup" "My Videos" "My Documents" test ffp
when running the above script I would get errors of
My no such file My
backup no such file
My no such file
and so on. because it treats My Backup as two parameters
I don't think tree -a does this. Secondly I have almost 500,000 files on the partition.
The question was how to pass the directory that has spaces in the name properly?
 
Old 11-12-2020, 07:31 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,853

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
first of all read man find, you will see -print0 for example.
also you might want to use a while cycle instead of for.

finally it is already answered for example here: https://stackoverflow.com/questions/...espace-in-bash
 
Old 11-12-2020, 10:34 AM   #5
rac8006
Member
 
Registered: Oct 2014
Posts: 39

Original Poster
Rep: Reputation: Disabled
With your information I have found three methods that do what I want.
find -maxdepth 1 -type d -print0|while read -d $'\0' file; do echo -n "$file";find "$file" -print|wc; done
find . -maxdepth 1 -type d \( ! -name . \) -print0|xargs -0 -n1 sh -c 'echo -n $1;find "$1" -print|wc' sh
find . -maxdepth 1 -type d \( ! -name . \) -exec /Volume_2/myscript.sh {} \;

myscript.sh
echo -n "$1"
find "$@" -print|wc
 
Old 11-12-2020, 01:53 PM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,796

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Code:
for i in `find . -maxdepth 1 -mindepth 1  -type d -print`
is problematic because the list is split on IFS that defaults to space,tab,newline
You can improve it by setting IFS to newline only
Code:
oIFS=$IFS
IFS="
"
for i in `find . -maxdepth 1 -mindepth 1  -type d -print`
I would rather go for read that by default reads one line i.e. at newline boundary. Still you should set IFS to preserve a leading space in a filename.
Code:
find . -maxdepth 1 -mindepth 1  -type d -print |
while IFS= read -r dir
do
  find "$dir" -print | wc
done
This will only fail if a filename contains a newline (quite unusual but allowed).
The split on globs can even handle newline characters.
Code:
for dir in */  .[!.]*/
do
  [ -d "$dir" ] &&
  find "$dir" -print | wc
done
The trailing / in a glob already ensures directories, so the following test is only for the no-match case.
In bash you can alternatively do
Code:
shopt -s nullglob
find */ .[!.]*/ -print | wc
Because no test is needed and because find handles multiple start directories there is no need for a loop
Last but not least, bash has another option
Code:
shopt -s nullglob dotglob
find */ -print | wc
Now the * glob includes a .[!.]*

Last edited by MadeInGermany; 11-12-2020 at 02:08 PM.
 
  


Reply



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
edit a file /etc/fstab , what parameters can be changebale and which parameters lenin99 Debian 7 01-24-2017 04:03 AM
[SOLVED] Why there are two separate parameters in /proc/sys/net/ipv4/ for security parameters gprathap1121@gmail.com Linux - Security 4 07-14-2014 12:41 AM
Elegant method for handling command line parameters in a switch block. stf92 General 1 08-29-2013 05:58 PM
Division of Logical Memory Space in to User Space and Kernel Space shreshtha Linux - Newbie 2 01-14-2010 09:59 AM
tuning parameters for handling more traffic bhrugu Linux - Networking 0 09-10-2008 08:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:18 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