Shr*_*ut1 2 bash find string printf
我正在尝试从使用以下 bash 脚本生成的文件列表中删除文件扩展名:
#!/bin/bash
file_list=$(find . -type f) #assuming that the files are stored in the same directory
trimmed_file_list=$(printf '%s\n' "${file_list[@]%.*}")
printf '%s\n' "${trimmed_file_list[@]}"
Run Code Online (Sandbox Code Playgroud)
此扩展从列表中的最后一个条目中删除扩展名,但不会删除任何较早的条目。
例如,我想要以下列表:
file1.pdf
file2.pdf
file3.png
Run Code Online (Sandbox Code Playgroud)
成为
file1
file2
file3
Run Code Online (Sandbox Code Playgroud)
但相反,我得到:
file1.pdf
file2.pdf
file3
Run Code Online (Sandbox Code Playgroud)
我不想在循环中执行此操作,我想使用参数扩展。我想避免剪切,因为我只想删除文件中的最后一个扩展名。
有很多关于参数扩展的主题,似乎 bash 可能会因为find
使用换行符而窒息......我真的不确定。如果另一个预先存在的主题解释了这个问题,我不完全理解发生了什么。
似乎相关但似乎无法解决我的问题的主题:
您可以在一个命令中完成所有操作。
find /path/to -type f -execdir bash -c 'printf "%s\n" "${@%.*}"' bash {} +
Run Code Online (Sandbox Code Playgroud)