Извлечение zip из терминала с переименованием при имени файла?

Я следую этому вопросу, чтобы извлечь zip. Есть ли способ автоматического добавления числа после того, как файл был извлечен, когда есть существующий файл с тем же именем?

Например, у меня есть example.zip с файлом с именем contents.csv. Моя директория такова:

contents.csv example.zip

В настоящее время, когда я разархивирую папку, мне будет предложено:

$ unzip example.zip Archive: example.zip replace contents.csv? [y]es, [n]o, [A]ll, [N]one, [r]ename: y

Вместо этого я просто хочу запустить unzip example.zip, затем иметь номер (-1) автоматически добавляется к имени файла в случае такого столкновения:

contents-1.csv contents.csv example.zip

Запуск unzip example.zip снова даст следующее:

contents-2.csv contents-1.csv contents.csv example.zip

Some Программное обеспечение GUI уже делает это, но я хочу сделать это из сценария.

1
задан 28 June 2017 в 05:27

3 ответа

Вы можете сделать это с помощью переключателя -B.

   -B     [when compiled with UNIXBACKUP defined] save a  backup  copy  of
          each  overwritten  file. The backup file is gets the name of the
          target file with a tilde and optionally a unique sequence number
          (up to 5 digits) appended.  The sequence number is applied when‐
          ever another file with the  original  name  plus  tilde  already
          exists.   When used together with the "overwrite all" option -o,
          numbered backup files are  never  created.  In  this  case,  all
          backup  files  are  named  as the original file with an appended
          tilde, existing backup files are deleted without  notice.   This
          feature  works  similarly to the default behavior of emacs(1) in
          many locations.

          Example: the old copy of ``foo'' is renamed to ``foo~''.

          Warning: Users should be aware that the -B option does not  pre‐
          vent  loss  of existing data under all circumstances.  For exam‐
          ple, when unzip  is  run  in  overwrite-all  mode,  an  existing
          ``foo~'' file is deleted before unzip attempts to rename ``foo''
          to ``foo~''.  When this rename attempt fails (because of a  file
          locks,  insufficient  privileges,  or  ...),  the  extraction of
          ``foo~'' gets cancelled, but the  old  backup  file  is  already
          lost.   A  similar scenario takes place when the sequence number
          range for numbered backup files gets exhausted (99999, or  65535
          for  16-bit  systems).   In  this case, the backup file with the
          maximum sequence number is  deleted  and  replaced  by  the  new
          backup version without notice.

Пример:

terrance@terrance-ubuntu:~/testdir$ ls
20161203_202646.jpg  20170610_110407-PANO.jpg
20161225_150835.jpg  98percenteffective.jpg
20170204_192943.jpg  Home_Is_dog_hair.jpg
20170204_193007.jpg  images.jpg
20170204_193309.jpg  img_7230.jpg
20170204_194206.jpg  ps_2013_12_29___11_47_19_fixed.jpg
20170322_230834.jpg  ps_2013_12_29___11_47_19.jpg
20170401_200805.jpg  unnamed.jpg
-------------------------------------------------------------- 06/27/17 20:43:56
terrance@terrance-ubuntu:~/testdir$ zip newfiles.zip *
  adding: 20161203_202646.jpg (deflated 1%)
  adding: 20161225_150835.jpg (deflated 0%)
  adding: 20170204_192943.jpg (deflated 0%)
  adding: 20170204_193007.jpg (deflated 0%)
  adding: 20170204_193309.jpg (deflated 0%)
  adding: 20170204_194206.jpg (deflated 3%)
  adding: 20170322_230834.jpg (deflated 1%)
  adding: 20170401_200805.jpg (deflated 0%)
  adding: 20170610_110407-PANO.jpg (deflated 1%)
  adding: 98percenteffective.jpg (deflated 1%)
  adding: Home_Is_dog_hair.jpg (deflated 4%)
  adding: images.jpg (deflated 0%)
  adding: img_7230.jpg (deflated 0%)
  adding: ps_2013_12_29___11_47_19_fixed.jpg (deflated 1%)
  adding: ps_2013_12_29___11_47_19.jpg (deflated 0%)
  adding: unnamed.jpg (deflated 0%)
