Excuse me if this question has been asked/answered before, but my search has not provided the answer I needed.

I have my teaching materials structured as follows: Year/semester/Course/.... Instead of this hierarchy, I want to use Course/year/Semester/.... How would I move the files/folders with a preferable single line bash script? Year: 2006 - 2021 Semester: Fall - Spring Course: 8 different courses (they have several other subfolders, which I want to keep for now)

Thanks in advance for your time and response.

  • Is there any particular reason why it needs to be a single line of code? Apr 1, 2021 at 23:05
  • No particular reason, other then me being a total noob. It might be easier for me to modify it later to reuse it for other variations. That does not however mean, I don't want other solutions. Just a preference. Apr 1, 2021 at 23:36

1 Answer 1


To confirm your existing tree do something like this -

# find . -type d -depth 3 -print | grep -E '20\d+' | sort

Then use a script like this


# Takes source directories as input and copies everything to the destination

makeNewStructure() {
    while read line; do
        newdir=$( echo "$line" |sed -e 's#^\./#/#' -e 's#\(/[^/]*\)\(/[^/]*\)\(/[^/]*\)$#.\3\1\2#' )
        mkdir -p "$newdir"
        cp -r "$line"/* "$newdir"

find . -type d -depth 3 -print | grep -E '/20\d+/' | sort | makeNewStructure 

This is non-destructive - it won't affect the original structure.

  • Thank you for the answer, but the first thing for confirmation gives error: find: warning: you have specified the global option -depth after the argument -type, but global options are not positional, i.e., -depth affects tests specified before it as well as those specified after it. Please specify global options before other arguments.* Apr 4, 2021 at 1:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .