Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
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.
How can I exclude certain directories from bash commands like find, locate, cp, and such? For example, if I want to find and/or copy all .ogg files on my system that aren't already in my main music folder, how do I set up the commands to ignore that folder?
Where find is concerned, you can limit the part of the directory tree to search by specifying /, /etc, /home, /usr, etc in the find command. Locate uses regular expressions to search the locate database. I think you're SOL there. Cp may work if it's in a script, inside nested loops to compare directories, etc.
As far as your .ogg files are concerned, nested loops with an if-then-else-fi or case argument would work. The outer loop reads file names from one directory of interest; the inner loop reads file names from the other directory of interest; the if-then-else or case argument compares contents of the two directories, using bash variables such as -e (exists); !-e (doesn't exist); -a (and) and so on to make the filename comparison. The if-then-else or case arguments would then step through the options. If it exists here but doesn't exist there, copy from here to there. If it exists both places, skip to the next one, and so on.
A thorough (an I mean thourgh) reading of the bash man pages, or the Advanced bash-scripting guide, is in order. The loops I mentioned will require the use of square brackets to enclose arguments. In some cases, there MUST be a blank space between the brackets and the arguments enclosed by the brackets. If the blank space is omitted, the command won't work. Little things like that can make debugging scripts a nightmare.
Gah, that's too complicated. Besides, your suggestion for find wouldn't help me. I would want to search my whole profile starting at ~/, but exclude only the ~/music subtree, for example.
My .ogg example was only one example, though. For another, I have a folder /backups where I keep copies of important files that I don't want to show up on most searches. If I start a search from /, then the /backup folder is automatically included in the search, making it take more time than necessary and sometimes giving me duplicate matches.
All I really was hoping for was a fairly simple way to exclude a file or folder sub-tree from *any* major bash function. I was thinking that a regex might be the key, since most commands support them in some way. I tried playing around with expressions that would exclude certain strings while matching everything else, but I couldn't get anything to work. I just don't have enough knowledge of either bash or regex to do myself any good. I've only done a couple of easy scripts before too. I'm not ready yet for something that complicated.
I would think that it's possible. I can't imagine I'm the only person who's ever needed this kind of functionality.
All I really was hoping for was a fairly simple way to exclude a file or folder sub-tree from *any* major bash function. I was thinking that a regex might be the key, since most commands support them in some way. I tried playing around with expressions that would exclude certain strings while matching everything else, but I couldn't get anything to work. I just don't have enough knowledge of either bash or regex to do myself any good. I've only done a couple of easy scripts before too. I'm not ready yet for something that complicated.
I would think that it's possible. I can't imagine I'm the only person who's ever needed this kind of functionality.
That's not generically possible, you can just filter out any output
you don't desire ... but that will vary with different commands, and
their different invocations. You still have the processing overhead
(searching/accessing the disks).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.