Deleting everything in directory except for two specific folders - unix

I'm trying to delete everything within a directory except for two folders that I know the names of. Let's say that the two folders are called "dont_delete1" and "dont_delete2". And within the the current directory, other folder and files exist.
I have tried
rm -r !(dont_delete1|dont_delete2)
but that requires me to shopt -s extglob which due to certain restraint, I can't use.
So I turned to
find . \! -name [folder name] -delete
I've tested it out on a single folder and it works. But I can't figure out a way to use the above command for multiple folders. I've tried all sorts of commands that I thought would work but was unsuccessful.

Lots of different solutions, here's one thats easy to understand:
$ls | grep -v dont_delete1 | grep -v dont_delete2 | xargs rm -rf

Related

How to list and delete directories that are not symbolic links?

I see plenty of answers on how to list all symlinks and how to remove all symlinks within a specific directory. However what about vice versa?
How would one go about listing/removing all directories within a directory that are not symlinks?
I know that rm -R removes all directories recursively but i want to know how to make it not delete symlinks in the process.
I also know that ls lists all directories files and symlinks however i would like to know how i would go about listing only directories that are not symbolic links.
Found a way finally.
First, run:
find . -depth -type d
to make sure the output looks sane, then:
sudo find . -depth -type d -exec rm -rf '{}' \;
Sure this does get a bit messy on the console to look through, but ... it works! If anyone can find a better and cleaner way to do this please post it.

How to diff all modifications of all checkout files under a directory recursively with Clearcase?

I'm using ClearCase. I checked out several files under a directory, some of files in its sub-directory, some in sub-sub-directory.
What I want is to list the diff of all my modifications on these checked out files under this certain directory recursively.
What I currently do is:
for file in $(cleartool lsco -recurse -me -cview -fmt "%n\n"); do
cleartool diff -serial_format -pred $file;
done
I use a bash for loop, but I perhaps it can be done with a simple ClearCase command.
The OP suggests using the list of checked out files, but there is no way to find the diff in one cleartool command.
An xargs (used here) might be easier
cleartool lsco -recurse -me -cview -fmt "%n\n" | xargs -n 1 cleartool diff -serial_format -pred

Tar creating a file that is unexpectedly large

Figured maybe someone here might know whats going on, but essentially what I have to do is take a directory, and make a tar file omitting a subdir two levels down (root/1/2). Given it needs to work on a bunch of platforms, the easiest way I thought was to do a find and egrep that directory out, which works well giving me the list of files.
But then I pipe that file list into a xargs tar rvf command and the resulting file comes out something like 33gb. I've tried to output the find to a file, and use tar -T with that file as input, its still comes out to about 33gb, when if I did a straight tar of the whole directory (not omitting anything) it comes in where I'd expect it at 6-ish gb.
Any thoughts on what is going on? Or how to remedy this? I really need to get this figured out, I'm guessing it has to do with feeding it a list of files vs. having it just tar a directory, but not sure how to fix that.
Your find command will return directories as well as files
Consider using find to look for directories and to exclude some
tar cvf /path/to/archive.tar $(find suite -type d ! -name 'suite/tmp/Shared/*')
When you specify a directory in the file list, tar packages the directory and all the files in it. If you then list the files in the directory separately, it packages the files (again). If you list the sub-directories, it packages the contents of each subdirectory again. And so on.
If you're going to do a files list, make sure it truly is a list of files and that no directories are included.
find . -type f ...
The ellipsis might be find options to eliminate the files in the sub-directory, or it might be a grep -v that eliminates them. Note that -name normally only matches the last component of the name. GNU find has ! -path '*/subdir/*' or variants that will allow you to eliminate the file based on path, rather than just name:
find . -type f ! -path './root/1/2/*' -print

how do I zip a whole folder tree in unix, but only certain files?

I've been stuck on a little unix command line problem.
I have a website folder (4gb) I need to grab a copy of, but just the .php, .html, .js and .css files (which is only a couple hundred kb).
I'm thinking ideally, there is a way to zip or tar a whole folder but only grabbing certain file extensions, while retaining subfolder structures. Is this possible and if so, how?
I did try doing a whole zip, then going through and excluding certain files but it seemed a bit excessive.
I'm kinda new to unix.
Any ideas would be greatly appreciated.
Switch into the website folder, then run
zip -R foo '*.php' '*.html' '*.js' '*.css'
You can also run this from outside the website folder:
zip -r foo website_folder -i '*.php' '*.html' '*.js' '*.css'
You can use find and grep to generate the file list, then pipe that into zip
e.g.
find . | egrep "\.(html|css|js|php)$" | zip -# test.zip
(-# tells zip to read a file list from stdin)
This is how I managed to do it, but I also like ghostdog74's version.
tar -czvf archive.tgz `find test/ | egrep ".*\.html|.*\.php"`
You can add extra extensions by adding them to the regex.
I liked Nick's answer, but, since this is a programming site, why not use Ant to do this. :)
Then you can put in a parameter so that different types of files can be zipped up.
http://ant.apache.org/manual/Tasks/zip.html
you may want to use find(GNU) to find all your php,html etc files.then tar them up
find /path -type f \( -iname "*.php" -o -iname "*.css" -o -iname "*.js" -o -iname "*.ext" \) -exec tar -r --file=test.tar "{}" +;
after that you can zip it up
You could write a shell script to copy files based on a pattern/expression into a new folder, zip the contents and then delete the folder. Now, as for the actual syntax of it, ill leave that to you :D.

Unable to move all files except directories to a folder in terminal

I have extended regexes enabled in my Bash by
shopt -s extglob
They may be useful in solving the problem.
I run the following unsuccessfully, since it moves also directories
$ mv `find . -maxdepth 1` django-tes/
I am trying to find all files except directories and move them to a directory called django-tes/.
How can you move all files except directories in a folder to a folder in terminal?
Try using find . -type f -maxdepth 1

Resources