LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-06-2008, 03:34 AM   #1
cghcgh
Member
 
Registered: Mar 2008
Posts: 69

Rep: Reputation: 15
How to write this script?


Hi,

I was given a task to do some housekeeping of users directory
Basically, the structure is of below.

/User DIR
---UserA
---UserB
---UserC
---UserD
---UserE


Each user contain several type of files.

I wan to do a housekeep of the directories to remove *.txt, *.log, *.wk1 that are more than 5 days old.

Pls kindly assist.
 
Old 05-06-2008, 03:55 AM   #2
Randux
Senior Member
 
Registered: Feb 2006
Location: Siberia
Distribution: Slackware & Slamd64. What else is there?
Posts: 1,705

Rep: Reputation: 55
Are you gonna pay us too?
 
Old 05-06-2008, 04:10 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
The commands you will need are find, xargs and rm. This exact combination has been discussed many times before in this forum. Have a look at this thread to get a very good idea of what you need to do:

http://www.linuxquestions.org/questi...ght=find+xargs
 
Old 05-06-2008, 06:15 AM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Just one advice.

At the start of your script, put:
Code:
CMD=echo
Once you have tested and debugged your code, change this to:
Code:
CMD=/bin/rm
In your script use $CMD instead of rm.

You will be debugging with echo statements, after all is tested you replace echo with rm. Otherwise you might wipe out your hard disk when you forget a decimal point or so in your code. Been there, done that.

jlinkels
 
Old 05-06-2008, 09:39 PM   #5
cghcgh
Member
 
Registered: Mar 2008
Posts: 69

Original Poster
Rep: Reputation: 15
hi guys,
i did look at the link before.

i need to search for several type of extension .wk1 , .log , .txt etc. how do i incorporate it in? . Another thing is
ls -l | find . -name "*.txt" -mtime +3 -exec ls {} \; it actually returns me all the subdirectories as well. y is that so?

in my office, i have existing scripts that have similiar syntax
find . ! -type d -ctime -5 -name '*.txt' -exec ls {} \;
find . ! -type d -ctime -5 -name '*.txt' -exec rm -f {} \;

what does find . ! -type d means?

Pls kindly assist.
 
Old 05-06-2008, 11:19 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Read this page: http://www.oreillynet.com/linux/cmd/cmd.csp?path=f/find
 
Old 05-07-2008, 03:22 AM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
You can combine terms with OR and AND logical operators. The find syntax is "-o" and "-a" respectively. For example:
Code:
find . -name '*.txt' -o '*.xls'
Which will match files whose name matches the pattern *.txt OR the pattern *.xls - i.e. it will list them both.

You can also create more complex conditions using ( and ) to group statements. You can also use ! for logical NOT. These operators need to be escaped with a \ to prevent the shell from interpreting them. For example the condition "files whose name does not contain 'eight', and whose name matches *.jpg or *.txt" might be represented with the pseudo code:
Code:
(NOT NAME '*eight*') AND (NAME '*.jpg' OR NAME '*.txt')
...and this would be done with find like this:
Code:
$ ls 
eight.jpg  five.doc  four.dat  nine.jpg  one.txt  seven.jpg  six.jpg  three.xls  two.txt
$ find . \! -name '*eight*' -a \( -name '*.jpg' -o -name '*.txt' \)
./seven.jpg
./two.txt
./one.txt
./nine.jpg
./six.jpg

Last edited by matthewg42; 05-07-2008 at 03:34 AM. Reason: Adding example
 
Old 05-08-2008, 12:32 AM   #8
cghcgh
Member
 
Registered: Mar 2008
Posts: 69

Original Poster
Rep: Reputation: 15
hi matthew, i finally make out the command..
KEEP=5

find . ! -type d -mtime +${KEEP} -a \( -name '*.log' -o -name '*.xls' -o -name '*.txt' -o -name '*.wk1'
\) -exec ls {} \;

i guess this should be correct. but the issue is that it will traverse down the subdirectories as well...i just want to limit to the current directory. how do i do that?

another question is...i have a ! ...what does it mean in the statement?
 
Old 05-08-2008, 03:48 AM   #9
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Don't forget to escape the "!" character - many shells use this character to refer to previous commands, and can do weird stuff if you don't escape it with a preceding "\" character.

The -maxdepth option takes an integer argument which says how many levels of sub-directory to descend. 1 means just the specified directory (i.e. do not look in sub-directories. It matters where you put the -maxdepth option - it should go directly after the ".".

Code:
KEEP=5
find . -maxdepth 1 \! -type d -mtime +${KEEP} -a \( -name '*.log' -o -name '*.xls' -o -name '*.txt' -o -name '*.wk1' \) -exec ls {} \;
 
Old 05-28-2008, 04:36 AM   #10
cghcgh
Member
 
Registered: Mar 2008
Posts: 69

Original Poster
Rep: Reputation: 15
hi guys..thanks very much for the help. i managed to resolve this issue.
 
  


Reply



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
how write a script saif Linux - Newbie 8 07-27-2007 12:31 PM
script to write to a cd nasirdaudahmad Linux - Hardware 3 01-23-2007 11:45 PM
How to write a Script Blake Linux - Software 6 07-18-2004 11:41 AM
how to write this script? MeganageM Linux - Newbie 1 03-10-2004 05:01 PM
Help with a script I need to write... cmfarley19 Programming 9 12-06-2003 12:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:10 AM.

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
Open Source Consulting | Domain Registration