-------------------------------------------------------------- 06/27/17 20:44:03
terrance@terrance-ubuntu:~/testdir$ unzip -B newfiles.zip 
Archive:  newfiles.zip
  inflating: 20161203_202646.jpg     
  inflating: 20161225_150835.jpg     
  inflating: 20170204_192943.jpg     
  inflating: 20170204_193007.jpg     
  inflating: 20170204_193309.jpg     
  inflating: 20170204_194206.jpg     
  inflating: 20170322_230834.jpg     
  inflating: 20170401_200805.jpg     
  inflating: 20170610_110407-PANO.jpg  
  inflating: 98percenteffective.jpg  
  inflating: Home_Is_dog_hair.jpg    
  inflating: images.jpg              
  inflating: img_7230.jpg            
  inflating: ps_2013_12_29___11_47_19_fixed.jpg  
  inflating: ps_2013_12_29___11_47_19.jpg  
  inflating: unnamed.jpg             
-------------------------------------------------------------- 06/27/17 20:44:12
terrance@terrance-ubuntu:~/testdir$ ls
20161203_202646.jpg       20170610_110407-PANO.jpg~
20161203_202646.jpg~      98percenteffective.jpg
20161225_150835.jpg       98percenteffective.jpg~
20161225_150835.jpg~      Home_Is_dog_hair.jpg
20170204_192943.jpg       Home_Is_dog_hair.jpg~
20170204_192943.jpg~      images.jpg
20170204_193007.jpg       images.jpg~
20170204_193007.jpg~      img_7230.jpg
20170204_193309.jpg       img_7230.jpg~
20170204_193309.jpg~      newfiles.zip
20170204_194206.jpg       ps_2013_12_29___11_47_19_fixed.jpg
20170204_194206.jpg~      ps_2013_12_29___11_47_19_fixed.jpg~
20170322_230834.jpg       ps_2013_12_29___11_47_19.jpg
20170322_230834.jpg~      ps_2013_12_29___11_47_19.jpg~
20170401_200805.jpg       unnamed.jpg
20170401_200805.jpg~      unnamed.jpg~
20170610_110407-PANO.jpg
-------------------------------------------------------------- 06/27/17 20:44:13
terrance@terrance-ubuntu:~/testdir$ unzip -B newfiles.zip 
Archive:  newfiles.zip
  inflating: 20161203_202646.jpg     
  inflating: 20161225_150835.jpg     
  inflating: 20170204_192943.jpg     
  inflating: 20170204_193007.jpg     
  inflating: 20170204_193309.jpg     
  inflating: 20170204_194206.jpg     
  inflating: 20170322_230834.jpg     
  inflating: 20170401_200805.jpg     
  inflating: 20170610_110407-PANO.jpg  
  inflating: 98percenteffective.jpg  
  inflating: Home_Is_dog_hair.jpg    
  inflating: images.jpg              
  inflating: img_7230.jpg            
  inflating: ps_2013_12_29___11_47_19_fixed.jpg  
  inflating: ps_2013_12_29___11_47_19.jpg  
  inflating: unnamed.jpg             
