scripting help
want to write a script to remove files in a directory that have certain attributes...but i don't know how to travers the list of files to choose which one's to delete. any help on how the files are assigned to some wildcard variable that you can actually delete and then assign variable to the next file to check and see if you want to delete that one too. sorry i have experiance in programming but script writing.....i'm still learning the syntax. any help would be appreciative. thnx
|
are you sure you can do that ?
I've never heard of assigning files variables. I know that its done low level by jfs to mark which files to move, make changes to. Maybe you can just rename the files, to something with an extra "-" or something.
Then you could just use bash and sed to work that out. -kilka |
basically i need to be able to have an array of filenames. then i can traverse the array on the index and delete files that way. still don't know how to do that....
|
Like to help you, but it's unclear to me what it is you actually want to do.
Quote:
And if so are these tokens stored in some kinda file. Tell a bit more about what the basic problem is. I could be wrong, but al the other stuff you wrote seems part of a solution you try to get working. |
sorry for not being clear. my script will delete files from a directory......but only based on there age. therefore the names are irrelevant. so i need a way to reference the file that is too old so i can delete it. similar to deleting elements in an array. you don't care what is stored in array[10].....you just know that the size of array[10] > 0.
|
sed sucks, but....
I know using sed or awk sucks, but I see it as a solution. Take the output of ls -al, which would give you something like this:
drwxr-xr-x 7 user group 8192 Sep 23 23:08 WWW and sed "Sep 23" out. Then you should just be able to write a bash script that can run under cron. -kilka |
Why can't you use find??
It has options to find files by last access time, changed files (file owner/access etc), modified time (the data). And that are just 3 of the many options. In a script you could feed it al kinda data. |
but i still don't know the syntax....which is the problem. what to do is not the problem. how to do it is. scripting is not my background. c++ java is. i need to know what to put here.
rm <??????> i could easily do a [date -r <filename> +%s ] > X to see what to delete. but i want a script running off some cron to do it for me from now on. which means i don't know what future filenames there will be. so i need some way to reference the filename in the "if statement" and then use that as the ????? in my rm statement |
Syntax is easy: man find ;)
But to get you starting: Show files modified within the last 2 days, startingpoint is root dir. find / -mtime -2 -print And things can be combined: Remove all files [-exec rm {}\;] ending with bak[-name "*bak"], must be of type file [-type f], change date must be older then 2 days [-ctime +2] and starttingpoint current dir[.]. find . -ctime +2 -type f -name "*bak" -exec rm {} \; About the -exec command: make sure you don't make mistakes! If I'm not sure I do the following: -exec echo <command> {} \; |
if i do this:
find /etc/backup -mtime -1 -type f -print | tar -cvf backup.tar i think it will grab all the files in my backup folder that are a day old and tar them together. butwhen i do this find /etc -mtime -1 -type f -print | tar -cvf backup.tar will it ONLY grab day old stuff in all the directories under /etc (ex: /etc/junk/test/.....) and tar them up in the correct file structure? |
Quote:
-mtime -1 => modified files within 1 day, so only the last 24 hours -mtime +1 => older then 1 day tar will complain because the pipe will not work. The following example does work. The shell executes the find statement first and replaces everything within, and including the ` (backquotes) with the result of the find statement. I also changed the -1 to +1 tar cvf backup.tar `find /etc/backup -mtime +1 -type f -print` Quote:
If you want to limit the descend you can use -maxdepth, -mindepth and -depth (see manpage). |
All times are GMT -5. The time now is 01:56 PM. |