You are not logged in.

#1 2018-09-13 20:23:35

INIROBO
Member
Registered: 2018-09-06
Posts: 2

[SOLVED] Problem with "cp --backup=t" and existing files

Hi,

I'm making a bash script to move some files from several directories to a single directory. So if two files have the same name, one will overwrite the other, so I used "--backup=t/numbered" flag to avoid that.

The problem is that the files with the same name are copied with ".~n~" (n= number) at the end, but I need the number to be before the extension of the file (example, original: "foo.txt.~1~"; what I want: "foo_1.txt", I don't really care about how and where the number is placed while it is not after the extension).

I've been looking on the Internet about this and the only thing that I found was a replay by Byte Commander on unix.stackexchange:

rename 's/((?:\..+)?)\.~(\d+)~$/_$2$1/' *.~*~

This command just does nothing. I get his idea of what this command should be doing but it doesn't work.

I also looked at the cp man page to see if it possible to change the way the number is aded to the filename, but the only thing I see is to customize the suffix but that is not what I want.

Just in case someone asks, I placed the "cp" command inside a loop where I also prepared the number the file to be copied should have in case another one with the same name exists (the idea was to add that number to cp somehow).



So I'm posting this so I could get some help with my problem, just in case it wasn't clear enough smile .

Thank you in advance.



(This is my first post on the Arch forums so I'm not really sure if I should post this question here)

Last edited by INIROBO (2018-09-14 18:29:05)

Offline

#2 2018-09-13 20:34:30

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,541
Website

Re: [SOLVED] Problem with "cp --backup=t" and existing files

cp_num() { # file dest-dir
   fname=$(basenane "$1")
   n=$(find "$2" -name "$fname" | wc -l)
   if [ $n -eq 0 ]; then
      cp "$1" "$2"
   else
      cp "$1" "$2/${fname%.*}$n.${fname##*.}"
   fi
}

Note that this assumes that every file does have an extension (i.e., at least one '.' in the name).  You could test for this if needed under the else condition and if there is no '.' in the name, just move to "$2/$fname$n".

Last edited by Trilby (2018-09-13 20:36:18)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2018-09-14 17:49:44

INIROBO
Member
Registered: 2018-09-06
Posts: 2

Re: [SOLVED] Problem with "cp --backup=t" and existing files

Trilby wrote:
cp_num() { # file dest-dir
   fname=$(basenane "$1")
   n=$(find "$2" -name "$fname" | wc -l)
   if [ $n -eq 0 ]; then
      cp "$1" "$2"
   else
      cp "$1" "$2/${fname%.*}$n.${fname##*.}"
   fi
}

Note that this assumes that every file does have an extension (i.e., at least one '.' in the name).  You could test for this if needed under the else condition and if there is no '.' in the name, just move to "$2/$fname$n".

Thank you for your reply. It was really useful actually. The only problem I found with your version is that, as you said, files with non extension should be also treated and that on the find command, the way you did it, only allows to backup up to 1 file because you only check for the filename, so what I made was take the extension to a variable (with the dot) and then find the word ${filenameWithoutExtension}*${extension}. I tried using regular expresions to be more accurate (* is too general) but I wasn't able to make it work.

I ofc made some more changes, but on a more generic point of view, that's the only thing that would matter.

Offline

Board footer

Powered by FluxBB