Score:1

I would like to organize ~2000 subfolders by Country

pe flag

I have a folder with a lot of subfolders starting with his country name in ISO Alpha-2 (US,GB,etc) i have made a basic bash script

#!/bin/bash
echo Folder name:
read country 
mkdir $country 
echo Prefix of folders:
read folder
mv $folder* $country
echo done

for organize but is quite exhausting one by one, I'm looking for a way to automatically do this, i guess that i will need a full country list but i don't know exactly how to do it.

terdon avatar
cn flag
Please [edit] your question and i) show us the actual subdirectory names and ii) show us the result you want from your example input. You can use the `tree` command to see and share the folder structure here.
ar flag
It is not clear if you want to move the folder `GB` to `Great Britain` or `Great Britain` to `GB`. What do you want to do about space?
Score:2
kp flag

You can use the find command to loop over all subfolders:

find . -type d -exec ./myscript.sh {} \;

This will pass the name of each folder path as variable $1 to the script. You can use the sed command to extract the relevant parts of the path from $1, e.g.

#!/bin/sh

country=$( echo $1 | sed -e 's/[^\/]*\/\(..\).*/\1/' )
remainder=$( echo $1 | sed -e 's/[^\/]*\/..\(.*\)/\1/' )

# There must be a remainder.
echo $remainder | grep '.' > /dev/null || exit
# Ignore subdirectories.
echo $remainder | grep '\/' > /dev/null && exit

echo "$1 => $country/$remainder"
mkdir -p $country
mv $1 $country/$remainder
Score:1
jp flag

Run this for a dry-run:

for d in */
do
    echo mkdir -p "${d:0:2}" && echo mv -n -- "$d" "${d:0:2}"
done

Then, when satisfied with the output, remove the two echo commands and run it again to do the real work.

Dani avatar
pe flag
This is making the trick, looking how to mark the question as solved.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.