Register
It is currently Sat Dec 20, 2014 4:35 pm

I need help with a script please.


All times are UTC - 6 hours


Post new topic Reply to topic  [ 8 posts ] 
Author Message
 PostPosted: Sat Jul 16, 2011 4:37 am   

Joined: Sat Jul 16, 2011 4:17 am
Posts: 5
Hi all,

New here and new to linux.

I present this scenerio:

You work for a company that collects billing data and manages the billing for several customers. Last week, a customer sent you a number of files that were not intended for you. The customer just discovered the problem and is not sure about the files that were sent to you. The customer knows that all files end with .csv and the files that customers send end with .dat. Write a script that will use a loop to generate a list of the files to be deleted – so that the customer can see what you received – and delete the files one at a time in the loop.

I just started my linux class and i'm about 92% lost. haha If anyone could solve this for me, along with, a brief explanation of what is being done in the script and possibly any alternatives, I would greatly appreciate it.

I don't know much about this, but I am a fast learner when someone is willing to teach me and guide me properly. Please help me save my class from a careless instructor. We really wish I had a better instructor. I'm eager to learn this stuff, but the instructor only seems to be cashing a paycheck every week.

Thanks in advance


Top
 Profile  
 PostPosted: Sat Jul 16, 2011 6:17 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Welcome to the Free world. ; -)

This is just a find(1) job.
Since there is no directory specified, I consider searching from the root directory.

The portable way ( POSIX ):
Code:
find / -type f \( -name '*.csv' -o -name '*.dat' \) -mtime -7 -print -exec rm -f -- {} \; 2> /dev/null

Though, it'd be faster to delete every file thanks to one rm(1) call:
Code:
find / -type f \( -name '*.csv' -o -name '*.dat' \) -mtime -7 -print -exec rm -f -- {} + 2> /dev/null


The non-portable way ( GNU ):
Code:
find / -type f \( -name '*.csv' -o -name '*.dat' \) -mtime -7 -print -delete 2> /dev/null


This simply says:
« From the root directory, recursively search files ending with .csv or .dat which have been modified within
the last seven days ; print their absolute path, then delete them. And don't display permission errors
by redirecting file descriptor 2, which is STDERR, to /dev/null. »

For further information:
http://pubs.opengroup.org/onlinepubs/96 ... /find.html ; http://linux.die.net/man/1/find
http://pubs.opengroup.org/onlinepubs/96 ... es/rm.html


Last edited by jsz on Tue Jul 19, 2011 4:55 pm, edited 1 time in total.

Top
 Profile  
 PostPosted: Sun Jul 17, 2011 4:12 pm   

Joined: Sat Jul 16, 2011 4:17 am
Posts: 5
Thank you for the welcome, jsz. I must say I really like it so far. The history up until this point is great as well!

Oh man! This is awesome!! Thank you so much for the help! I was having a hard time with the recursive search syntax. Seeing it makes it click.

I will be sure to bookmark those links and pass them along to my classmates.

Again, thank you for the help. I(we) really appreciate it.

Have a great day!

(not as) lostonlinux :D


Top
 Profile  
 PostPosted: Sun Jul 17, 2011 4:44 pm   

Joined: Sat Jul 16, 2011 4:17 am
Posts: 5
One other question.

Is there a way to create a script using vim to do the same thing?


Top
 Profile  
 PostPosted: Sun Jul 17, 2011 8:16 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
You mean to put this in a script? Sure.

Since you're not using bash(1) ( or other shells ) feature(s), but just an external program ( find(1) ), you can
put #!/bin/sh as the shebang of your script.

So, write this in a file:
Code:
#!/bin/sh

...find(1) command goes here...


Then make the file executable thanks to chmod(1):
Code:
chmod +x scriptname


You can now run your script by typing:
Code:
./scriptname

or directly ( if you're in a directory that is in your PATH variable. ( « man bash | less +/^[[:blank:]]*PATH » ) ):
Code:
scriptname


------

If your question is « Is it possible to write the same thing in Vimscript, Perl, Python Ruby, Tcl, Lua... », then it's another subject.


Top
 Profile  
 PostPosted: Mon Jul 18, 2011 12:35 am   

Joined: Sat Jul 16, 2011 4:17 am
Posts: 5
That easy.. Wow. Thank you jsz!


Top
 Profile  
 PostPosted: Tue Jul 19, 2011 6:45 am   
User avatar

Joined: Wed Nov 17, 2010 1:02 pm
Posts: 9
Location: North Bethesda, MD
Can you tell us what version or distribution of Linux your class is using? Is it Red Hat or Ubuntu or Fedora for example?


Top
 Profile WWW  
 PostPosted: Fri Aug 26, 2011 12:55 am   

Joined: Sat Jul 16, 2011 4:17 am
Posts: 5
Hi SuperFly.

We were using Fedora/RHEL 12. Just wrapped up that class on Tuesday.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP