Emm*_*maV 3 bash terminal zip filenames arguments
我有一个包含数百个 zip 文件的目录。我希望能够选择大约 50 个文件并在终端中使用unzip(或7z)一次提取它们。这些文件的名称完全不同,因此我无法使用正则表达式来匹配所有文件。
作为一个简化的例子,我有以下三个文件:
41a02d81b1c7b6225b11908c38b820cc.zip
d581697699c4321c32b733a9678.zip
ffb7077a319b2d168d5415b0f59a9e2ba3a.zip
Run Code Online (Sandbox Code Playgroud)
我只想提取其中的两个,而无需手动输入命令。
有没有办法使用 GUI 选择多个文件,然后将列表导出到 bash 以供命令处理?
使用zsh, shell,并提供您:
setopt auto_menu autolist correct no_list_ambiguous
setopt list_types auto_pushd no_list_beep
zstyle ':completion:*' completer _expand _complete _ignored _correct _approximate _prefix
zstyle ':completion:*' format 'Completing %d'
zstyle ':completion:*' group-name ''
eval "$(dircolors)"
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*' matcher-list '' 'm:{a-z}={A-Z}' 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=* l:|=*'
zstyle ':completion:*' max-errors 1 not-numeric
zstyle ':completion:*' menu select=2
zstyle ':completion:*' original true
zstyle ':completion:*' prompt 'correct> '
zstyle ':completion:*' select-prompt '%SScrolling active: current selection at %p%s'
zstyle ':completion:*' verbose true
zstyle ':completion:*' menu select=2
autoload -Uz compinit
compinit -i
Run Code Online (Sandbox Code Playgroud)
在您的~/.zshrc(请注意,并非所有这些都在这里相关;并且是通过运行compinstall以调整完成而获得的)中,然后您可以键入:
$ 为 f (*.zipTab
此时,它将列出zip当前目录中的文件,并让您使用箭头键和 选择它们Enter。
如果不是按Enter选择条目,而是按Alt+ A,它将插入当前条目,但也可以让您继续选择更多文件。
最后,文件,按Enter,然后您可以完成命令,例如:
for f (file1.zip file2.zip) unzip -d $f:r $f
Run Code Online (Sandbox Code Playgroud)
对于每个文件,哪个文件会将其解压缩到具有相同名称但没有.zip扩展名(file1.zipin file1/、file2.zipinfile2/等)的目录中。
在zsh/ bash/ ksh93(或使用 GNU printf)中,您可以执行以下操作:
$ printf 'unzip %q\n' ./*.zip > commands
$ vi commands
Run Code Online (Sandbox Code Playgroud)
在vi(或您最喜欢的文本编辑器)中,删除您不想解压缩的文件的行。保存更改。然后运行:
zsh -x ./commands
Run Code Online (Sandbox Code Playgroud)
(替换zsh为您最喜欢的 shell,最好是您printf从中运行命令的那个)。
除了unzip 'file.zip'在该文件中存储命令之外,您还可以只存储文件:
printf '%s\n' ./*.zip > files
Run Code Online (Sandbox Code Playgroud)
并编辑那个files。
并假设没有文件名包含换行符和 GNU xargs:
xargs -d '\n' -tr0a files -n1 unzip
Run Code Online (Sandbox Code Playgroud)
解压缩它们。
与 zsh 类似的方法是做类似的事情,但不是编辑文件,而是使用vared内置函数编辑变量:
list=(*.zip)
Run Code Online (Sandbox Code Playgroud)
$list使用当前目录中的 zip 文件为阵列播种。
vared list
Run Code Online (Sandbox Code Playgroud)
对于ed它var,删除您不想解压缩的条目。然后:
for f ($list) unzip -d $f:r $f
Run Code Online (Sandbox Code Playgroud)
要解压缩$list.