-------------------------------------------------------------- 06/27/17 20:44:16
terrance@terrance-ubuntu:~/testdir$ ls
20161203_202646.jpg       20170610_110407-PANO.jpg~
20161203_202646.jpg~      20170610_110407-PANO.jpg~1
20161203_202646.jpg~1     98percenteffective.jpg
20161225_150835.jpg       98percenteffective.jpg~
20161225_150835.jpg~      98percenteffective.jpg~1
20161225_150835.jpg~1     Home_Is_dog_hair.jpg
20170204_192943.jpg       Home_Is_dog_hair.jpg~
20170204_192943.jpg~      Home_Is_dog_hair.jpg~1
20170204_192943.jpg~1     images.jpg
20170204_193007.jpg       images.jpg~
20170204_193007.jpg~      images.jpg~1
20170204_193007.jpg~1     img_7230.jpg
20170204_193309.jpg       img_7230.jpg~
20170204_193309.jpg~      img_7230.jpg~1
20170204_193309.jpg~1     newfiles.zip
20170204_194206.jpg       ps_2013_12_29___11_47_19_fixed.jpg
20170204_194206.jpg~      ps_2013_12_29___11_47_19_fixed.jpg~
20170204_194206.jpg~1     ps_2013_12_29___11_47_19_fixed.jpg~1
20170322_230834.jpg       ps_2013_12_29___11_47_19.jpg
20170322_230834.jpg~      ps_2013_12_29___11_47_19.jpg~
20170322_230834.jpg~1     ps_2013_12_29___11_47_19.jpg~1
20170401_200805.jpg       unnamed.jpg
20170401_200805.jpg~      unnamed.jpg~
20170401_200805.jpg~1     unnamed.jpg~1
20170610_110407-PANO.jpg

Надеюсь, что это поможет!

1
ответ дан 22 May 2018 в 21:05

Вы можете сделать это с помощью переключателя -B.

-B [when compiled with UNIXBACKUP defined] save a backup copy of each overwritten file. The backup file is gets the name of the target file with a tilde and optionally a unique sequence number (up to 5 digits) appended. The sequence number is applied when‐ ever another file with the original name plus tilde already exists. When used together with the "overwrite all" option -o, numbered backup files are never created. In this case, all backup files are named as the original file with an appended tilde, existing backup files are deleted without notice. This feature works similarly to the default behavior of emacs(1) in many locations. Example: the old copy of ``foo'' is renamed to ``foo~''. Warning: Users should be aware that the -B option does not pre‐ vent loss of existing data under all circumstances. For exam‐ ple, when unzip is run in overwrite-all mode, an existing ``foo~'' file is deleted before unzip attempts to rename ``foo'' to ``foo~''. When this rename attempt fails (because of a file locks, insufficient privileges, or ...), the extraction of ``foo~'' gets cancelled, but the old backup file is already lost. A similar scenario takes place when the sequence number range for numbered backup files gets exhausted (99999, or 65535 for 16-bit systems). In this case, the backup file with the maximum sequence number is deleted and replaced by the new backup version without notice.

Пример:

