LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-12-2019, 05:03 AM   #1
LinuGeek
Member
 
Registered: Jun 2008
Posts: 126

Rep: Reputation: 0
Find command to collect the file list having spaces


Hello Experts,

I wish to list the files using arrays. But the filenames are containing many spaces. How do I correctly collect and list the files.
I wrote following script but it does not seem to escape the white spaces.

Code:
#!/bin/bash

echo "Checking for older files now..." >>/tmp/list
tree_file=(`find /Datafolder/ -type f -ctime +1460 -exec echo '{}' +`)

        for i in "${tree_file[@]}"
        do
                echo Files are "$i" >> /tmp/list
        done

After execution the output shows like this,

[QUOTE]
Quote:
Files are /Datafolder/Infos
Files are SZ
Files are 04
Files are 07
Files are Information
Files are Datafolder/BZ
Files are SZ
Files are SZ10

The actual file names are as follows
Quote:
/Datafolder/Infos SZ 04 07
/Datafolder/Infos SZ 04 SZ10
So it seems the white spaces are not getting escaped. What is missing in the script can someone please guide?

Thanx in advance.


Regards,
Admin
 
Old 06-12-2019, 05:47 AM   #2
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,627

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
Your problem is the expansion in the 'for i in' line, where the values stored in the variable are exanded to SPACE DELIMITED WORDS!

Instead of looping through a variable, have find do the printing. Check the man page and play with the -printf option and reduce your script to two lines of code.
 
Old 06-12-2019, 05:47 AM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Code:
#!/bin/bash

echo "Checking for older files now..." >>/tmp/list
find /Datafolder/ -type f -ctime +1460 | while read i
        do
                echo Files are "$i" >> /tmp/list
        done
 
Old 06-12-2019, 05:48 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
I ran into something like this the other day. grail suggested bash's mapfile function.

Code:
#!/bin/bash

mapfile -t tree <<<"$(find /Datafolder/ -type f -ctime +1460 -print)";

IFS=$'\n';
for i in ${tree[*]}; do
        echo Files are "$i" >> /tmp/list;
done;
 
1 members found this post helpful.
Old 06-12-2019, 06:31 AM   #5
LinuGeek
Member
 
Registered: Jun 2008
Posts: 126

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ondoho View Post
Code:
#!/bin/bash

echo "Checking for older files now..." >>/tmp/list
find /Datafolder/ -type f -ctime +1460 | while read i
        do
                echo Files are "$i" >> /tmp/list
        done

Thanks Ondoho for this code. It works. But the Original script is much more complex and we need to collect the output of find in an array to be able to use it further down the script. So Array is important here.
 
Old 06-12-2019, 06:47 AM   #6
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,627

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
Quote:
Originally Posted by LinuGeek View Post
Thanks Ondoho for this code. It works. But the Original script is much more complex and we need to collect the output of find in an array to be able to use it further down the script. So Array is important here.
It would have been helpful had you mentioned this in the original message.
Using an array for this case is an elegant option. Another is a delimited list, but I like the array better.
 
Old 06-12-2019, 06:51 AM   #7
LinuGeek
Member
 
Registered: Jun 2008
Posts: 126

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by wpeckham View Post
It would have been helpful had you mentioned this in the original message.
Using an array for this case is an elegant option. Another is a delimited list, but I like the array better.
Sorry. But with Array we get the mentioned errors. So any suggestion?
 
Old 06-12-2019, 06:58 AM   #8
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,627

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
Quote:
Originally Posted by LinuGeek View Post
Sorry. But with Array we get the mentioned errors. So any suggestion?
In your original script you did NOT use an array. You just captured the find output into a list variable.
The solution by Turbocapitalist uses an array. That solution should serve you well. You can parse over the array as many times as you need.
 
Old 06-12-2019, 07:09 AM   #9
LinuGeek
Member
 
Registered: Jun 2008
Posts: 126

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by wpeckham View Post
In your original script you did NOT use an array. You just captured the find output into a list variable.
The solution by Turbocapitalist uses an array. That solution should serve you well. You can parse over the array as many times as you need.
Sorry mapfile on SuSE 10 SP4 does not exist.
 
Old 06-12-2019, 07:44 AM   #10
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,627

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
Quote:
Originally Posted by LinuGeek View Post
Sorry mapfile on SuSE 10 SP4 does not exist.
Excuse me? See https://www.gnu.org/software/bash/ma...-Builtins.html! The mapfile used is a builtin in bash. Is your bash some non-GNU mutant bash clone? What version of bash are you using, that lacks mapfile?
 
Old 06-12-2019, 07:48 AM   #11
LinuGeek
Member
 
Registered: Jun 2008
Posts: 126

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by wpeckham View Post
Excuse me? See https://www.gnu.org/software/bash/ma...-Builtins.html! The mapfile used is a builtin in bash. Is your bash some non-GNU mutant bash clone? What version of bash are you using, that lacks mapfile?

Following is the status.

Code:
# rpm -qa | grep bash
bash-3.1-24.26.20

# bash -version
GNU bash, version 3.1.17(1)-release (i586-suse-linux)

> mapfile
bash: mapfile: command not found


