简体   繁体   English

如何使用Shell脚本正确处理文件名

[英]how to process filenames correct with shell script

I want to eval the latest filename inside my latest folder with some shell script. 我想用一些shell脚本评估最新文件夹中的最新文件名。 So im getting the latest folder name and the latest filename inside it: 所以即时通讯获取最新的文件夹名称和其中的最新文件名:

latest_folder=$(ls -td */ | head -n 1)
echo $latest_folder
cd $latest_folder
latest_file="$(ls -t *$str | head -n 1)"
echo $latest_file

folder structure: 文件夹结构:

base_folder
    |
    |---000
    |     |---123.*
    |     |---124.*
    |     |---...
    |     |---999.*
    |
    |---001
    |     |---997.*
    |     |---998.*
    |     |---999.*

Where 001 is the latest folder. 其中001是最新文件夹。 If the latest file inside this folder equals 999.* i need to create a new folder 002. 如果此文件夹中的最新文件等于999. *,我需要创建一个新文件夹002。

Anyone an idea? 有人知道吗?

Your thinking is not far off. 您的想法并不遥远。 I would advise against cd into the latest dire and simply use the same scheme to get the latest file in that folder by appending latest_folder/ with your command substitution. 我建议不要将cd放入最新的目录中,而只需使用相同的方案,通过将latest_folder/替换为您的命令来获取该文件夹中的最新文件。

Once you have the latest_file , you can simply use parameter expansion with substring removal to isolate the leading 3-digit number (eg filenum="${latest_file%%.*}" ). 一旦拥有了latest_file ,您就可以简单地使用参数扩展和去除子字符串来隔离前3位数字(例如, filenum="${latest_file%%.*}" )。 After than you can make your decision on whether a new directory is required based on filenum . 之后,您可以根据filenum决定是否需要新目录。

If you do need to create a new directory, then use can use printf -v to take advantage of the field-width modifier and '0' padding to format your new_folder names a 002 by simply adding 1 to the latest_folder number, eg printf -v new_folder "%03d" $((latest_folder + 1)) note: that is after removing the leading-zeros from latest_folder to prevent interpretation as Octal constants when adding 1 to get the new_folder number, eg $((latest_folder + 1)) . 如果你需要创建一个新的目录,然后使用可以使用printf -v采取现场宽度调节和优势'0'填充格式化你的new_folder名的002只需添加1latest_folder数量,例如printf -v new_folder "%03d" $((latest_folder + 1))注意:那是在从latest_folder删除前导零latest_folder ,当加1以获得new_folder编号时,防止解释为八进制常量,例如$((latest_folder + 1))

Putting it altogether, you should be able to do something similar to: 综上所述,您应该能够执行以下操作:

#!/bin/bash

limit=999

latest_folder="$(ls -td */ | head -n 1)"                ## latest folder
latest_folder="${latest_folder:0:(-1)}"                 ## remove / at end
latest_file="$(ls -t "$latest_folder/" | head -n 1)"    ## latest file
filenum="${latest_file%%.*}"                            ## isolate ###
if ((filenum == limit)); then                           ## if ### = 999
    n=0
    while [ "${latest_folder:n:1}" = '0' ]; do          ## remove zeros
        latest_folder="${latest_folder:$((n+1))}"
        ((n++))
    done
    printf -v new_folder "%03d" $((latest_folder + 1))  ## new folder name
    mkdir -p new_folder                                 ## create new folder
fi

note: you should generally use find and sort to get the list of directories and filenames (using -maxdepth and -type to control the depth and type of file/dir searched for) 注意:通常应使用findsort来获取目录和文件名的列表(使用-maxdepth-type控制搜索到的文件/目录的深度和类型)

Let me know if you have questions. 如果您有任何问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM