I need to move files from a folder to a subfolder and this is a process that can be executed more than once, so the subfolder may not be empty.
Let's say the structure is as follows:
renatospaka@dell-w10home:~/devlpm$ ls -la
total 24
drwxr-xr-x 4 renatospaka renatospaka 4096 Sep 8 12:36 .
drwxr-xr-x 10 renatospaka renatospaka 4096 Sep 8 12:34 ..
drwxr-xr-x 2 renatospaka renatospaka 4096 Sep 8 12:36 api
-rw-r--r-- 1 renatospaka renatospaka 10 Sep 8 12:36 file1.txt
-rw-r--r-- 1 renatospaka renatospaka 10 Sep 8 12:36 file2.txt
drwxr-xr-x 2 renatospaka renatospaka 4096 Sep 8 12:34 new
There is this excellent question from which I extracted the following command: ls | grep -v new | xargs mv -t new
. It really works and moves the content to the new folder. However, from the 2nd execution on, an error pops up because the destination folder is not empty and the process abends.
How to fix this problem? I've tried some flag combinations on xargs
but all failed...
renatospaka@dell-w10home:~/devlpm$ ls | grep -v new | xargs -r mv -f -t new mv: cannot move 'api' to 'new/api': Directory not empty
β Renato Spakauskas Sep 08 '20 at 16:55