Finding all .cpp and .h files and moving them to ~/junk

find

I created a folder on my home directory call junk, and let's say I want to move all .cpp and .h files to it. How would I do it? My first thought is to start with find ~ -name *.cpp -print, but I don't know how to put in multiple patterns into the find argument, and I'm fairly lost after that.

Best Answer

portably:

cd &&
  find . -path ./junk -prune -o -type f \( \
    -name '*.h' -o -name '*.cpp' \) -exec sh -c '
      exec mv -i "$@" junk' sh {} +

Above excluding the junk folder itself from the search.

We're only removing regular files (-type f). There may be other types of files you want to move like symlinks, but beware that moving symlinks often break them.

The -i is a safeguard to avoid two files with the same name overriding each other.

Related Question