AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 778828
Accepted
Terry
Terry
Asked: 2024-06-23 05:49:46 +0800 CST2024-06-23 05:49:46 +0800 CST 2024-06-23 05:49:46 +0800 CST

在 zsh 中增加文件名

  • 772

我正在尝试用 zsh (Linux Mint 21.3 “Virginia”) 编写一个脚本,将文件从 $source 移动(或复制)到 $target,但结果却令人困惑。我使用 yad 文件对话框来选择文件并命名目标目录。我能做到的最好的就是只将最终选择复制到空目标。

以下是整个脚本


setopt -x
setopt -v
autoload -Uz zmv

# WARNING Just because two or more files from different sources have identical names does not mean 
#that the files are identical.


declare -a array
array=$(yad --width=600 --height=800 \
--title="Choose files to move." \
--file --multiple --separator='\n') 
echo $array

target=$(yad --width=600 --height=800 \
--title="Choose destination." \
--file --directory)
echo "target="$target

# The file you wish to move may already exist in the target directory. 
# If you do not want to overwrite it you can use
# mv -n
# mv --no-clobber
# or 
# mv --backup=numbered
# Clumsy, but better than nothing, and doesn't give me what I want.
# That is why I wrote this script, it adds an incremented number to the name.
# eg <movie>.<ext>
# eg <movie>_copy1.<ext>
# eg <movie>_copy2.<ext>
# Yes, I could use brackets but they are illegal characters and I have a renamer
# that would remove them.

# You can't just use <filename>.$ext to check if the file exists in target because
# you'll miss any existing copies. You have to account for iteration.
for filename in $array; do
unset x
# decompose filename
# just name without path 
filename=$filename:t
echo
echo
echo "filename="$filename
echo
echo
# name without extension
shortname=${filename%.*}
echo "shortname="$shortname
echo
echo
# extension
ext=${filename##*.}
echo
echo
echo "ext="$ext

# The key here is to rename the file (if necessary) BEFORE copying it to target instead 
# of after.
#   Does file already exist in $target? Count them.
x=$(find $target -iname "$shortname*.$ext" | wc -c)
echo
echo
echo "x="$x

# If no file exists, move to target.
# If one file exists then $x will return "1". Think of it as
# <filename>_copy0.<ext>,
# this makes the file you wish to move
# <filename>_copy1.<ext>, - the value of $x.

case $x in
    0) cp $filename $target
    sleep .2
    ;;
    *) mv $filename $shortname_copy$x.$ext
    cp $filename $target
    sleep .2
    ;;
esac
    done
    exit 0

这是使用相同参数两次运行脚本的终端输出。

+/home/revision-6/bin/cpi:4> setopt -v
autoload -Uz zmv
+/home/revision-6/bin/cpi:5> autoload -Uz zmv

# WARNING Just because two or more files from different sources have identical names does not mean 
#that the files are identical.


declare -a array
+/home/revision-6/bin/cpi:11> declare -a array
array=$(yad --width=600 --height=800 \
--title="Choose files to move." \
--file --multiple --separator='\n') 
+/home/revision-6/bin/cpi:12> array=+/home/revision-6/bin/cpi:12> yad '--width=600' '--height=800' '--title=Choose files to move.' --file --multiple '--separator=\n'

(yad:3962282): dbind-WARNING **: 15:34:42.076: Couldn't connect to accessibility bus: Failed to connect to socket /root/.cache/at-spi/bus_0.0: Permission denied
+/home/revision-6/bin/cpi:12> array=$'/home/revision-6/Videos/betty_boop_a_song_a_day_1936.mkv\n/home/revision-6/Videos/betty_boop_is_my_palm_read_1932.mkv\n/home/revision-6/Videos/betty_boop_more_pep_1936.mkv' 
echo $array
+/home/revision-6/bin/cpi:15> echo $'/home/revision-6/Videos/betty_boop_a_song_a_day_1936.mkv\n/home/revision-6/Videos/betty_boop_is_my_palm_read_1932.mkv\n/home/revision-6/Videos/betty_boop_more_pep_1936.mkv'
/home/revision-6/Videos/betty_boop_a_song_a_day_1936.mkv
/home/revision-6/Videos/betty_boop_is_my_palm_read_1932.mkv
/home/revision-6/Videos/betty_boop_more_pep_1936.mkv

