hi
I am trying to write shell script that must scan a series of filesystems and find files that have not been accessed for over some number
of days and move them to /tmp/old
Did anyone write a such a script before?
Any help is really appreciated.
hi
I am trying to write shell script that must scan a series of filesystems and find files that have not been accessed for over some number
of days and move them to /tmp/old
Did anyone write a such a script before?
Any help is really appreciated.
By accessed do you mean modified or just read..... assuming you want access time
days=20
find /path/to/files -type f -atime $days | \
while read file
do
mv $file /tmp/old
done
thank a lot JIM.
Can I also create a log
of each user's files that are relocated and e-mail the log to the owner of the files?
jim mcnamara script is an over kill.
find can execute comands on the files founds.
most of the times piping finds output to xarg or loops is not needed
but if you want to email the user, then you probably shoudl use waht he said, and ad the folowing things
an echo before the find, starting a file with some header like
and echo inside the loop, using >> to add the file names
one or more echos after the loop with some kind of finalization of the email body.
and for the tricky part, for each file, get the user. you can use ls -l, and pipe it to awk. or use some comand like "stat"
create temporal files for each user, with the body of the mail, and keep adding each file you find about that user.
and use mailx comand to send that file to someone
I tried something like this, but it doesnt work.
#!/bin/sh
days=10
find /home -type f -atime $days | \
while read file
do
basename $file >>log.txt
cp $file /tmp/old
owner = find /home -type f -atime +5 -exec ls -l {} \;|awk '{print $3}'
mail -s "Log of the deleted files from your account " $owner@gmail.com < log.txt
done
of course it dosnt.
first of all. when you execute a command and what to put that into a var, you use foward ticks ``
owner=`find /home -type f -atime +5 -exec ls -l {} \;|awk '{print $3}'`
and you move the files before doing the ls. and you even do a second find !!
second, you will be sending a mail for each file, and each mail would contain all the previous file also
and pls use the
tags
#!/bin/sh
days=10
temporal_folder=/tmp/`basename $0`-$$
mkdir $temporal_folder
find /home -type f -atime $days | \
while read file
do
owner=`ls -l $file | awk ' { print $3 } '`
basename $file >> $temporal_folder/$owner
cp $file /tmp/old
done
for file in $temporal_folder
do
mail -s "Log of the deleted files from your account " $file@gmail.com < $file
done
rm -rf $temporal_folder
thanks
I will work on it
Please use the code tags.
#!/bin/sh
days=10
find /home -type f -atime $days | \
while read file
do
basename $file >>log.txt
cp "$file" /tmp/old
#What do you try to find here? The owner of the file?
owner=$(find /home -type f -atime +5 -exec ls -l {} \;|awk '{print $3}')
#Do you really want to send the log for each file?
mail -s "Log of the deleted files from your account " $owner@gmail.com < log.txt
done
#What do you try to find here? The owner of the file?
#Yes
owner=$(find /home -type f -atime +5 -exec ls -l {} \;|awk '{print $3}')
#Do you really want to send the log for each file?
The files that are copied should be written into log file and then the entire logfile should be mailed to owner of the files
mail -s "Log of the deleted files from your account " $owner@gmail.com < log.txt
broli already provide the solution, http://www.unix.com/shell-programming-scripting/76781-move-old-files.html\#post302224142
yes
I got it
thanks broli
Can I modify this script to use a file that contains a list of files that should be excluded to allow the user
to specify files in non-standard locations that should be exempt from being moved.
if you are asking for permission to doit, sure, go head.
if you are asking a way to doit. this is getting a little old
#!/bin/sh
days=10
temporal_folder=/tmp/`basename $0`-$$
$blacklist=/path/to/file
mkdir $temporal_folder
find /home -type f -atime $days | \
while read file
do
while read line
do
if [ $line != $file ]
#is not a protected file
owner=`ls -l $file | awk ' { print $3 } '`
basename $file >> $temporal_folder/$owner
cp $file /tmp/old
fi
done < $blacklist #black list
done #find's output
for file in $temporal_folder
do
mail -s "Log of the deleted files from your account " $file@gmail.com < $file
done
rm -rf $temporal_folder
this script expects a a black list file that contains the path, exactly as find's output.
one file per line.
So
During the execution of the script, I have to pass blacklist as an argument at the command prompt and find command willnot search the directory in the blacklist. Right?
and here is my questions about your script:
$blacklist=/path/to/file --so why do we use $blacklist and not only blacklist?
mkdir $temporal_folder
find /home -type f -atime $days | \
while read file
do
while read line
do
if [ $line != $file ] -- why not if [ $line != $blacklist ]
#is not a protected file
owner=`ls -l $file | awk ' { print $3 } '`
basename $file >> $temporal_folder/$owner
cp $file /tmp/old
fi
done < $blacklist #black list -- and what is the use of < in here
done #find's output
?????
no. its a variable, with the path to the real blacklist file. created by you.
one file per line. full path.
as i said. is a variable i create. so next time i use it, i have to tell bash i want the variable and not the literal text.
$file contains one line, that has been erad from the file. as i explain below
means that im redirecting the file (wich full path is holded in the variable $blacklist) to the while.
i this case, the "read" command will read one line at a time. and execute the while loop, until it reaschs EOF
this documents is perfect for people starting with bash scripting
Advanced Bash-Scripting Guide
and pls dont sent me pms with questions. specially if you posted the same in here
ok
my last question is:
How can I modify the script so that it will track the size of the directory it is creating and stop and create a
new directory when the size is some value
if you are worried about this script filling up some disc, then you are in serious problems.
even if it had to process hundreds of files the text files wont go over a few kbs
you have to star talking about thousands of files to get something like 1mb
actually. i did some tests.
if your script encoutnered arround 5thousand files to be moved (that implies temporal files containig the body of the mail to have 5thousand lines)
and each line beein 76 chars long (wich is several subfolders long)
the temporal folder would be still under 400k
but, if youw ant to keep track of how much a folder or file is worth in kb, i suggest you to search the forums.
i know there must be at least 5 similar quesiotns on the last week
ok broli thanks for ur help.
I will search the forum
hi again broli
I tested and corrected some syntax, but still not working.
Even echo doesnt work inside the 2nd while loop
#!/bin/sh
days=10
temporal_folder=/tmp/`basename $0`-$$
blacklist=/home/xcv.txt
mkdir $temporal_folder
find /home -type f -atime +$days | \
while read file
do
while read line
do
echo 12345
done < $blacklist #black list
done #find's output
------
No output
but
12345 must have been shown