TipGiver
Programmer
- Sep 1, 2005
- 1,863
Hello.
Firstly, let me tell you want i want to do. I give a folder (directory -d) as an argument ($1), and i want to know what is the most recent file in every folder. The $1 folder may have many folders inside and files, the sub-folders can have other sub folders and other files, and so on.
So a recursive function is needed.
In the For Loop i check if the 'file' is a folder with the '-d'. The recursion is done in the IF statement. If it is not a directory then i assign the $file to a variable (since is is not a directory) called 'recent'. Then i check all items in the current folder. If the anyfile is a simple file AND it is newer than the previous assigned "recent" then the anyfile is now the recent one.
After the "done" it is supposed to have found the most recent file in the directory, but no!
Here is the code. What is/are wrong ?!
Thanks in advance.
Firstly, let me tell you want i want to do. I give a folder (directory -d) as an argument ($1), and i want to know what is the most recent file in every folder. The $1 folder may have many folders inside and files, the sub-folders can have other sub folders and other files, and so on.
So a recursive function is needed.
In the For Loop i check if the 'file' is a folder with the '-d'. The recursion is done in the IF statement. If it is not a directory then i assign the $file to a variable (since is is not a directory) called 'recent'. Then i check all items in the current folder. If the anyfile is a simple file AND it is newer than the previous assigned "recent" then the anyfile is now the recent one.
After the "done" it is supposed to have found the most recent file in the directory, but no!
Here is the code. What is/are wrong ?!
Thanks in advance.
Code:
#!/bin/bash
folder=$1
function func1
{
for file in * ; do
if [ -d $file ] ; then
cd $file
func1 # calls itseft
cd ..
else
recent=$file
for anyfile in * ; do
if [ -f $anyfile ] && [ $anyfile -nt $recent ] ; then
recent=$anyfile
fi
done
# *******
# Handle the most recent (-nt) file of the $file directory
# *******
fi
done
}
cd $folder
func1
exit -0