target=$(yad --width=600 --height=800 \
--title="Choose destination." \
--file --directory)
+/home/revision-6/bin/cpi:17> target=+/home/revision-6/bin/cpi:17> yad '--width=600' '--height=800' '--title=Choose destination.' --file --directory

(yad:3962312): dbind-WARNING **: 15:34:54.949: Couldn't connect to accessibility bus: Failed to connect to socket /root/.cache/at-spi/bus_0.0: Permission denied
+/home/revision-6/bin/cpi:17> target=/home/revision-6/video_processing 
echo "target="$target
+/home/revision-6/bin/cpi:20> echo 'target=/home/revision-6/video_processing'
target=/home/revision-6/video_processing

# The file you wish to move may already exist in the target directory. 
# If you do not want to overwrite it you can use
# mv -n
# mv --no-clobber
# or 
# mv --backup=numbered
# Clumsy, but better than nothing, and doesn't give me what I want.
# That is why I wrote this script, it adds an incremented number to the name.
# eg <movie>.<ext>
# eg <movie>_copy1.<ext>
# eg <movie>_copy2.<ext>
# Yes, I could use brackets but they are illegal characters and I have a renamer
# that would remove them.

# You can't just use <filename>.$ext to check if the file exists in target because
# you'll miss any existing copies. You have to account for iteration.
for filename in $array; do
unset x
# decompose filename
# just name without path 
filename=$filename:t
echo
echo
echo "filename="$filename
echo
echo
# name without extension
shortname=${filename%.*}
echo "shortname="$shortname
echo
echo
# extension
ext=${filename##*.}
echo
echo
echo "ext="$ext

# The key here is to rename the file (if necessary) BEFORE copying it to target instead 
# of after.
#   Does file already exist in $target? Count them.
x=$(find $target -iname "$shortname*.$ext" | wc -c)
echo
echo
echo "x="$x

# If no file exists, move to target.
# If one file exists then $x will return "1". Think of it as
# <filename>_copy0.<ext>,
# this makes the file you wish to move
# <filename>_copy1.<ext>, - the value of $x.

case $x in
    0) cp $filename $target
    sleep .2
    ;;
    *) mv $filename $shortname_copy$x.$ext
    cp $filename $target
    sleep .2
    ;;
esac
    done
+/home/revision-6/bin/cpi:38> filename=/home/revision-6/Videos/betty_boop_a_song_a_day_1936.mkv
/home/revision-6/Videos/betty_boop_is_my_palm_read_1932.mkv
/home/revision-6/Videos/betty_boop_more_pep_1936.mkv
+/home/revision-6/bin/cpi:39> unset x
+/home/revision-6/bin/cpi:42> filename=betty_boop_more_pep_1936.mkv 
+/home/revision-6/bin/cpi:43> echo

+/home/revision-6/bin/cpi:44> echo

+/home/revision-6/bin/cpi:45> echo 'filename=betty_boop_more_pep_1936.mkv'
filename=betty_boop_more_pep_1936.mkv
+/home/revision-6/bin/cpi:46> echo

+/home/revision-6/bin/cpi:47> echo

+/home/revision-6/bin/cpi:49> shortname=betty_boop_more_pep_1936 
+/home/revision-6/bin/cpi:50> echo 'shortname=betty_boop_more_pep_1936'
shortname=betty_boop_more_pep_1936
+/home/revision-6/bin/cpi:51> echo

+/home/revision-6/bin/cpi:52> echo

+/home/revision-6/bin/cpi:54> ext=mkv 
+/home/revision-6/bin/cpi:55> echo

+/home/revision-6/bin/cpi:56> echo

+/home/revision-6/bin/cpi:57> echo 'ext=mkv'
ext=mkv
+/home/revision-6/bin/cpi:62> x=+/home/revision-6/bin/cpi:62> find /home/revision-6/video_processing -iname 'betty_boop_more_pep_1936*.mkv'
+/home/revision-6/bin/cpi:62> x=+/home/revision-6/bin/cpi:62> wc -c
+/home/revision-6/bin/cpi:62> x=0 
+/home/revision-6/bin/cpi:63> echo

+/home/revision-6/bin/cpi:64> echo

