Move files and folders recursively on Linux

Unless I am misunderstanding the question, this would work:

mv /public-back/templates/* /public/templates

Also, unless you have a huge list of files, adding -i will ask before it overwrites anything, which add some safety when using wildcards like *.


The man page for cp states:

-p same as --preserve=mode,ownership,timestamps
-r same as --recursive=copy directories recursively

Try;

cp -rp /public-back/templates/* /public/templates/

When moving items from my thumb drive to my OSMC system, I've found the following very useful:

find /media/Pi\ Hard\ 16GB/ -name '*' -exec mv -v {} /media/External\ HDD/Videos/ \;

Explanation on how it works below.

BTW, Don't forget to add a backslash before any spaces in the source or destination directory names (see above).

find  finds all files and folders in the destination path.

/media/Pi Hard 16GB/ is the path searched. Escape special char such as spaces.

-name '*' filters on names. If you do not escape or quote this then 
          the shell will expand it before find sees it.

-exec     Executes a command, in our case mv

-v        Verbose, so you can see what's happening (optional)

{}        is replaced by the name of the found object.

Effectively, you are finding all files and all folders and moving them one by one (or if a directory gets found first, you are moving that directory and the contents in it). This starts a new process for each move and is very inefficient. Only use this when the regular commands fail.


mv doesn't seem to do this. But you can use this little trick, works like a charm:

tar cf - . |(cd /targetdir; tar xvf -)

and preserves permissions and all.

Note: none of the above worked for me, that's why this workaround.