[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: cp output format



Quoting Haines Brown (haines@histomat.net):

> Andrew, thanks for your addition to this interesting
> thread. Unfortunately the implication is that no simple command will
> copy a file to another directory and avoid clash by sequentially
> numbering the copies. It will be simpler just to live with the "~"
> signs.

If the "sequentialness" (where you wouldn't delete foo.~3~ even if it was
identical to foo.~2~ and foo.~4~) is not important, and filenamelength
is not critical, you could consider adding a more "meaningful" suffix
to the filename. For example, this command adds the file's own
modification timestamp.

for j in *[0-9]~ ; do mv -i "$j" "$j$(ls -bog1 --time-style=+'"-%Y%m%d-%H%M%S"' "$j" | cut -f 2 -d '"')" ; done

As written here, the ~N~ is left in the middle of the filename, but
could be removed with bash's ${parameter%word} expansion. A side-effect
(as long as ~N~ is removed) is that an unmodified backup will provoke
a name collision, which mv -i will then show you. The quoting deals
with spaces in filenames, but I haven't bothered with really wacky
characters (even though I put -b in the ls).

The one-step method is, of course, to forget --backup altogether:

for j in file ; do cp -ip "$j" ".../destination/$j$(ls -bog1 --time-style=+'"-%Y%m%d-%H%M%S"' "$j" | cut -f 2 -d '"')" ; done

Example with cp (and using bash completion to type the filename!):

$ ls -og 27*
-rw-r----- 1 148033383 Jan 17  2014 27c3 - Desktop on the Linux... (and BSD, of course) - Wolfgang Draxinger (+ Lennart Poettering)-ZTdUmlGxVo0.mp4

$ for j in 27c3\ -\ Desktop\ on\ the\ Linux...\ \(and\ BSD\,\ of\ course\)\ -\ Wolfgang\ Draxinger\ \(+\ Lennart\ Poettering\)-ZTdUmlGxVo0.mp4 ; do cp -ip "$j" "/tmp/$j$(ls -bog1 --time-style=+'"-%Y%m%d-%H%M%S"' "$j" | cut -f 2 -d '"')" ; done

$ ls -og /tmp/27*
-rw-r----- 1 148033383 Jan 17  2014 27c3 - Desktop on the Linux... (and BSD, of course) - Wolfgang Draxinger (+ Lennart Poettering)-ZTdUmlGxVo0.mp4-20140117-160615

Cheers,
David.


Reply to: