0

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.

2
  • 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

0

To confirm your existing tree do something like this -

# find . -type d -depth 3 -print | grep -E '20\d+' | sort
./2006/autumn/Course101
./2006/autumn/Course102
./2006/autumn/CourseA103
./2006/fall/Course101
./2006/fall/Course102
./2006/fall/CourseA103
./2006/spring/Course101
./2006/spring/Course102
./2006/spring/CourseA103
./2006/summer/Course101
./2006/summer/Course102
./2006/summer/CourseA103
./2007/autumn/Course101
./2007/autumn/Course102

Then use a script like this

#!/bin/sh

# Takes source directories as input and copies everything to the destination
#!/bin/sh

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


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

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

1
  • 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 .