# which mapfile
which: no mapfile in (/sbin:/usr/sbin:/usr/local/sbin:/opt/gnome/sbin:/root/bin:/usr/local/bin:/usr/bin:/usr/X11R6/bin:/bin:/us
 
Old 06-12-2019, 07:53 AM   #12
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
I got slackwhere, and it says the same.
Code:
$ bash -version
GNU bash, version 5.0.7(1)-release (x86_64-slackware-linux-gnu)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
---------------

userx@slack.curry.org:~
$ which mapfile
which: no mapfile in (/usr/local/bin:/usr/bin:/bin:/usr/games:/usr/lib64/kde4/libexec:/usr/lib64/qt/bin:/usr/lib64/qt5/bin:/home/userx/bin:/sbin:/usr/sbin)
mapfile usages

the mapfile still work though
Code:
userx@slack.curry.org:~
$ printf "Line1\nLine2\nLine3\nLine4\n" | ( mapfile -t; echo "${MAPFILE[@]}" )
Line1 Line2 Line3 Line4

userx@slack.curry.org:~
$ mapfile -t < <(printf "Line1\nLine2\nLine3\nLine4\n")

$ echo "${MAPFILE[@]}"
Line1 Line2 Line3 Line4

$ for ((i=0;i<${#MAPFILE[@]};i++)) ; do echo ${MAPFILE[$i]} ; done
Line1
Line2
Line3
Line4
mapfile is built into BASH so it is not going to show as a external program.

spaces too
Code:
$ mapfile -t < <(printf "Line 1\nLine 2\nLine 3\nLine 4\n")
userx@slack.curry.org:~
$ for ((i=0;i<${#MAPFILE[@]};i++)) ; do echo ${MAPFILE[$i]} ; done
Line 1
Line 2
Line 3
Line 4
application:
Code:
mapfile -t < <(find bin -type f)
results
Code:
$ for ((i=0;i<${#MAPFILE[@]};i++)) ; do echo ${MAPFILE[$i]} ; done
bin/hostname_change
bin/switchgears
bin/testfile
bin/trim
bin/fbmenu
bin/iso2usb
bin/shufimages
bin/ttemp
bin/mntdrives
bin/ranImg
bin/unetbootin-linux64-661.bin
bin/wmmenu
bin/throttle-temp
bin/bbmenu
bin/obmenu
bin/cputemp
bin/upfire
bin/mwmenu
bin/adjm
bin/upslak.sh
bin/CPUHeatSpeed
bin/Eup
bin/mixpic
bin/changebg
bin/startdbus
bin/limitcpu
bin/obshutdown
bin/lightup
bin/shiftgears
bin/tp
bin/ranbg
so yours should be something like this:

Code:
#!/bin/bash
#creates file and adds first line '>'
echo "Checking for older files now..." > /tmp/list
mapfile -t < <(find /Datafolder/ -type f -ctime +1460)
echo "adding to file..."
for ((i=0;i<${#MAPFILE[@]};i++)) ; do echo ${MAPFILE[$i]} >> /tmp/list ; done

Last edited by BW-userx; 06-12-2019 at 08:32 AM.
 
Old 06-12-2019, 08:09 AM   #13
LinuGeek
Member
 
Registered: Jun 2008
Posts: 126

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by BW-userx View Post
I got slackwhere, and it says the same.
Code:
$ bash -version
GNU bash, version 5.0.7(1)-release (x86_64-slackware-linux-gnu)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
---------------

userx@slack.curry.org:~
$ which mapfile
which: no mapfile in (/usr/local/bin:/usr/bin:/bin:/usr/games:/usr/lib64/kde4/libexec:/usr/lib64/qt/bin:/usr/lib64/qt5/bin:/home/userx/bin:/sbin:/usr/sbin)
mapfile usages

the mapfile still work though
Code:
userx@slack.curry.org:~
$ printf "Line1\nLine2\nLine3\nLine4\n" | ( mapfile -t; echo "${MAPFILE[@]}" )
Line1 Line2 Line3 Line4

userx@slack.curry.org:~
$ mapfile -t < <(printf "Line1\nLine2\nLine3\nLine4\n")

$ echo "${MAPFILE[@]}"
Line1 Line2 Line3 Line4
mapfile is built into BASH so it is not going to show as a external program.
For me it does not work though.

Code:
#./findfiles.sh
line 19: mapfile: command not found
 
Old 06-12-2019, 08:46 AM   #14
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
you ought to try shellcheck.net: https://github.com/koalaman/shellcheck/wiki/SC2207
 
Old 06-12-2019, 08:52 AM   #15
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
Quote:
Originally Posted by LinuGeek View Post
For me it does not work though.

Code:
#./findfiles.sh
line 19: mapfile: command not found
BUTT's... change distros, j/k

Quote:
Thanks Ondoho for this code. It works. But the Original script is much more complex and we need to collect the output of find in an array to be able to use it further down the script. So Array is important here.
try this
Code:
#!/bin/bash
      
#declare array for later processing
fileArray=()
#split it to see it starting
echo "finding files..." | tee -a /tmp/MyList

while read f 
do
       #fill array
	fileArray+=("$f")
	#want it in a file and output to screen too?
	#echo "$f" | tee -a /tmp/MyList
	#just put it in a file too?
	#echo "$f" >> /tmp/MyList
done <<<$(find /Datafolder/ -type f -ctime +1460)

echo "here is the array filled"
printf '%s\n' "${fileArray[@]}"
echo "amount"
echo "${#fileArray[@]}"

Last edited by BW-userx; 06-12-2019 at 10:08 AM.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Tabs or spaces? Spaces, obviously, but how many? LXer Syndicated Linux News 0 09-13-2018 09:50 AM
[SOLVED] having problems with spaces in file/folder name with 'for' command steve51184 Programming 17 07-12-2012 08:39 PM
Using bt3 final CD can't collect ap list after using macchanger billyboohi LinuxQuestions.org Member Intro 0 10-19-2009 08:00 AM
Spaces and escaped spaces pslacerda Linux - Newbie 13 12-20-2008 09:03 AM
collect 2: cannot find 'ld' dancindoc Linux - Newbie 1 09-08-2002 01:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:26 PM.

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