0

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 ..
6
  • 2
    A screenshot is of no use to us. Please paste the code in the question Commented Feb 3, 2016 at 11:53
  • 2
    Please don't post images of code - paste your actual code into the question. It's easier to read, easier to copy and test, and easier for search engines to index (meaning that more people can benefit from your question). Thank you. Commented Feb 3, 2016 at 11:53
  • 1
    From snapshot of code we only get the knowledge that you like to play counter strike 1.6... Please post code instead of snapshot :) Commented Feb 3, 2016 at 11:56
  • code has been posted .. Commented Feb 3, 2016 at 12:02
  • 1
    The arr: command not found error message does not make sense with the code you posted There seems to be a dangling done after the fi in the first code snippet, but I assume that's a paste error. (Your second paste had similar errors which were not present in the screen shot.) Commented Feb 3, 2016 at 13:09

1 Answer 1

1
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
Sign up to request clarification or add additional context in comments.

1 Comment

the code has worked fine but in my case it is posing a partial problem: it only moves 00a, 01a, 002,..07a..it should also move other files as i created 208 files in all(208 combinations of 00..07 and a..z).. how to correct this error..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.