terrance@terrance-ubuntu:~/testdir$ ls 20161203_202646.jpg 20170610_110407-PANO.jpg 20161225_150835.jpg 98percenteffective.jpg 20170204_192943.jpg Home_Is_dog_hair.jpg 20170204_193007.jpg images.jpg 20170204_193309.jpg img_7230.jpg 20170204_194206.jpg ps_2013_12_29___11_47_19_fixed.jpg 20170322_230834.jpg ps_2013_12_29___11_47_19.jpg 20170401_200805.jpg unnamed.jpg -------------------------------------------------------------- 06/27/17 20:43:56 terrance@terrance-ubuntu:~/testdir$ zip newfiles.zip * adding: 20161203_202646.jpg (deflated 1%) adding: 20161225_150835.jpg (deflated 0%) adding: 20170204_192943.jpg (deflated 0%) adding: 20170204_193007.jpg (deflated 0%) adding: 20170204_193309.jpg (deflated 0%) adding: 20170204_194206.jpg (deflated 3%) adding: 20170322_230834.jpg (deflated 1%) adding: 20170401_200805.jpg (deflated 0%) adding: 20170610_110407-PANO.jpg (deflated 1%) adding: 98percenteffective.jpg (deflated 1%) adding: Home_Is_dog_hair.jpg (deflated 4%) adding: images.jpg (deflated 0%) adding: img_7230.jpg (deflated 0%) adding: ps_2013_12_29___11_47_19_fixed.jpg (deflated 1%) adding: ps_2013_12_29___11_47_19.jpg (deflated 0%) adding: unnamed.jpg (deflated 0%) -------------------------------------------------------------- 06/27/17 20:44:03 terrance@terrance-ubuntu:~/testdir$ unzip -B newfiles.zip Archive: newfiles.zip inflating: 20161203_202646.jpg inflating: 20161225_150835.jpg inflating: 20170204_192943.jpg inflating: 20170204_193007.jpg inflating: 20170204_193309.jpg inflating: 20170204_194206.jpg inflating: 20170322_230834.jpg inflating: 20170401_200805.jpg inflating: 20170610_110407-PANO.jpg inflating: 98percenteffective.jpg inflating: Home_Is_dog_hair.jpg inflating: images.jpg inflating: img_7230.jpg inflating: ps_2013_12_29___11_47_19_fixed.jpg inflating: ps_2013_12_29___11_47_19.jpg inflating: unnamed.jpg -------------------------------------------------------------- 06/27/17 20:44:12 terrance@terrance-ubuntu:~/testdir$ ls 20161203_202646.jpg 20170610_110407-PANO.jpg~ 20161203_202646.jpg~ 98percenteffective.jpg 20161225_150835.jpg 98percenteffective.jpg~ 20161225_150835.jpg~ Home_Is_dog_hair.jpg 20170204_192943.jpg Home_Is_dog_hair.jpg~ 20170204_192943.jpg~ images.jpg 20170204_193007.jpg images.jpg~ 20170204_193007.jpg~ img_7230.jpg 20170204_193309.jpg img_7230.jpg~ 20170204_193309.jpg~ newfiles.zip 20170204_194206.jpg ps_2013_12_29___11_47_19_fixed.jpg 20170204_194206.jpg~ ps_2013_12_29___11_47_19_fixed.jpg~ 20170322_230834.jpg ps_2013_12_29___11_47_19.jpg 20170322_230834.jpg~ ps_2013_12_29___11_47_19.jpg~ 20170401_200805.jpg unnamed.jpg 20170401_200805.jpg~ unnamed.jpg~ 20170610_110407-PANO.jpg -------------------------------------------------------------- 06/27/17 20:44:13 terrance@terrance-ubuntu:~/testdir$ unzip -B newfiles.zip Archive: newfiles.zip inflating: 20161203_202646.jpg inflating: 20161225_150835.jpg inflating: 20170204_192943.jpg inflating: 20170204_193007.jpg inflating: 20170204_193309.jpg inflating: 20170204_194206.jpg inflating: 20170322_230834.jpg inflating: 20170401_200805.jpg inflating: 20170610_110407-PANO.jpg inflating: 98percenteffective.jpg inflating: Home_Is_dog_hair.jpg inflating: images.jpg inflating: img_7230.jpg inflating: ps_2013_12_29___11_47_19_fixed.jpg inflating: ps_2013_12_29___11_47_19.jpg inflating: unnamed.jpg -------------------------------------------------------------- 06/27/17 20:44:16 terrance@terrance-ubuntu:~/testdir$ ls 20161203_202646.jpg 20170610_110407-PANO.jpg~ 20161203_202646.jpg~ 20170610_110407-PANO.jpg~1 20161203_202646.jpg~1 98percenteffective.jpg 20161225_150835.jpg 98percenteffective.jpg~ 20161225_150835.jpg~ 98percenteffective.jpg~1 20161225_150835.jpg~1 Home_Is_dog_hair.jpg 20170204_192943.jpg Home_Is_dog_hair.jpg~ 20170204_192943.jpg~ Home_Is_dog_hair.jpg~1 20170204_192943.jpg~1 images.jpg 20170204_193007.jpg images.jpg~ 20170204_193007.jpg~ images.jpg~1 20170204_193007.jpg~1 img_7230.jpg 20170204_193309.jpg img_7230.jpg~ 20170204_193309.jpg~ img_7230.jpg~1 20170204_193309.jpg~1 newfiles.zip 20170204_194206.jpg ps_2013_12_29___11_47_19_fixed.jpg 20170204_194206.jpg~ ps_2013_12_29___11_47_19_fixed.jpg~ 20170204_194206.jpg~1 ps_2013_12_29___11_47_19_fixed.jpg~1 20170322_230834.jpg ps_2013_12_29___11_47_19.jpg 20170322_230834.jpg~ ps_2013_12_29___11_47_19.jpg~ 20170322_230834.jpg~1 ps_2013_12_29___11_47_19.jpg~1 20170401_200805.jpg unnamed.jpg 20170401_200805.jpg~ unnamed.jpg~ 20170401_200805.jpg~1 unnamed.jpg~1 20170610_110407-PANO.jpg

