LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   scripting help (https://www.linuxquestions.org/questions/linux-software-2/scripting-help-96275/)

wedgeworth 09-24-2003 09:03 AM

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

Kilka 09-24-2003 10:18 AM

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

wedgeworth 09-24-2003 01:49 PM

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....

druuna 09-24-2003 02:02 PM

Like to help you, but it's unclear to me what it is you actually want to do.
Quote:

want to write a script to remove files in a directory that have certain attributes
attributes. Are we talking about owner/group/rwx or some self made token
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.

wedgeworth 09-24-2003 02:13 PM

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.

Kilka 09-24-2003 02:18 PM

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

druuna 09-24-2003 02:28 PM

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.

wedgeworth 09-24-2003 02:36 PM

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

druuna 09-24-2003 02:57 PM

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> {} \;

wedgeworth 09-25-2003 12:12 PM

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?

druuna 09-25-2003 01:57 PM

Quote:

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.
The above command will not do what you want:

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

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?
All files in /etc and all the subdirs.

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.