Ubuntu – A script for moving files from subdirs to parent dir

bashcommand linescripts

I am looking for a script for moving files from subdirs to the parent directory

My folder structure looks like this.

  • Inside of a folder A I have a lot of folders named 1,2,3,4,5…
  • All these folders contain 2 more folders named B and C.
  • B and C contain files that I want to move one level up, then delete folders B and C.

Thank you in advance for any help you can give me with this script

Best Answer

You can use:

find test/*/* -type d | xargs -n1  sh -c 'echo mv -b ${0}/* "$( dirname ${0})" ";" rm -rvf ${0} '

It will just print Output on screen as below :

mv -b test/1/B/file1 test/1/B/file2 test/1/B/file3 test/1 ; rm -rvf test/1/B
mv -b test/1/C/file1 test/1/C/file2 test/1/C/file3 test/1 ; rm -rvf test/1/C
mv -b test/2/B/file1 test/2/B/file2 test/2/B/file3 test/2 ; rm -rvf test/2/B
mv -b test/2/C/file1 test/2/C/file2 test/2/C/file3 test/2 ; rm -rvf test/2/C
mv -b test/3/B/file1 test/3/B/file2 test/3/B/file3 test/3 ; rm -rvf test/3/B
mv -b test/3/C/file1 test/3/C/file2 test/3/C/file3 test/3 ; rm -rvf test/3/C
mv -b test/4/B/file1 test/4/B/file2 test/4/B/file3 test/4 ; rm -rvf test/4/B
mv -b test/4/C/file1 test/4/C/file2 test/4/C/file3 test/4 ; rm -rvf test/4/C
mv -b test/5/B/file1 test/5/B/file2 test/5/B/file3 test/5 ; rm -rvf test/5/B
mv -b test/5/C/file1 test/5/C/file2 test/5/C/file3 test/5 ; rm -rvf test/5/C

If output looks OK then you can simply append | sh at the end of that command, then it will run command which is shows in output.

Related Question