LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to write a script file (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-write-a-script-file-802402/)

shravee 04-16-2010 09:09 AM

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.

TB0ne 04-16-2010 09:19 AM

Quote:

Originally Posted by shravee (Post 3937643)
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.

HasC 04-16-2010 09:28 AM

Show us what you've already tried to do

shravee 04-16-2010 11:59 PM

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.

dv502 04-17-2010 01:11 AM

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

TB0ne 04-17-2010 10:10 AM

Quote:

Originally Posted by shravee (Post 3938194)
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.

catkin 04-17-2010 10:39 AM

Quote:

Originally Posted by shravee (Post 3938194)
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.

onebuck 04-17-2010 10:43 AM

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!

shravee 04-17-2010 09:46 PM

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

TB0ne 04-17-2010 09:58 PM

Quote:

Originally Posted by shravee (Post 3938978)
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.

catkin 04-18-2010 06:47 AM

Quote:

Originally Posted by shravee (Post 3938978)
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.

colucix 04-18-2010 08:51 AM

Quote:

Originally Posted by catkin (Post 3939204)
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*
;)

cola 04-18-2010 09:17 AM

Quote:

Originally Posted by shravee (Post 3937643)
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


All times are GMT -5. The time now is 09:00 AM.