0

I kind of got stuck in a situation. I wanted to copy certain files from one directory to another (non recursive).

I have multiple files with extensions like .txt, .so, .so.mem, .so.lib, .lib and multiple directories in a directory called base. I'd like to copy all the files non-recursively (only from the base directory) to another directory called test.

I did the following:

Try 1 
pushd $base
find -not -name '*.so' -not -name '*.so.*' -type f -print() -exec rm -f {} +
cp -f $base/* $test

In above try the find has somehow deleted all except .so, even though I have written -not -name '*.so.*' i.e. files .so.mem and .so.lib should not be deleted.

Am I doing something wrong ?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Nepal12
  • 583
  • 1
  • 12
  • 29
  • try it with `! -name '*.so'` instead of the not to add: here is the url maybe it helps you [link](http://stackoverflow.com/questions/1313590/bash-copy-all-files-except-one) – SeRu Feb 04 '16 at 15:40
  • Works for me: `touch a.txt a.so a.so.mem a.so.lib a.lib ; find -not -name '*.so' -not -name '*.so.*' -type f` only prints `a.lib` and `a.txt`. Next time, run without `-exec` first and check the output. – choroba Feb 04 '16 at 15:42
  • 1
    Never risk losing files; always run your code without risking damage before running the code with an active `-exec rm -f {} +`. Put an `echo` in front of the `rm` if nothing else. This is even more important if you're working with root privileges. Exploring what an inaccurate `rm` will do if you're running as root is not a good idea. – Jonathan Leffler Feb 04 '16 at 15:56

1 Answers1

0

If I understood right, that's what you want:

mkdir test
# move all required files to the 'test' directory
mv base/*.txt base/*.so base/*.so.* base/*.lib test
# delete all remaining files from the 'base' directory
rm base/*

In the end, you'll have an empty base directory (assuming no hidden files) and a test directory full of *.txt, *.so and *.lib files.

Andrea Corbellini
  • 17,339
  • 3
  • 53
  • 69