简体   繁体   English

在BASH Shell脚本中创建文件和目录

[英]Creating files and directories in BASH Shell Scripting

I have a few hundred files that I want to sort into subdirectories. 我有几百个文件要分类到子目录中。 For each 2-letter prefix, I want to create a new directory, and copy into it all the files that begin with that prefix, stripping the prefix as I go. 对于每个2个字母的前缀,我想创建一个新目录,并将所有以该前缀开头的文件复制到该目录中,并随即删除该前缀。

In other words, 00a -> 00/a and so on. 换句话说, 00a > 00/a ,依此类推。

I have this code: 我有以下代码:

cd try
arr=$( ls )

line=$(echo $arr | tr " " "\n")
for x in $line
do
  if [ ! -d "$x" ]
  then
    s=${x:0:2}
    if [ ! -d "$s" ]
    then
      mkdir "$s"
    fi x=${x:-1:-1}
    mv "$x" "$s"
  fi done

But I get this persistent error: 但是我得到这个持续的错误:

arr - command not found.

Although I have created 200 files successfully, I could not create the new directories (as explained and hence no files). 尽管我已经成功创建了200个文件,但无法创建新目录(如所述,因此没有文件)。

Here's a short script to give the filenames I have: 这是一个简短的脚本,用于提供我拥有的文件名:

#!/bin/bash
if [ ! -d "try" ]
then
    mkdir "try"
fi
cd try/
for x in {00..07}
do
    for y in {a..z}
    do
        touch $x$y
    done
done
cd ..
for i in [0-9][0-9]?*
do
    d=${i::2}
    test -d "$d" || mkdir "$d"
    mv "$i" "$d/${i:2}"
done

You may want set -e earlier in the script to hard-fail early if mkdir or mv fail, or you may want to push on with the remainder of the files - your choice. 如果mkdirmv失败,则可能希望set -e脚本中的set -e早期硬故障,或者您可以继续处理文件的其余部分-您的选择。

I tested this in a new, clean directory: 我在一个干净的新目录中对此进行了测试:

$ touch {00..20}{a..z}; for i in [0-9][0-9]?*; do d=${i::2}; test -d "$d" || mkdir "$d"; mv "$i" "$d/${i:2}"; done; ls -R
.:
00  01  02  03  04  05  06  07  08  09  10  11  12  13  14  15  16  17  18  19  20

./00:
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

./01:
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

./02:
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

./03:
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

./04:
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

./05:
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

./06:
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

./07:
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

./08:
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

./09:
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

./10:
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

./11:
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

./12:
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

./13:
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

./14:
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

./15:
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

./16:
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

./17:
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

./18:
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

./19:
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

./20:
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

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

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