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-31-2009, 02:12 PM   #1
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Rep: Reputation: 37
[bash] rm regular expression help


I am having troubles with regular expressions...

I read the beginners guide to regex from here: [http://tldp.org/LDP/Bash-Beginners-G...l/chap_04.html]

But there is something evading me!

I want to learn in general but there is a particular problem that i want to solve:

I have a folder in which i will put my trash and when i exit bash i will delete the contents of that folder except two folders, so i have something like this right now:

[files]
[info]
temp1
temp2
temp3

doing some testing i get to this:

Code:
[~/.trash]$ rm $TRASH/*[!files]
rm: cannot remove `~/.trash//info': Is a directory
so basically [!expression] is a negation and the folder [files] is not being counted by rm.

now if I try:

Code:
[~/.trash]$ rm $TRASH/*[!files][!info]
rm: cannot remove `/home/.Trash-1000//*[!files][!info]': No such file or directory

[~/.trash]$ rm $TRASH/*[!files] [!info]
rm: cannot remove `~/.trash//info': Is a directory
rm: cannot remove `[!info]': No such file or directory

[~/.trash]$ rm $TRASH/*[!files] *[!info]
rm: cannot remove `~/.trash//info': Is a directory
rm: cannot remove `files': Is a directory
so I guess the question that i should be asking is:

How do i exclude *several* files or folders from being deleted?

Note that i could use "find -type f" to help me out but in general my trash can be files OR folders so I do want to remove other folders but not those two.
 
Old 07-31-2009, 02:21 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Looks to me like (in the first & 4th examples) you are going about it the right way. Check out the man page for rm for further details, particularly the -f and -R switches, which will cause your commands to act on folders. By default, rm works on files, not folders, and especially not folders with stuff in them. (I stand to be corrected on the semantics here, but this is what my experience has shown).

Also, FWIW, you have an extra slash in there, probably because you added it to the end of the $TRASH variable.

Sasha

Last edited by GrapefruiTgirl; 07-31-2009 at 02:26 PM.
 
Old 07-31-2009, 02:23 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Hello RaptorX

Too many "regular expressions" on *n*x systems!

That link is to regular expressions as used by grep and others. The expression in
Code:
[~/.trash]$ rm $TRASH/*[!files]
is a file name expansion as explained at http://www.gnu.org/software/bash/man...name-Expansion.

Maybe that's enough to point you in the right direction. Meanwhile I'll think about the job could be done and post again later.

Best

Charles
 
Old 07-31-2009, 02:33 PM   #4
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Original Poster
Rep: Reputation: 37
thanks for the quick replays guys...

@GrapefruiTgirl

right now im just testing, I plan to do a rm -rf soon but if i do it now i WILL remove the folders [info] and [files] which already have some info that i dont want to mess up... I know... move them out and try with dummy folders... I was just lazy...


Quote:
Also, FWIW, you have an extra slash in there, probably because you added it to the end of the $TRASH variable.
it is true... I will fix that. thanks for that I was already wondering a little time ago but didnt think about that.

@catkin

you are correct, that file was talking about grep in particular... I was finding it bit strange that some expressions did not work on other commands so I tried to figure out myself but I figure out that it would take a lot of time... (reading man bash at the moment, which can cause an aneurysm in the brain very quickly!!) i will take a time and read what you sent me.

Last edited by RaptorX; 07-31-2009 at 02:38 PM.
 
Old 07-31-2009, 02:39 PM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
This works
Code:
find . -name './\[files\]' -prune -o -name '\[info\]' -prune -o -exec echo rm -fr {} \;
The backslashes are necessary because [<stuff>] is a regular expression term for find.

Edit: actually is doesn't work unless the echo is removed (once you are confident it is generating the right commads)!

Last edited by catkin; 07-31-2009 at 02:42 PM.
 
Old 07-31-2009, 02:42 PM   #6
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Original Poster
Rep: Reputation: 37
interesting...

[can you tell me what -prune and -o means?]
I dont like to simply copy/paste things.

[EDIT: never mind im reading find --help]

EDIT2: sorry i dont find a definition for -prune, can you explain me what it does in this particular example?

Last edited by RaptorX; 07-31-2009 at 02:48 PM.
 
Old 07-31-2009, 02:49 PM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
And this produces a list of all the files-and-directories you want to delete using extended globbing (globbing is another, delightful, term for file name expansion)
Code:
shopt -s extglob
echo !('[files]'|'[info]')
 
Old 07-31-2009, 02:51 PM   #8
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Original Poster
Rep: Reputation: 37
Thanks for the help I ended up with

Code:
find $TRASH ! -name 'files' -a ! -name 'info' -exec rm -rf {} \;
which works exactly as i need.
 
Old 07-31-2009, 02:53 PM   #9
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Original Poster
Rep: Reputation: 37
Quote:
Originally Posted by catkin View Post
And this produces a list of all the files-and-directories you want to delete using extended globbing (globbing is another, delightful, term for file name expansion)
Code:
shopt -s extglob
echo !('[files]'|'[info]')
actually yes!
I was reading about it but I had a question that i couldnt find the answer...

do i have to initialize shopt -s extglob every time i start bash or is it a 1 time command?

if i have to initialize it then i guess i can put it in .bashrc
 
Old 07-31-2009, 02:56 PM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by RaptorX View Post
EDIT2: sorry i dont find a definition for -prune, can you explain me what it does in this particular example?
The man page is not easy to understand about -prune (best description under the explanation of -wholename). It stops "find" from processing the directory just matched -- no further action is taken on that directory and files-and-directories in it are not examined.
 
Old 07-31-2009, 02:59 PM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by RaptorX View Post
Thanks for the help I ended up with

Code:
find $TRASH ! -name 'files' -a ! -name 'info' -exec rm -rf {} \;
which works exactly as i need.
So the directories were not called [files] and [info], that was just the formatting you were using to denote them as directories? Hah! Not a convention I have seen before ... where is it from?
 
Old 07-31-2009, 03:02 PM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by RaptorX View Post
actually yes!
I was reading about it but I had a question that i couldnt find the answer...

do i have to initialize shopt -s extglob every time i start bash or is it a 1 time command?

if i have to initialize it then i guess i can put it in .bashrc
Every time, unless you put it in one of the shell initialisation files like ~/bashrc. Downside of doing that is any scripts written assuming it is set and using extended globbing will fail if it is not set, so you may prefer to set it explicitly when you need it rather than putting it in a shell initialisation file.
 
Old 07-31-2009, 03:05 PM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by RaptorX View Post
Thanks for the help I ended up with

Code:
find $TRASH ! -name 'files' -a ! -name 'info' -exec rm -rf {} \;
which works exactly as i need.
It will delete the contents of the files/ and info/ directories. OK?
 
Old 07-31-2009, 03:08 PM   #14
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Original Poster
Rep: Reputation: 37
hahaha that with the folders was just my doing, nothing copied...

so if i do not specify -prune even though i said to "find" do not match [files] it will still delete what is inside of that folder?? why on earth? where is the logic on that?

EDIT: it is true... I just tested the command and it does not list the folder [files] but it does list everything what is inside that folder... go figure.

Last edited by RaptorX; 07-31-2009 at 03:12 PM.
 
Old 07-31-2009, 03:17 PM   #15
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Original Poster
Rep: Reputation: 37
when i used the -prune option it doesnt list anything...

im trying:

Code:
find $TRASH ! -name 'files' -prune -a ! -name 'info' -prune -exec echo {} \;
 
  


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: checking if a variable is a number (need regular expression help) anonguy9 Linux - Newbie 6 03-29-2009 02:37 AM
regular expression (.*?) uttam_h Programming 6 05-30-2008 05:45 PM
Help with regular expression Feyd-Rautha Programming 8 04-21-2008 11:18 AM
Do we have regular expression in C++ ? indian Programming 4 03-06-2006 09:54 AM
Regular expression datbenik Programming 1 01-05-2006 01:58 PM

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

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