LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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: Slackware-current
Posts: 5,192

Rep: Reputation: 1853Reputation: 1853Reputation: 1853Reputation: 1853Reputation: 1853Reputation: 1853Reputation: 1853Reputation: 1853Reputation: 1853Reputation: 1853Reputation: 1853

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: 77

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 Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 12,699

Rep: Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981
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: 18,594

Rep: Reputation: 2700Reputation: 2700Reputation: 2700Reputation: 2700Reputation: 2700Reputation: 2700Reputation: 2700Reputation: 2700Reputation: 2700Reputation: 2700Reputation: 2700
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 14.2 current / ArcoLinux / Void Linux
Posts: 8,647

Rep: Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797
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: 18,594

Rep: Reputation: 2700Reputation: 2700Reputation: 2700Reputation: 2700Reputation: 2700Reputation: 2700Reputation: 2700Reputation: 2700Reputation: 2700Reputation: 2700Reputation: 2700
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: 11,658
Blog Entries: 9

Rep: Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097
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
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 3,962
Blog Entries: 3

Rep: Reputation: 1889Reputation: 1889Reputation: 1889Reputation: 1889Reputation: 1889Reputation: 1889Reputation: 1889Reputation: 1889Reputation: 1889Reputation: 1889Reputation: 1889
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
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 2,917

Rep: Reputation: 1258Reputation: 1258Reputation: 1258Reputation: 1258Reputation: 1258Reputation: 1258Reputation: 1258Reputation: 1258Reputation: 1258
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 14.2 current / ArcoLinux / Void Linux
Posts: 8,647

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

#array for files
theList=()
snitchList=()

#function to fill array with paths to images
loadList()
{
	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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 07:01 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration