LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-24-2003, 10:03 AM   #1
wedgeworth
Member
 
Registered: Aug 2003
Posts: 234

Rep: Reputation: 30
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
 
Old 09-24-2003, 11:18 AM   #2
Kilka
Member
 
Registered: Sep 2003
Location: Canada
Distribution: Mandrake, Redhat, openBSD, Gentoo
Posts: 84

Rep: Reputation: 15
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
 
Old 09-24-2003, 02:49 PM   #3
wedgeworth
Member
 
Registered: Aug 2003
Posts: 234

Original Poster
Rep: Reputation: 30
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....
 
Old 09-24-2003, 03:02 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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.
 
Old 09-24-2003, 03:13 PM   #5
wedgeworth
Member
 
Registered: Aug 2003
Posts: 234

Original Poster
Rep: Reputation: 30
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.
 
Old 09-24-2003, 03:18 PM   #6
Kilka
Member
 
Registered: Sep 2003
Location: Canada
Distribution: Mandrake, Redhat, openBSD, Gentoo
Posts: 84

Rep: Reputation: 15
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
 
Old 09-24-2003, 03:28 PM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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.
 
Old 09-24-2003, 03:36 PM   #8
wedgeworth
Member
 
Registered: Aug 2003
Posts: 234

Original Poster
Rep: Reputation: 30
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

Last edited by wedgeworth; 09-24-2003 at 03:39 PM.
 
Old 09-24-2003, 03:57 PM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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> {} \;

Last edited by druuna; 09-24-2003 at 04:00 PM.
 
Old 09-25-2003, 01:12 PM   #10
wedgeworth
Member
 
Registered: Aug 2003
Posts: 234

Original Poster
Rep: Reputation: 30
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?
 
Old 09-25-2003, 02:57 PM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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).

Last edited by druuna; 09-25-2003 at 02:59 PM.
 
1 members found this post helpful.
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
new to scripting mifan Linux - Newbie 2 08-17-2005 01:10 PM
another help with scripting?! ice99 Programming 2 08-09-2005 09:56 AM
Need help scripting Tamara Programming 7 06-06-2005 03:06 AM
Scripting Help Please Jazinator Linux - Newbie 7 10-17-2004 07:35 PM
scripting bforest Linux - Newbie 4 05-11-2004 03:45 AM


All times are GMT -5. The time now is 01:11 PM.

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