Unix mv --backup = numbered

I am trying to move a folder in php, but keep both files in the dest folder if there is a duplicate. I tried to do this in recursion, but too complicated, so many things can go wrong, such as with files and duplicate files / folders.

im trying to work with the system () command, and I cannot figure out how to move files, but keep a backup if duplicate without destroying the extension

$last_line = system('mv --backup=t websites/test/ websites/test2/', $retval); 

gives the following if the file exists in both directories:

 ajax.html~ ajax.html~1 ajax.html~2 

Are looking for:

 ajax~.html ajax~1.html ajax~2.html 

or any others like (1), (2) ... but without destroying the file extension. any ideas? you are welcome.

ps should use the system () command.

+4
source share
5 answers

if you want to save the source files and just create a copy, use cp not mv .

If you want to create a backup archive then make tar gzip from a folder like this

tar -pczf name_of_your_archive.tar.gz /path/to/directory/to/backup

0
source
 rsync --ignore-existing --remove-source-files /path/to/source /path/to/dest 
0
source

Use rsync with the --backup and --backup-dir . eg:

 rsync -a --backup --backup-dir /usr/local/backup/2013/03/20/ /path/to/source /path/to/dest 

Each time a file can be overwritten, it is copied to the specified folder plus the path to this item. for example: /path/to/dest/path/to/source/file.txt

0
source

From the looks of things, there seems to be no built-in method for backing up files, keeping the extension in the right place. It may be wrong, but I could not find one that does not do what your original question already indicated.

Since you said that it is difficult to copy files using php, maybe you can do it the same way you do it right now, getting the files in the format

 ajax.html~ ajax.html~1 ajax.html~2 

Then, using PHP, you need to parse the files and rename them to the desired format. This way, you don’t have to deal with permissions and duplicate files that you mentioned complications. You just need to search for files with this format and rename them.

0
source

I do not answer strictly your question, but the case that I present here is very common and, therefore, valid!

Here is my hack!

USE FILES:

 #!/bin/bash # It will find all the files according to the arguments in # "<YOUR_ARGUMENT_TO_FIND_FILES>" ("find" command) and move them to the # "<DEST_FOLDER>" folder. Files with the same name will follow the pattern: # "same_name.ext", "same_name (1).ext", "same_name (2).ext", # "same_name (3).ext"... cd <YOUR_TARGET_FOLDER> mkdir ./<DEST_FOLDER> find ./ -iname "<YOUR_ARGUMENT_TO_FIND_FILES>" -type f -print0 | xargs -0 -I "{}" sh -c 'cp --backup=numbered "{}" "./<DEST_FOLDER>/" && rm -f "{}"' cd ./<DEST_FOLDER> for f_name in *.~*~; do f_bak_ext="${f_name##*.}" f_bak_num="${f_bak_ext//[^0-9]/}" f_orig_name="${f_name%.*}" f_only_name="${f_orig_name%.*}" f_only_ext="${f_orig_name##*.}" mv "$f_name" "$f_only_name ($f_bak_num).$f_only_ext" done cd .. 

USE WITH FOLDERS:

 #!/bin/bash # It will find all the folders according to the arguments in # "<YOUR_ARGUMENT_TO_FIND_FOLDERS>" ("find" command) and move them to the # "<DEST_FOLDER>" folder. Folders with the same name will have their contents # merged, however files with the same name WILL NOT HAVE DUPLICATES (example: # "same_name.ext", "same_name (1).ext", "same_name (2).ext", # "same_name (3).ext"...). cd <YOUR_TARGET_FOLDER> find ./ -path "./<DEST_FOLDER>" -prune -o -iname "<YOUR_ARGUMENT_TO_FIND_FOLDERS>" -type d -print0 | xargs -0 -I "{}" sh -c 'rsync -a "{}" "./<DEST_FOLDER>/" && rm -rf "{}"' 
0
source

All Articles