Надеюсь, что это поможет!

1
ответ дан 18 July 2018 в 11:00

Вы можете сделать это с помощью переключателя -B.

-B [when compiled with UNIXBACKUP defined] save a backup copy of each overwritten file. The backup file is gets the name of the target file with a tilde and optionally a unique sequence number (up to 5 digits) appended. The sequence number is applied when‐ ever another file with the original name plus tilde already exists. When used together with the "overwrite all" option -o, numbered backup files are never created. In this case, all backup files are named as the original file with an appended tilde, existing backup files are deleted without notice. This feature works similarly to the default behavior of emacs(1) in many locations. Example: the old copy of ``foo'' is renamed to ``foo~''. Warning: Users should be aware that the -B option does not pre‐ vent loss of existing data under all circumstances. For exam‐ ple, when unzip is run in overwrite-all mode, an existing ``foo~'' file is deleted before unzip attempts to rename ``foo'' to ``foo~''. When this rename attempt fails (because of a file locks, insufficient privileges, or ...), the extraction of ``foo~'' gets cancelled, but the old backup file is already lost. A similar scenario takes place when the sequence number range for numbered backup files gets exhausted (99999, or 65535 for 16-bit systems). In this case, the backup file with the maximum sequence number is deleted and replaced by the new backup version without notice.

Пример:

