ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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)
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
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.
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?
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.