LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-16-2010, 09:09 AM   #1
shravee
Member
 
Registered: Sep 2009
Posts: 75

Rep: Reputation: 15
Post how to write a script file


hi all,

objective: i want to write a shell script file for the below subject

subject / situation :
i have many users say user1, user2, user3, user4 and so on... within my /home dir

within a user dir say.. /home/user1 i have many unwanted files. these unwanted files start with the name core for eg. core2324, core9789, core 9079 etc.. i need to delete them.

i want to write an automated script for this, which can do the same.
can someone help me to write a script which can delte these unwanted core files which exist in all user dirs.


thanks in advance.

regards
shravee

PS: i am new to scripting .. u can say no idea. but i will follow watever u guide. interms of learning any material or so on.

Last edited by shravee; 04-16-2010 at 09:24 AM. Reason: to make it more clear
 
Old 04-16-2010, 09:19 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,806

Rep: Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179
Quote:
Originally Posted by shravee View Post
hi all,

objective: i want to write a shell script file for thew below subject

subject / situation :
i have many users dir say user1 user2 user3 user4 and so on...within my /home dir

within a user dir say.. /home/user1 i have many unwanted files. these unwanted files start with core34242.... i need to delete them.

i want to write an automated script for them which can do the same.
can someone help me to write a script which can delte these unwanted core files which exist in all user dirs.


thanks in advance.

regards
shravee

PS: i am new to scripting .. u can say no idea. but i will follow watever u guide. interms of learning any material or so on.
First, spell out your words, and wirte clearly.

We can HELP you to write a script...so provide what you've done so far, and tell us where you're getting stuck. Otherwise, go to http://www.google.com and look at any one of the thousands of bash scripting tutorials that are out there. You may also want to be aware of how to 'chain' commands together in Linux. In this case, the find command into the rm command, like so:
Code:
rm `find -name <path> <expression`
Those are back-ticks, not single quotes. You can also do it via an ls command, piped into rm. Lots of options, see the tutorials.

Also, be aware that since you want to delete files from other users home directories, you need to be root to run this.
 
Old 04-16-2010, 09:28 AM   #3
HasC
Member
 