+/home/revision-6/bin/cpi:65> echo 'x=0'
x=0
+/home/revision-6/bin/cpi:73> case 0 (0)
+/home/revision-6/bin/cpi:74> cp betty_boop_more_pep_1936.mkv /home/revision-6/video_processing
+/home/revision-6/bin/cpi:75> sleep .2
    exit 0
+/home/revision-6/bin/cpi:83> exit 0
revision-6@revision6-NYI3 ~/Videos
 % cpi
+/home/revision-6/bin/cpi:4> setopt -v
autoload -Uz zmv
+/home/revision-6/bin/cpi:5> autoload -Uz zmv

# WARNING Just because two or more files from different sources have identical names does not mean 
#that the files are identical.


declare -a array
+/home/revision-6/bin/cpi:11> declare -a array
array=$(yad --width=600 --height=800 \
--title="Choose files to move." \
--file --multiple --separator='\n') 
+/home/revision-6/bin/cpi:12> array=+/home/revision-6/bin/cpi:12> yad '--width=600' '--height=800' '--title=Choose files to move.' --file --multiple '--separator=\n'

(yad:3962420): dbind-WARNING **: 15:35:35.029: Couldn't connect to accessibility bus: Failed to connect to socket /root/.cache/at-spi/bus_0.0: Permission denied
+/home/revision-6/bin/cpi:12> array=$'/home/revision-6/Videos/betty_boop_a_song_a_day_1936.mkv\n/home/revision-6/Videos/betty_boop_is_my_palm_read_1932.mkv\n/home/revision-6/Videos/betty_boop_more_pep_1936.mkv' 
echo $array
+/home/revision-6/bin/cpi:15> echo $'/home/revision-6/Videos/betty_boop_a_song_a_day_1936.mkv\n/home/revision-6/Videos/betty_boop_is_my_palm_read_1932.mkv\n/home/revision-6/Videos/betty_boop_more_pep_1936.mkv'
/home/revision-6/Videos/betty_boop_a_song_a_day_1936.mkv
/home/revision-6/Videos/betty_boop_is_my_palm_read_1932.mkv
/home/revision-6/Videos/betty_boop_more_pep_1936.mkv

target=$(yad --width=600 --height=800 \
--title="Choose destination." \
--file --directory)
+/home/revision-6/bin/cpi:17> target=+/home/revision-6/bin/cpi:17> yad '--width=600' '--height=800' '--title=Choose destination.' --file --directory

(yad:3962450): dbind-WARNING **: 15:35:43.654: Couldn't connect to accessibility bus: Failed to connect to socket /root/.cache/at-spi/bus_0.0: Permission denied
+/home/revision-6/bin/cpi:17> target=/home/revision-6/video_processing 
echo "target="$target
+/home/revision-6/bin/cpi:20> echo 'target=/home/revision-6/video_processing'
target=/home/revision-6/video_processing

# The file you wish to move may already exist in the target directory. 
# If you do not want to overwrite it you can use
# mv -n
# mv --no-clobber
# or 
# mv --backup=numbered
# Clumsy, but better than nothing, and doesn't give me what I want.
# That is why I wrote this script, it adds an incremented number to the name.
# eg <movie>.<ext>
# eg <movie>_copy1.<ext>
# eg <movie>_copy2.<ext>
# Yes, I could use brackets but they are illegal characters and I have a renamer
# that would remove them.

# You can't just use <filename>.$ext to check if the file exists in target because
# you'll miss any existing copies. You have to account for iteration.
for filename in $array; do
unset x
# decompose filename
# just name without path 
filename=$filename:t
echo
echo
echo "filename="$filename
echo
echo
# name without extension
shortname=${filename%.*}
echo "shortname="$shortname
echo
echo
# extension
ext=${filename##*.}
echo
echo
echo "ext="$ext

# The key here is to rename the file (if necessary) BEFORE copying it to target instead 
# of after.
#   Does file already exist in $target? Count them.
x=$(find $target -iname "$shortname*.$ext" | wc -c)
echo
echo
echo "x="$x

# If no file exists, move to target.
# If one file exists then $x will return "1". Think of it as
# <filename>_copy0.<ext>,
# this makes the file you wish to move
# <filename>_copy1.<ext>, - the value of $x.

case $x in
    0) cp $filename $target
    sleep .2
    ;;
    *) mv $filename $shortname_copy$x.$ext
    cp $filename $target
    sleep .2
    ;;
esac
    done