terrance@terrance-ubuntu:~/testdir$ ls 20161203_202646.jpg 20170610_110407-PANO.jpg 20161225_150835.jpg 98percenteffective.jpg 20170204_192943.jpg Home_Is_dog_hair.jpg 20170204_193007.jpg images.jpg 20170204_193309.jpg img_7230.jpg 20170204_194206.jpg ps_2013_12_29___11_47_19_fixed.jpg 20170322_230834.jpg ps_2013_12_29___11_47_19.jpg 20170401_200805.jpg unnamed.jpg -------------------------------------------------------------- 06/27/17 20:43:56 terrance@terrance-ubuntu:~/testdir$ zip newfiles.zip * adding: 20161203_202646.jpg (deflated 1%) adding: 20161225_150835.jpg (deflated 0%) adding: 20170204_192943.jpg (deflated 0%) adding: 20170204_193007.jpg (deflated 0%) adding: 20170204_193309.jpg (deflated 0%) adding: 20170204_194206.jpg (deflated 3%) adding: 20170322_230834.jpg (deflated 1%) adding: 20170401_200805.jpg (deflated 0%) adding: 20170610_110407-PANO.jpg (deflated 1%) adding: 98percenteffective.jpg (deflated 1%) adding: Home_Is_dog_hair.jpg (deflated 4%) adding: images.jpg (deflated 0%) adding: img_7230.jpg (deflated 0%) adding: ps_2013_12_29___11_47_19_fixed.jpg (deflated 1%) adding: ps_2013_12_29___11_47_19.jpg (deflated 0%) adding: unnamed.jpg (deflated 0%) -------------------------------------------------------------- 06/27/17 20:44:03 terrance@terrance-ubuntu:~/testdir$ unzip -B newfiles.zip Archive: newfiles.zip inflating: 20161203_202646.jpg inflating: 20161225_150835.jpg inflating: 20170204_192943.jpg inflating: 20170204_193007.jpg inflating: 20170204_193309.jpg inflating: 20170204_194206.jpg inflating: 20170322_230834.jpg inflating: 20170401_200805.jpg inflating: 20170610_110407-PANO.jpg inflating: 98percenteffective.jpg inflating: Home_Is_dog_hair.jpg inflating: images.jpg inflating: img_7230.jpg inflating: ps_2013_12_29___11_47_19_fixed.jpg inflating: ps_2013_12_29___11_47_19.jpg inflating: unnamed.jpg -------------------------------------------------------------- 06/27/17 20:44:12 terrance@terrance-ubuntu:~/testdir$ ls 20161203_202646.jpg 20170610_110407-PANO.jpg~ 20161203_202646.jpg~ 98percenteffective.jpg 20161225_150835.jpg 98percenteffective.jpg~ 20161225_150835.jpg~ Home_Is_dog_hair.jpg 20170204_192943.jpg Home_Is_dog_hair.jpg~ 20170204_192943.jpg~ images.jpg 20170204_193007.jpg images.jpg~ 20170204_193007.jpg~ img_7230.jpg 20170204_193309.jpg img_7230.jpg~ 20170204_193309.jpg~ newfiles.zip 20170204_194206.jpg ps_2013_12_29___11_47_19_fixed.jpg 20170204_194206.jpg~ ps_2013_12_29___11_47_19_fixed.jpg~ 20170322_230834.jpg ps_2013_12_29___11_47_19.jpg 20170322_230834.jpg~ ps_2013_12_29___11_47_19.jpg~ 20170401_200805.jpg unnamed.jpg 20170401_200805.jpg~ unnamed.jpg~ 20170610_110407-PANO.jpg -------------------------------------------------------------- 06/27/17 20:44:13 terrance@terrance-ubuntu:~/testdir$ unzip -B newfiles.zip Archive: newfiles.zip inflating: 20161203_202646.jpg inflating: 20161225_150835.jpg inflating: 20170204_192943.jpg inflating: 20170204_193007.jpg inflating: 20170204_193309.jpg inflating: 20170204_194206.jpg inflating: 20170322_230834.jpg inflating: 20170401_200805.jpg inflating: 20170610_110407-PANO.jpg inflating: 98percenteffective.jpg inflating: Home_Is_dog_hair.jpg inflating: images.jpg inflating: img_7230.jpg inflating: ps_2013_12_29___11_47_19_fixed.jpg inflating: ps_2013_12_29___11_47_19.jpg inflating: unnamed.jpg -------------------------------------------------------------- 06/27/17 20:44:16 terrance@terrance-ubuntu:~/testdir$ ls 20161203_202646.jpg 20170610_110407-PANO.jpg~ 20161203_202646.jpg~ 20170610_110407-PANO.jpg~1 20161203_202646.jpg~1 98percenteffective.jpg 20161225_150835.jpg 98percenteffective.jpg~ 20161225_150835.jpg~ 98percenteffective.jpg~1 20161225_150835.jpg~1 Home_Is_dog_hair.jpg 20170204_192943.jpg Home_Is_dog_hair.jpg~ 20170204_192943.jpg~ Home_Is_dog_hair.jpg~1 20170204_192943.jpg~1 images.jpg 20170204_193007.jpg images.jpg~ 20170204_193007.jpg~ images.jpg~1 20170204_193007.jpg~1 img_7230.jpg 20170204_193309.jpg img_7230.jpg~ 20170204_193309.jpg~ img_7230.jpg~1 20170204_193309.jpg~1 newfiles.zip 20170204_194206.jpg ps_2013_12_29___11_47_19_fixed.jpg 20170204_194206.jpg~ ps_2013_12_29___11_47_19_fixed.jpg~ 20170204_194206.jpg~1 ps_2013_12_29___11_47_19_fixed.jpg~1 20170322_230834.jpg ps_2013_12_29___11_47_19.jpg 20170322_230834.jpg~ ps_2013_12_29___11_47_19.jpg~ 20170322_230834.jpg~1 ps_2013_12_29___11_47_19.jpg~1 20170401_200805.jpg unnamed.jpg 20170401_200805.jpg~ unnamed.jpg~ 20170401_200805.jpg~1 unnamed.jpg~1 20170610_110407-PANO.jpg

Надеюсь, что это поможет!

1
ответ дан 24 July 2018 в 19:42

Другие вопросы по тегам:

Похожие вопросы: