Shubham Goyal
Shubham Goyal

Reputation: 13

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.

In other words, 00a -> 00/a and so on.

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).

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 ..

Upvotes: 0

Views: 102

Answers (1)

Toby Speight
Toby Speight

Reputation: 30841

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.

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

Upvotes: 1

Related Questions