+/home/revision-6/bin/cpi:38> filename=/home/revision-6/Videos/betty_boop_a_song_a_day_1936.mkv
/home/revision-6/Videos/betty_boop_is_my_palm_read_1932.mkv
/home/revision-6/Videos/betty_boop_more_pep_1936.mkv
+/home/revision-6/bin/cpi:39> unset x
+/home/revision-6/bin/cpi:42> filename=betty_boop_more_pep_1936.mkv 
+/home/revision-6/bin/cpi:43> echo

+/home/revision-6/bin/cpi:44> echo

+/home/revision-6/bin/cpi:45> echo 'filename=betty_boop_more_pep_1936.mkv'
filename=betty_boop_more_pep_1936.mkv
+/home/revision-6/bin/cpi:46> echo

+/home/revision-6/bin/cpi:47> echo

+/home/revision-6/bin/cpi:49> shortname=betty_boop_more_pep_1936 
+/home/revision-6/bin/cpi:50> echo 'shortname=betty_boop_more_pep_1936'
shortname=betty_boop_more_pep_1936
+/home/revision-6/bin/cpi:51> echo

+/home/revision-6/bin/cpi:52> echo

+/home/revision-6/bin/cpi:54> ext=mkv 
+/home/revision-6/bin/cpi:55> echo

+/home/revision-6/bin/cpi:56> echo

+/home/revision-6/bin/cpi:57> echo 'ext=mkv'
ext=mkv
+/home/revision-6/bin/cpi:62> x=+/home/revision-6/bin/cpi:62> find /home/revision-6/video_processing -iname 'betty_boop_more_pep_1936*.mkv'
+/home/revision-6/bin/cpi:62> x=+/home/revision-6/bin/cpi:62> wc -c
+/home/revision-6/bin/cpi:62> x=63 
+/home/revision-6/bin/cpi:63> echo

+/home/revision-6/bin/cpi:64> echo

+/home/revision-6/bin/cpi:65> echo 'x=63'
x=63
+/home/revision-6/bin/cpi:73> case 63 (0)
+/home/revision-6/bin/cpi:73> case 63 (*)
+/home/revision-6/bin/cpi:77> mv betty_boop_more_pep_1936.mkv 63.mkv
+/home/revision-6/bin/cpi:78> cp betty_boop_more_pep_1936.mkv /home/revision-6/video_processing
cp: cannot stat 'betty_boop_more_pep_1936.mkv': No such file or directory
+/home/revision-6/bin/cpi:79> sleep .2
    exit 0
+/home/revision-6/bin/cpi:83> exit 0

在我看来,这个逻辑似乎有效。在移动文件之前,计算目标中的迭代次数并重命名文件,但我想我的 shell 脚本编写能力不足以胜任这项任务。欢迎任何帮助。

linux
  • 1 1 个回答
  • 78 Views

