LinuxQuestions.org
Visit Jeremy's Blog.
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 07-29-2012, 07:46 PM   #1
james jackson
LQ Newbie
 
Registered: Jul 2012
Posts: 8

Rep: Reputation: Disabled
Post bash shell script for deleting files


hello, i would like to know how to remove files from a directory and its sub-directories using shell script. the script should be able to handle files and directory with space in them.
thank you
 
Old 07-29-2012, 07:51 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Do let us know your level of experience in shell scripting and what you've tried already by posting (pseudo) code.
 
Old 07-29-2012, 07:55 PM   #3
james jackson
LQ Newbie
 
Registered: Jul 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
i just install ubuntu, i'm very new to linux. i know the basic command but i dont know how to use them in script. i been doing some reading about it.
Quote:
Originally Posted by unSpawn View Post
Do let us know your level of experience in shell scripting and what you've tried already by posting (pseudo) code.
 
Old 07-29-2012, 08:02 PM   #4
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Rep: Reputation: 31
$ man find

$ find . -name '*.bored' -delete
 
Old 07-29-2012, 08:16 PM   #5
james jackson
LQ Newbie
 
Registered: Jul 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
ok i want to delete file that end in .c and .text
would it handle file with space and directory
find -name /home '.c' '.text' -delete


Quote:
Originally Posted by Jerry Mcguire View Post
$ man find

$ find . -name '*.bored' -delete
 
Old 07-29-2012, 08:56 PM   #6
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Rep: Reputation: 31
yes.

It should be

# find /home -name '*.c' -delete
# find /home -name '*.text' -delete

Read the man page again.

and be very careful since you are deleting files as root. Don't yell if anything goes wrong.
 
Old 07-29-2012, 09:01 PM   #7
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Rep: Reputation: Disabled
Quote:
Originally Posted by james jackson View Post
ok i want to delete file that end in .c and .text
would it handle file with space and directory
find -name /home '.c' '.text' -delete
Hi James, This will remove any files with the extensions c or text:
Code:
find /home/ -regex ".*\(text\|c\)$" -delete
 
Old 07-29-2012, 09:43 PM   #8
james jackson
LQ Newbie
 
Registered: Jul 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
pretty cool trick i loving linux
what if i want to do that in a script using rm command with multiple pattern
for example in the command prompt i type remove /home ".c" ".text" "~"
remove would be the name of my script so $1=/home, $2=the patterns
would this code work
for -d in $1 do
rm -regex ".$2"

Quote:
Originally Posted by amboxer21 View Post
Hi James, This will remove any files with the extensions c or text:
Code:
find /home/ -regex ".*\(text\|c\)$" -delete
 
Old 08-01-2012, 01:46 AM   #9
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Rep: Reputation: Disabled
Quote:
Originally Posted by james jackson View Post
pretty cool trick i loving linux
what if i want to do that in a script using rm command with multiple pattern
for example in the command prompt i type remove /home ".c" ".text" "~"
remove would be the name of my script so $1=/home, $2=the patterns
would this code work
for -d in $1 do
rm -regex ".$2"
Maybe something like this?
Code:
#!/bin/bash


        if [ $# -lt '1' ]; then
        echo -e "\n=> Listing files." && ls;
        echo -e "\nUSAGE: $0 + extensions to files you want to remove completely.\n"
fi

        if [ $# -eq '1' ]; then
        find -regex ".*\($1\)$" -delete
        echo -e "\nRemoving files with extension $1."
fi

        if [ $# -eq '2' ]; then
        find -regex ".*\($1\|$2\)$" -delete
        echo -e "\nRemoving files with extensions $1 and $2."
fi

        if [ $# -eq '3' ]; then
        find -regex ".*\($1\|$2\|$3\)$" -delete
        echo -e "\nRemoving files with extensions $1, $2 and $3."
fi

Last edited by amboxer21; 08-01-2012 at 01:58 AM.
 
  


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
bash script deleting rows mr_twister Programming 3 01-17-2012 03:29 PM
how to check folder if it has files in it using Bash Shell script vincent.dang Linux - Newbie 7 08-05-2010 07:58 PM
[SOLVED] Bash script: how to move files to Trash instead of deleting? KonfuseKitty Programming 1 07-10-2010 12:44 PM
[SOLVED] Bash shell script to check the if empty files are being created & start the process hussa1n Linux - General 8 06-29-2010 05:49 PM
Simple Shell Script? Deleting Duplicate Files... Tag234 Linux - Newbie 6 10-10-2009 04:49 AM

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

All times are GMT -5. The time now is 04:17 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