LinuxQuestions.org
Help answer threads with 0 replies.
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, 09:24 AM   #16
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749

An alternative solution that does not use find, but rather a shell option and the stat command.
Code:
#!/bin/bash

days=1460
mypath="/Datafolder"

shopt -s globstar

secs=days*24*60*60
now=$(date +%s)  # Time now in seconds since epoch
i=1

for f in $mypath/**/*; do
  d=$(stat -c %Z "$f")  # Time in seconds since epoch of last status change
  if ((now - d > secs)); then
    a[i]=$f
    ((i++))
  fi
done

for ((i=1; i<=${#a[@]}; i++)); do
  echo "${a[i]}"
done

Last edited by allend; 06-12-2019 at 09:43 AM.
 
Old 06-12-2019, 09:25 AM   #17
LinuGeek
Member
 
Registered: Jun 2008
Posts: 126

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by BW-userx View Post
BUTT's... change distros, j/k


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"
echo "${fileArray[@]}"
echo "amount"
echo "${#fileArray[@]}"
if you got files with spaces within the file names you need to have double quotes around the string to separate the elements or keep them in order, how ever you look at it.
Code:
arrayWithSpaces=( "here is a space" "here is second space")
to get the elements and keep them in order when you access them.
Code:
fileArray+=('"'$f'"')
adds the double quotes to "file with spaces in it".
Actually adds quotes to everything. You might have to remove the quotes down the line to deal with it, which should not be a big deal.
I appreciate your efforts. Thank you. This is a nice trick you have written in that, it first collects the whole bunch of filenames in array named fileArray. And then in the end , you simple echo the contents of it all at once. My original script was also collecting the list and was able to print the whole contents of an Array. What I am seeking is , to be able to work on the individual contents of this array in a loop or so.
For example once the array has the list of older directories/files, I need to check this list , one by one, if the directories contain certain type of files. Or if the files pertaining to certain department.
For this to achieve basically I wish to create an Array and then iterate through the contents of this Array one by one using loop. Which is not working as the filenames contain multiple spaces.
 
Old 06-12-2019, 09:27 AM   #18
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
so write that script, use shellcheck to fix array related problems and it will work. Or post what you have tried and we will help you to fix.
 
Old 06-12-2019, 10:07 AM   #19
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,699

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
See post #1...
Here is one solution.

Code:
array=()
while IFS=  read -r -d $'\0'; do
    array+=("$REPLY")
done < <(find /Datafolder/ -type f -ctime +1460 -print0)
REPLY is just the default variable for the read command.

From the find man pages.
Quote:
-print0
True; print the full file name on the standard output, followed by a null character (instead of the newline character that -print uses). This allows file names that contain newlines or other types of white space to be correctly interpreted by programs that process the find output.

Last edited by michaelk; 06-12-2019 at 10:13 AM.
 
Old 06-12-2019, 10:17 AM   #20
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
I appreciate your efforts. Thank you. This is a nice trick you have written in that, it first collects the whole bunch of filenames in array named fileArray. And then in the end , you simple echo the contents of it all at once. My original script was also collecting the list and was able to print the whole contents of an Array. What I am seeking is , to be able to work on the individual contents of this array in a loop or so.
For example once the array has the list of older directories/files, I need to check this list , one by one, if the directories contain certain type of files. Or if the files pertaining to certain department.
For this to achieve basically I wish to create an Array and then iterate through the contents of this Array one by one using loop. Which is not working as the filenames contain multiple spaces.
also disregard the adding quotes to keep it in elements, I update that post, just reference it now as it is. that fixes the spaces in the file names issue. I was over thinking it, that is what was only needed.
Code:
array+=("$f")
keeps it in line.
and as with michaelk solution to adding to an array, to then check what you're looking for is, I assume you know what you're doing there, yes?
 
Old 06-12-2019, 11:01 AM   #21
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,699

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
In addition, I do not know of a single line solution except for mapfile as posted which also why I have a loop. If you only need to iterate through the list once then an array isn't necessary.
 
Old 06-12-2019, 10:41 PM   #22
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
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.
you can add a counter to that loop and assign to an array:
Code:
#!/bin/bash

echo "Checking for older files now..." >>/tmp/list
array=()
count=0
find /Datafolder/ -type f -ctime +1460 | while read i
        do
                echo Files are "$i" >> /tmp/list
                array[$((count++))]="$i"
        done
(not tested. esp. not on bash 3.1)

I thought Turbocapitalist' solution was more elegant though.
Quote:
Originally Posted by LinuGeek View Post
Sorry mapfile on SuSE 10 SP4 does not exist.
SuSE 10 is not supported anymore. Hasn't been for 3 years!
 
Old 06-12-2019, 11:01 PM   #23
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,306
Blog Entries: 3

Rep: Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720
Quote:
Originally Posted by ondoho View Post
I thought Turbocapitalist' solution was more elegant though.
Thanks but it's just a slight paraphrasing of grail's solution to a similar question I had the other week. I hadn't even seen mapfile before that. It's not in the POSIX shell that I normally script in.
 
Old 06-13-2019, 07:34 PM   #24
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,617

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
Quote:
Originally Posted by LinuGeek View Post
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
Thank you, that was exactly the information we needed to understand WHY it was not working for you.
The mapfile internal was added in version 4.x of bash. The "which" line would never find an internal bash command, because it does not exist out on the file system, it is withoin the bash executable in version 4 and up.

I have not used BASH 3 in a long time. I remember using some array based solutions, but not how I did it. If someone recalls the way, that may be faster than waiting for me to do the research.

OR, you could install a more modern BASH version if that is an option for you.



Could I ask why you are using 10 SP4? Both version 12 and version 15 use newer versions of BASH. Version 10 is almost ancient by current standards.

Last edited by wpeckham; 06-13-2019 at 07:41 PM.
 
Old 06-13-2019, 09:02 PM   #25
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
just to show what about mapfile that I just tested. Because I wanted to see if there was a way to use it with more than one array.

just the snippet of my script to show the filling of the arrays using mapfile.
Code:
#!/bin/bash
...
source1=/path
source2=/path

#function to fill arrays 
loadList()
{                   #array names
	mapfile -t theList < <(find $source1 -type f | shuf)
	mapfile -t snitchList < <(find $source2 -type f | shuf)
}
fills the arrays without the use of a loop.

Last edited by BW-userx; 06-20-2019 at 07:05 AM.
 
Old 06-20-2019, 05:00 AM   #26
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
An array is important only if you want to treat certain elements in a special way.
The =( ) and the for loop's list split on IFS, while the latter has got the "${array[@]}" escape that you already applied in your post#1.
Code:
#!/bin/bash

echo "Checking for older files now..." >>/tmp/list
oIFS=$IFS; IFS=
tree_file=(`find /Datafolder/ -type f -ctime +1460 -exec echo '{}' +`)
IFS=$oIFS
tmplist=/tmp/list #world-writable=unsafe directory
        for i in "${tree_file[@]}"
        do
                echo Files are "$i"
        done >$tmplist
 
  


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 10:44 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