首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我想找到所有的report.txt并将它们粘贴到另一个重命名的文件夹中

我想找到所有的report.txt并将它们粘贴到另一个重命名的文件夹中
EN

Stack Overflow用户
提问于 2021-09-15 02:18:36
回答 2查看 69关注 0票数 0

我在磁盘中的不同文件夹中有大量的report.txt文件。有些文件被命名为Report.txt,有些文件的后缀为reportmonday.txt。因此,如果已经存在,我希望找到所有包含报表的文件并将其复制到一个具有重命名的文件夹中。类似于report.txt报告1.txt报告2.txt

我试过命令

找出来。-type f -iname 'report‘-exec mv {}_renamed /home/ram/allreport*

此命令不重命名我的文件名,而是对文件进行重正。请帮帮我

EN

回答 2

Stack Overflow用户

发布于 2021-09-15 03:26:20

解决这个问题的一种有点费解的方法:

find . -type f -iname \*report\* | awk 'BEGIN{com="ls /home/ram/allreport/*[Rr]eport*|awk -F. \047{print $NF}\047|sort -n|tail -n1"; com | getline result}{result++; printf "mv \"%s\" /home/ram/allreport/report.%s\n",$0,result}'

  • if,上面的输出看起来是明智的,在上面的命令末尾添加一个| bash,然后再次运行它. find . -type f -iname \*report\* | awk 'BEGIN{com="ls /home/ram/allreport/*[Rr]eport*|awk -F. \047{print $NF}\047|sort -n|tail -n1"; com | getline result}{result++; printf "mv \"%s\" /home/ram/allreport/report.%s\n",$0,result}'|bash

这里是awk部分w/解释性词的一个漂亮版本:

代码语言:javascript
复制
BEGIN { # prerequisite action, this happens before we check what we "find"
        com = "ls /home/ram/allreport/*[Rr]eport*|awk -F. '{print $NF}'|sort -n|tail -n1"  # we execute an ls on the target directory to get the highest existing report number
        com | getline result                                            # and store the number in the variable result
}

{  # here the processing of find's output starts
        result++                                                        # increase result by one for each line 
        printf "mv \"%s\" /home/ram/allreport/report.%s\n", $0, result  # print the command we want to execute, 
                                                                        # a mv of the original file to the target directory with a new name and the counter "result" tacked on.
}
票数 2
EN

Stack Overflow用户

发布于 2021-09-15 15:39:24

下面的脚本将检查任何文件,其中包含“报表”(大小写不敏感)一词,并将复制到所需的目录。如果一个文件已经以该名称存在于该目录下,它将尝试重命名report1.txtreport2.txt等,如果它不能将其重命名为该数字,它将指定一个随机后缀。就像report1354.txt。请注意,它将检查目录是否存在。如果目录不存在,脚本将创建一个目录。

代码语言:javascript
复制
#!/bin/bash

# make sure the below variable does not contain tailing '/'
# it should be '/home/tfp/allreports' not '/home/tfp/allreports/'
ALL_REPORTS_DIRECTORY="/home/tfp/allreports"
FILE_NAMES_ARRAY=$(find . -type f -name "*[rR][eE][pP][oO][rR][tT]*" 2>>/dev/null)
RENAME_SUFFIX=1

if [[ -d ${ALL_REPORTS_DIRECTORY} ]]; then
    echo "directory exists. not creating"
else
    mkdir ${ALL_REPORTS_DIRECTORY}
    echo "directory doesn't exists. creating"
fi

# to do give random suffix to files under subfolder
for file in ${FILE_NAMES_ARRAY}; do
    file_name=$(basename $file)
    if [[ -f "${ALL_REPORTS_DIRECTORY}/${file_name}" ]]; then
        if [[ -f "${ALL_REPORTS_DIRECTORY}/report${RENAME_SUFFIX}.txt" ]]; then
            random_suffix=$RANDOM
            echo "  couldn't rename '${file_name}'. suffix '${RENAME_SUFFIX}' already exists so giving random suffix"
            cp $file ${ALL_REPORTS_DIRECTORY}/report${random_suffix}.txt
            echo "  '$file' -> new name: report${random_suffix}.txt"
        else
            echo "file '${file}' exists under '${ALL_REPORTS_DIRECTORY}'. renaming to report${RENAME_SUFFIX}.txt"
            cp $file ${ALL_REPORTS_DIRECTORY}/report${RENAME_SUFFIX}.txt
            echo "  '$file' -> new name: report${RENAME_SUFFIX}.txt"
        fi
        RENAME_SUFFIX=$(echo $((${RENAME_SUFFIX}+1)))
    else
        cp $file ${ALL_REPORTS_DIRECTORY}
        echo "copied '${file}'"
    fi
done

下面是一些输出示例。

代码语言:javascript
复制
$ls

myreport.txt  report1.txt  report3.txt  report55.txt  report6.txt  Report.txt  script.sh  sundayReport.txt  testfolder
代码语言:javascript
复制
$bash script.sh

directory exists. not creating
copied './Report.txt'
copied './testfolder/rePorT.txt'
copied './testfolder/RReport.txt'
copied './myreport.txt'
copied './sundayReport.txt'
copied './report1.txt'
copied './report6.txt'
copied './report55.txt'
copied './report3.txt'
代码语言:javascript
复制
$bash script.sh

directory exists. not creating
  couldn't rename 'Report.txt'. suffix '1' already exists so giving random suffix
  './Report.txt' -> new name: report3262.txt
file './testfolder/rePorT.txt' exists under '/home/tfp/allreports'. renaming to report2.txt
  './testfolder/rePorT.txt' -> new name: report2.txt
  couldn't rename 'RReport.txt'. suffix '3' already exists so giving random suffix
  './testfolder/RReport.txt' -> new name: report6693.txt
file './myreport.txt' exists under '/home/tfp/allreports'. renaming to report4.txt
  './myreport.txt' -> new name: report4.txt
file './sundayReport.txt' exists under '/home/tfp/allreports'. renaming to report5.txt
  './sundayReport.txt' -> new name: report5.txt
  couldn't rename 'report1.txt'. suffix '6' already exists so giving random suffix
  './report1.txt' -> new name: report31626.txt
file './report6.txt' exists under '/home/tfp/allreports'. renaming to report7.txt
  './report6.txt' -> new name: report7.txt
file './report55.txt' exists under '/home/tfp/allreports'. renaming to report8.txt
  './report55.txt' -> new name: report8.txt
file './report3.txt' exists under '/home/tfp/allreports'. renaming to report9.txt
  './report3.txt' -> new name: report9.txt
代码语言:javascript
复制
$bash script.sh

directory exists. not creating
  couldn't rename 'Report.txt'. suffix '1' already exists so giving random suffix
  './Report.txt' -> new name: report32658.txt
  couldn't rename 'rePorT.txt'. suffix '2' already exists so giving random suffix
  './testfolder/rePorT.txt' -> new name: report1976.txt
  couldn't rename 'RReport.txt'. suffix '3' already exists so giving random suffix
  './testfolder/RReport.txt' -> new name: report26897.txt
  couldn't rename 'myreport.txt'. suffix '4' already exists so giving random suffix
  './myreport.txt' -> new name: report31297.txt
  couldn't rename 'sundayReport.txt'. suffix '5' already exists so giving random suffix
  './sundayReport.txt' -> new name: report29608.txt
  couldn't rename 'report1.txt'. suffix '6' already exists so giving random suffix
  './report1.txt' -> new name: report7259.txt
  couldn't rename 'report6.txt'. suffix '7' already exists so giving random suffix
  './report6.txt' -> new name: report16691.txt
  couldn't rename 'report55.txt'. suffix '8' already exists so giving random suffix
  './report55.txt' -> new name: report12176.txt
  couldn't rename 'report3.txt'. suffix '9' already exists so giving random suffix
  './report3.txt' -> new name: report11087.txt

来自目标目录的最终ls

代码语言:javascript
复制
$ls -lah

total 8,0K
drwxrwxr-x  2 tfp tfp 4,0K Sep 15 18:37 .
drwxr-xr-x 46 tfp tfp 4,0K Sep 15 18:32 ..
-rw-rw-r--  1 tfp tfp    0 Sep 15 18:36 myreport.txt
-rw-rw-r--  1 tfp tfp    0 Sep 15 18:37 report11087.txt
-rw-rw-r--  1 tfp tfp    0 Sep 15 18:37 report12176.txt
-rw-rw-r--  1 tfp tfp    0 Sep 15 18:37 report16691.txt
-rw-rw-r--  1 tfp tfp    0 Sep 15 18:37 report1976.txt
-rw-rw-r--  1 tfp tfp    0 Sep 15 18:36 report1.txt
-rw-rw-r--  1 tfp tfp    0 Sep 15 18:37 report26897.txt
-rw-rw-r--  1 tfp tfp    0 Sep 15 18:37 report29608.txt
-rw-rw-r--  1 tfp tfp    0 Sep 15 18:36 report2.txt
-rw-rw-r--  1 tfp tfp    0 Sep 15 18:37 report31297.txt
-rw-rw-r--  1 tfp tfp    0 Sep 15 18:36 report31626.txt
-rw-rw-r--  1 tfp tfp    0 Sep 15 18:36 report3262.txt
-rw-rw-r--  1 tfp tfp    0 Sep 15 18:37 report32658.txt
-rw-rw-r--  1 tfp tfp    0 Sep 15 18:36 report3.txt
-rw-rw-r--  1 tfp tfp    0 Sep 15 18:36 report4.txt
-rw-rw-r--  1 tfp tfp    0 Sep 15 18:36 report55.txt
-rw-rw-r--  1 tfp tfp    0 Sep 15 18:36 report5.txt
-rw-rw-r--  1 tfp tfp    0 Sep 15 18:36 report6693.txt
-rw-rw-r--  1 tfp tfp    0 Sep 15 18:36 report6.txt
-rw-rw-r--  1 tfp tfp    0 Sep 15 18:37 report7259.txt
-rw-rw-r--  1 tfp tfp    0 Sep 15 18:36 report7.txt
-rw-rw-r--  1 tfp tfp    0 Sep 15 18:36 report8.txt
-rw-rw-r--  1 tfp tfp    0 Sep 15 18:36 report9.txt
-rw-rw-r--  1 tfp tfp    0 Sep 15 18:36 rePorT.txt
-rw-rw-r--  1 tfp tfp    0 Sep 15 18:36 Report.txt
-rw-rw-r--  1 tfp tfp    0 Sep 15 18:36 RReport.txt
-rw-rw-r--  1 tfp tfp    0 Sep 15 18:36 sundayReport.txt
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69186380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档