Moving multiple files with same name and renaming them on the fly

爷,独闯天下 提交于 2019-12-19 10:56:21

问题


I have multiple folders in my ubuntu 16.04 with pictures in them. I would like to move all pictures to one folder and rename all files with same name.

I can easily move pictures from first folder, but how do i copy pictures from rest of the folders without destroying (copying over) all existing files with same name?

Is there some handy oneliner that i could use in terminal for this?


回答1:


cp has a useful option --backup=numbered that adds a numbered suffix to the name of a file that would otherwise be clobbered.

If you have a directory tree containing duplicate file names, then you can combine cp with find to collapse the structure. (Disclaimer: I did not test this, so please tread with caution.)

find $SOURCEDIR -type f -exec cp --backup=numbered '{}' $TARGETDIR \;

Thanks go to:

  • https://unix.stackexchange.com/questions/16669/copy-files-with-renaming
  • Copy nested folders contents to one folder recursively (terminal)


来源:https://stackoverflow.com/questions/49152110/moving-multiple-files-with-same-name-and-renaming-them-on-the-fly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!