1 个回答

  • Voted
  1. Best Answer
    Gairfowl
    2024-06-23T21:33:48+08:002024-06-23T21:33:48+08:00

    zsh使用s 强大的通配符语法代替findand可能会更容易wc。试试这个:

    #!/usr/bin/env zsh
    setopt extendedglob
    
    nextFilename() {
        local fqp=${1:?}
        local -a fileCopies=( ${fqp:r}(_copy<->|).${fqp:e}(#qNn) )
        local latestCopy=${fileCopies[-1]}
    
        if [[ -z $latestCopy ]]; then
            # no matching files at destination, use original filename
            print -r -- $fqp
        elif [[ $latestCopy == (#b)(*)_copy(<->)(.${fqp:e}) ]]; then
            # matching filename with _copyN, add one to number
            print -r -- ${match[1]}_copy$((${match[2]}+1))${match[3]}
        else
            # only has filename without a copy number, use _copy1
            print -r -- ${fqp:r}_copy1.${fqp:e}
        fi
    }
    

    关键声明如下:

    local -a fileCopies=( ${fqp:r}(_copy<->|).${fqp:e}(#qNn) )
    

    它从目标目录中创建与源文件名的模式匹配的排序文件名数组。

    • ${fqp:r}- 文件路径的根目录。它包含目录和名称的前导部分,即除扩展名之外的所有内容。
    • (_copy<->|)- _copyN,或者什么都没有(例如fname.ext或fname_copy14.ext)。<->是任意数字的 glob 模式。
    • ${fqp:e}- 文件扩展名。
    • (#qNn)- 一组修改行为的 glob 限定符
      • #q- 表示这个括号内的表达式包含 glob 限定符。
      • N- 允许空 glob,这样零匹配就不会出现错误。
      • n- 为该扩展启用 NUMERIC_GLOB_SUBST,以便结果按数字排序。使用此选项,一组名称(如)f1 f11 f2将按排序f1 f2 f11。同一位置没有数字的文件名(如fname.ext和)fname_copy1.ext仍将按词汇顺序排序,但这是我们想要的顺序,因此不需要特殊处理。

    对数组进行排序后,数组中的最后一个值 (index -1) 将是编号最高的文件名,因此我们可以使用它来确定下一步该做什么。使用这种技术的一个优点是它可以处理编号中的间隙;基于计数的算法可能会因此而出错。

    使用/测试:

    >> destDir=./dest
    >> ls -l $destDir/*
    -rw-r--r--  1 me  staff  0 Jun 23 07:40 ./dest/filename.txt
    -rw-r--r--  1 me  staff  0 Jun 23 07:40 ./dest/filename_copy1.txt
    -rw-r--r--  1 me  staff  0 Jun 23 07:40 ./dest/filename_copy11.txt
    -rw-r--r--  1 me  staff  0 Jun 23 07:40 ./dest/filename_copy2.txt
    -rw-r--r--  1 me  staff  0 Jun 23 07:40 ./dest/onefile.txt
    
    >> srcFile=./src/filename.txt
    >> cp -v $srcFile $(nextFilename $destDir/${srcFile:t})
    ./src/filename.txt -> ./dest/filename_copy12.txt
    
    >> srcFile=./src/nodestfile.txt
    >> cp -v $srcFile $(nextFilename $destDir/${srcFile:t})
    ./src/nodestfile.txt -> ./dest/nodestfile.txt
    
    >> srcFile=onefile.txt
    >> cp -v $srcFile $(nextFilename $destDir/${srcFile:t})
    ./src/onefile.txt -> ./dest/onefile_copy1.txt
    

    我们可以使用此函数来构建一个循环遍历文件名数组的脚本:

    #!/usr/bin/env zsh
    setopt extendedglob
    
    #####
    local sourceFilePattern='~/src/*.mp4'
    vared -eh -p "Source Filename or Pattern > " sourceFilePattern
    if ((${ret::=$?})); then
        print "vared returned $ret; exiting ..."
        exit 1
    fi
    # expand the pattern
    local -a sourceFilenames=( ${~sourceFilePattern} )
    
    #####
    local destDir='~/dest'
    vared -eh -p "Target directory > " destDir
    if ((${ret::=$?})); then
        print "vared returned $ret; exiting ..."
        exit 1
    fi
    # expand '~', '*', etc.
    local -a destDirArray=( ${~destDir} )
    if ((${#destDirArray} > 1)); then
        print "Found more than one match for directory pattern $destDir"
        print "exiting ..."
        exit 1
    fi
    destDir=${destDirArray[1]}
    if [[ ! -d $destDir ]]; then
        print "Target $destDir does not exist or is not a directory."
        print "exiting ..."
        exit 1
    fi
    
    #####
    print
    print "Source file pattern: ${sourceFilePattern}"
    print "Source file count:   ${#sourceFilenames}"
    print "Destination:         ${destDir}"
    if ! read -q '?proceed (y/n)? '; then
        print;print "exiting ..."
        exit 1
    fi
    print
    
    #####
    nextFilename() {
        # copy from above
    }
    for src in "${sourceFilenames[@]}"; do
        # use the source file basename to determine the next name
        local dest=$(nextFilename $destDir/${src:t})
        print
        print "Executing: cp -v $src $dest"
        print "  Source directory: ${src:h}"
        print "  Source name:      ${src:t}"
        print "  Target directory: ${dest:h}"
        print "  Target name:      ${dest:t}"
        cp -v "$src" "$dest"
    done
    
    • 4

相关问题

  • 有没有办法让 ls 只显示某些目录的隐藏文件?

  • 使用键盘快捷键启动/停止 systemd 服务 [关闭]

  • 需要一些系统调用

  • astyle 不会更改源文件格式

  • 通过标签将根文件系统传递给linux内核

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve