zoukankan      html  css  js  c++  java
  • shell拷贝原文件到目标对应文件夹

     1 [root@localhost mv_test]# cat mv.sh
     2 #!/bin/bash
     3 
     4 DIR=/root/linshi/move
     5 FILE=/root/linshi/test/mv
     6 if [ -d $DIR ];then
     7     rm -rf $DIR/*
     8 fi
     9 if [ -d $FILE ];then
    10     rm -rf $FILE/*
    11 fi
    12 mkdir $DIR/{1..30}
    13 touch $FILE/{1..30}dinsoaul
    14 ls $FILE|sort -n > a.txt; ls $DIR |sort -n > b.txt;paste a.txt b.txt > paste.txt
    15 cat paste.txt |while read filename dirname
    16 do
    17     echo "$filename $dirname" &>/dev/null
    18     cp -a $FILE/$filename $DIR/$dirname
    19 done
    20 [root@localhost mv_test]# ls /root/linshi/test/mv
    21 10dinsoaul  13dinsoaul  16dinsoaul  19dinsoaul  21dinsoaul  24dinsoaul  27dinsoaul  2dinsoaul   4dinsoaul  7dinsoaul
    22 11dinsoaul  14dinsoaul  17dinsoaul  1dinsoaul   22dinsoaul  25dinsoaul  28dinsoaul  30dinsoaul  5dinsoaul  8dinsoaul
    23 12dinsoaul  15dinsoaul  18dinsoaul  20dinsoaul  23dinsoaul  26dinsoaul  29dinsoaul  3dinsoaul   6dinsoaul  9dinsoaul
    24 [root@localhost mv_test]# tree /root/linshi/move/
    25 /root/linshi/move/
    26 ├── 1
    27 │?? └── 1dinsoaul
    28 ├── 10
    29 │?? └── 10dinsoaul
    30 ├── 11
    31 │?? └── 11dinsoaul
    32 ├── 12
    33 │?? └── 12dinsoaul
    34 ├── 13
    35 │?? └── 13dinsoaul
    36 ├── 14
    37 │?? └── 14dinsoaul
    38 ├── 15
    39 │?? └── 15dinsoaul
    40 ├── 16
    41 │?? └── 16dinsoaul
    42 ├── 17
    43 │?? └── 17dinsoaul
    44 ├── 18
    45 │?? └── 18dinsoaul
    46 ├── 19
    47 │?? └── 19dinsoaul
    48 ├── 2
    49 │?? └── 2dinsoaul
    50 ├── 20
    51 │?? └── 20dinsoaul
    52 ├── 21
    53 │?? └── 21dinsoaul
    54 ├── 22
    55 │?? └── 22dinsoaul
    56 ├── 23
    57 │?? └── 23dinsoaul
    58 ├── 24
    59 │?? └── 24dinsoaul
    60 ├── 25
    61 │?? └── 25dinsoaul
    62 ├── 26
    63 │?? └── 26dinsoaul
    64 ├── 27
    65 │?? └── 27dinsoaul
    66 ├── 28
    67 │?? └── 28dinsoaul
    68 ├── 29
    69 │?? └── 29dinsoaul
    70 ├── 3
    71 │?? └── 3dinsoaul
    72 ├── 30
    73 │?? └── 30dinsoaul
    74 ├── 4
    75 │?? └── 4dinsoaul
    76 ├── 5
    77 │?? └── 5dinsoaul
    78 ├── 6
    79 │?? └── 6dinsoaul
    80 ├── 7
    81 │?? └── 7dinsoaul
    82 ├── 8
    83 │?? └── 8dinsoaul
    84 └── 9
    85     └── 9dinsoaul
    86 
    87 30 directories, 30 files
    88 [root@localhost mv_test]# 

    需求:将文件分别拷贝到对应的文件夹中(例如:将数字为1的文件拷贝到文件夹1中)

    cat paste.txt |while read filename dirname
    do
         echo "$filename $dirname" &>/dev/null
    done
    知识点:while读取一行,将数据赋值给2个变量。
  • 相关阅读:
    微服务2.0时代,论其痛点与触点
    微服务架构的中国式落地
    【干货】微服务技术栈选型手册2.0
    每位开发者都该看:如何在四十岁后还能继续从事软件开发?
    在IBM学到的东西,到底对我的程序生涯产生了多大的影响
    十年程序员老兵告诉你,2018年程序员如何发展
    IntelliJ IDEA 快捷键大全
    List<Integer>.remove()的一个小细节
    eclipse 设置 @author @version等注释模板
    Android 获取当前应用的版本号和当前系统的版本号
  • 原文地址:https://www.cnblogs.com/xiaofeng666/p/12792803.html
Copyright © 2011-2022 走看看