Registered: Oct 2009
Location: South America - Paraguay
Distribution: Debian 5 - Slackware 13.1 - Arch - Some others linuxes/*BSDs through KVM and Xen
Posts: 329

Rep: Reputation: 55
Show us what you've already tried to do
 
Old 04-16-2010, 11:59 PM   #4
shravee
Member
 
Registered: Sep 2009
Posts: 75

Original Poster
Rep: Reputation: 15
I created a file named "myscript.sh" within /home/user1


my script file contains.

--------------------------------------------

rm -rf `find core*`

-------------------------------------------

i came to cmd prompt and typed sh myscript.sh

this solved my purpose. but this was only for a single user.

i need to perform this for several userrs like... /home/user2 /home/user2 and so on...

so how to continue this script comd to perform the same for all users..


regards
shravee.
 
Old 04-17-2010, 01:11 AM   #5
dv502
Member
 
Registered: Sep 2006
Location: USA - NYC
Distribution: Whatever icon you see!
Posts: 642

Rep: Reputation: 57
Note: You must be root to delete other users files.

Be careful when using the wildcard * with the rm command. Always do a test run and check the results.

Do a test run first. If some file(s) are listed that you do not want to delete, you will need to tweak the command.

Code:
find /home/ -type f -name "core[0-9]*")
If you are satisfy with the results from above, run the code below. BE CAREFUL!
Code:
rm -rf $(find /home/ -type f -name "core[0-9]*")
For more options, see the man page

Last edited by dv502; 04-17-2010 at 01:54 AM.
 
1 members found this post helpful.
Old 04-17-2010, 10:10 AM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,806

Rep: Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179
Quote:
Originally Posted by shravee View Post
I created a file named "myscript.sh" within /home/user1

my script file contains.
--------------------------------------------
rm -rf `find core*`
-------------------------------------------
i came to cmd prompt and typed sh myscript.sh
this solved my purpose. but this was only for a single user.

i need to perform this for several userrs like... /home/user2 /home/user2 and so on...
so how to continue this script comd to perform the same for all users..
regards
shravee.
If you bothered to read my first post, or the man pages, you'd have seen that you need to specify the PATH for the find statement, which you didn't.
 
Old 04-17-2010, 10:39 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by shravee View Post
rm -rf `find core*`

...

this solved my purpose. but this was only for a single user.
Are you sure? AFAIK it should not have worked.
 
Old 04-17-2010, 10:43 AM   #8
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 12,714
Blog Entries: 27

Rep: Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058
Hi,

Just a few useful links;

Linux Documentation Project
Rute Tutorial & Exposition
Linux Command Guide
Bash Reference Manual <<<< Look here
Advanced Bash-Scripting Guide <<<< And here
Linux Newbie Admin Guide
LinuxSelfHelp
Getting Started with Linux

The above links and others can be found at 'Slackware-Links' . More than just SlackwareŽ links!
 
Old 04-17-2010, 09:46 PM   #9
shravee
Member
 
Registered: Sep 2009
Posts: 75

Original Poster
Rep: Reputation: 15
hi
---------------------------------------------------------------------------------------
dv502,

thanks for your code. I will try it out and let you know.


---------------------------------------------------------------------------------------
catkin,

I created files named cat, c, core2332 and core423 . then i used rm -rf `find core*` which really worked in deleting those core files.
---------------------------------------------------------------------------------------


I have a new problem , the script file solved my problem for a single user . so cant i still generalise it for the entire users listed out.


regards
shravee
 
Old 04-17-2010, 09:58 PM   #10
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,806

Rep: Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179
Quote:
Originally Posted by shravee View Post
hi
-----------------------------------------------------------------------
dv502,
thanks for your code. I will try it out and let you know.
------------------------------------------------------------------------
catkin,

I created files named cat, c, core2332 and core423 . then i used rm -rf `find core*` which really worked in deleting those core files.
-----------------------------------------------------------------------

I have a new problem , the script file solved my problem for a single user . so cant i still generalise it for the entire users listed out.
regards
shravee
Probably still having an issue, because you're not paying attention. You've been given the solution here three times, and directed to the man page twice. Again, you need to be root to run this, and you need to supply the find command with a starting path.
 
Old 04-18-2010, 06:47 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by shravee View Post
I created files named cat, c, core2332 and core423 . then i used rm -rf `find core*` which really worked in deleting those core files.
My gast is flabbered; I've just confirmed that it does work!

Nowhere in the man page does it say that path has to be a directory; given the behaviour established by shravee it seems that path can be a file. A seldom (ever, until now?!) used feature. Here's experimenting
Code:
c@CW8:/tmp/tmp$ ls -l
total 0
-rw-r--r-- 1 c users 0 2010-04-18 16:58 c
-rw-r--r-- 1 c users 0 2010-04-18 16:58 cat
-rw-r--r-- 1 c users 0 2010-04-18 16:58 core2332
-rw-r--r-- 1 c users 0 2010-04-18 16:58 core423
c@CW8:/tmp/tmp$ find core2332 -type d
c@CW8:/tmp/tmp$ find core2332 -type f
core2332
Incidentally, the find man page syntax diagram shows "[path ...]" implying that path/paths is/are optional. It seems to default to "." (the man page is not explicit)
Code:
c@CW8:/tmp/tmp$ find -type f
./c
./cat
./core2332
./core423
All of which is interesting but, for transparency if nothing else, I will continue with the common practice of specifying at least one path (even if it is the redundant ".") and using only directories as paths.
 
Old 04-18-2010, 08:51 AM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by catkin View Post
Incidentally, the find man page syntax diagram shows "[path ...]" implying that path/paths is/are optional. It seems to default to "."
The GNU findutils manual is a bit more clear. The syntax is:
Code:
find [file...] [expression]
where if it encounters a directory in the list of files [file...], it is descended recursively. The search results are filtered from the expression. The find command alone, without arguments, will find all objects into and below the current directory (included).

Indeed you can avoid to specify the dot as path because it is actually implicit, but I think it is a good habit for clarity and to remind how find actually works.

Maybe, it would be interesting to see what happen if you specify both dot and the list of files starting with c:
Code:
find . c*
 
Old 04-18-2010, 09:17 AM   #13
cola
Senior Member
 
Registered: Sep 2007
Location: Dhaka,Bangladesh
Distribution: Debian
Posts: 1,031

Rep: Reputation: 63
Quote:
Originally Posted by shravee View Post
hi all,

objective: i want to write a shell script file for the below subject

subject / situation :
i have many users say user1, user2, user3, user4 and so on... within my /home dir

within a user dir say.. /home/user1 i have many unwanted files. these unwanted files start with the name core for eg. core2324, core9789, core 9079 etc.. i need to delete them.

i want to write an automated script for this, which can do the same.
can someone help me to write a script which can delte these unwanted core files which exist in all user dirs.


thanks in advance.

regards
shravee

PS: i am new to scripting .. u can say no idea. but i will follow watever u guide. interms of learning any material or so on.
To delete those files automatically after a certain time,use cronjob,http://www.scrounge.org/linux/cron.html
 
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
write script to copy a file kavehiks Linux - General 5 10-21-2009 03:50 PM
Can you write a script to display file name, Inode, and size of any file? JaxsunApex Linux - Newbie 3 01-30-2007 08:39 AM
How do I write a script (batch file)?? cvzyl Linux - Software 4 07-30-2006 09:11 AM
How to write script file in Linux? rust8y Linux - General 3 05-20-2006 05:49 AM
Backup script to write to file? ctroyp Linux - Newbie 13 01-10-2006 06:15 PM


All times are GMT -5. The time now is 03:23 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration