There is only one file in one directory.
I'd like to have files from each folder starting from 1_ moved into folder 1, 2_ into 2 etc.
The final structure of directory would look like:
1/file1.tiff
1/file2.tiff
1/file3.tiff
1/file4.tiff
1/file5.tiff
1/file6.tiff
1/file7.tiff
2/file8.tiff
2/file9.tiff
2/file10.tiff
2/file11.tiff
etc.
I've tried with bash scripting, but without any success
#! /bin/bash
# range of directories to look into, change as needed
for d in {1,2}_{1..6}
do
dst=$(echo "$d" | cut -d"_" -f1) # name of destination directory
[ -d "$dst" ] || mkdir "$dst" # create new directory if it does not exist
cp "$d"/* "$dst" 2> /dev/null # copy content to new directory
done
Then obviously it creates additional empty folders.
I modified for loop like:
for d in {33..41}_{0..6}
It would be great that it looks for the directories that actually exists, without typing them into the script manually, each time.
I want also to move the files, and delete source directories, which i suppose will be like:
#! /bin/bash
# range of directories to look into, change as needed
for d in {33..43}_{0..6}
do
dst=$(echo "$d" | cut -d"_" -f1) # name of destination directory
[ -d "$dst" ] || mkdir "$dst" # create new directory if it does not exist
mv "$d"/* "$dst" 2> /dev/null # copy content to new directory
rm -R "$d"
done
Or maybe it will be easier to delete additional empty folders by:
If you can guarantee that every directory in . are part of the range, then you could
#! /bin/bash
# range of directories to look into, change as needed
for d in * # list everything under current directory
do
if test -d "$d"; then # if it's a directory
dst=${d%%_*} # name of destination directory
[ -d "$dst" ] || mkdir "$dst" # create new directory if it does not exist
cp "$d"/* "$dst" 2> /dev/null # copy content to new directory
fi
done
Yes, this is exactly what I was looking for!
Thank you one more time.
# range of directories to look into, change as needed
for d in * # list everything under current directory
do
if test -d "$d"; then # if it's a directory
dst=${d%%_*} # name of destination directory
[ -d "$dst" ] || mkdir "$dst" # create new directory if it does not exist
mv "$d"/* "$dst" 2> /dev/null # move content to new directory
rm -R "$d" # delete the source directory
fi
done