LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris
User Name
Password
Solaris / OpenSolaris This forum is for the discussion of Solaris, OpenSolaris, OpenIndiana, and illumos.
General Sun, SunOS and Sparc related questions also go here. Any Solaris fork or distribution is welcome.

Notices


Reply
  Search this Thread
Old 08-25-2007, 01:17 AM   #1
mrrx7
LQ Newbie
 
Registered: Aug 2007
Posts: 1

Rep: Reputation: 0
Script to delete files from specified folder


I was trying to write a script that will delete all .jpg and .bak files from a folder as well as all its subdirectories, but I couldn't really get anywhere as I don't know scripting. I keep my scripts in a separate folder and would like it to run as:

./script /XFOLDER

so the script would delete all .jpg and .bak files from /XFOLDER and all of XFOLDER's subdirectories, and all its subdirectories and onwards down the tree

Could someone help me?
 
Old 08-25-2007, 03:50 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

You could use the find command to find and delete the bak and jpg files. The general syntax for find is:

find <targetdir> <test(s)> <action(s)>

- <targetdir> being the starting point from where find starts to look (find is recursive, so subdirs are automatically included),
- <test(s)> in your case you want to look for files that end with .bak or .jpg
- <action(s)> in your case this will be a delete from the files found.

You would end up with something like this:

find /XFOLDER -type f -name "*.bak" -o -name "*.jpg" -exec rm {} \;

/XFOLDER needs to be replaced by the input given to the script:

Code:
#!/bin/bash

# check if target was supplied
if [[ $# != 1 ]] 
then 
  echo "Usage: tree_cleaner <dir>"
  exit 1
fi

targetDir="$1"

# testing without the rm part, only show what would have been removed.
find ${targetDir} -type f -name "*.bak" -o -name "*.jpg"

# Remove the above find line and uncomment this line if the above works.
#find ${targetDir} -type f -name "*.bak" -o -name "*.jpg" -exec rm {} \;

exit 0
Here's a good site that deals with (bash) scripting: Advanced Bash-Scripting Guide
And here you can find a bit more info about the find command: find (Linux in a Nutshell, 5th Edition)

Hope this helps.
 
Old 08-25-2007, 04:03 AM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Welcome to LQ.

Are you running Solaris ?
 
Old 08-26-2007, 05:07 PM   #4
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197

Rep: Reputation: 105Reputation: 105
It's true, a lot of people like bash. But that same script will also work in ksh (just change the first line to #!/bin/ksh).

Also, if you are going to be doing a significant amount of work on a unix/linux system, you might want to get the O'Reilly book, "Unix in a Nutshell". That's been the guide next to my keyboard for more than 5 years (I work mostly in Solaris), and it is still regularly useful. That particular one liner (to find and remove files) can be found among the examples in the section on find.
 
Old 08-27-2007, 03:50 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
And by the way, this optimized version is faster:
Code:
find /XFOLDER -type f -name "*.bak" -o -name "*.jpg" -exec rm {} +
 
  


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
Script to delete week old files in a directory ilyavishnyakov Linux - General 11 02-13-2007 04:37 PM
need a script to delete all files from a folder and subfolders cccc Programming 1 03-04-2005 10:54 AM
bash script to delete files c0d3 Programming 9 12-05-2004 10:45 PM
Delete old files and folders Script? AsteX Linux - General 4 11-11-2004 06:26 PM
Script on linux to delete certain files ForumKid Linux - General 2 06-22-2002 01:09 PM

LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris

All times are GMT -5. The time now is 05:16 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
Open Source Consulting | Domain Registration