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 ..
arr: command not founderror message does not make sense with the code you posted There seems to be a danglingdoneafter thefiin 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.)