How to move files from subdirectory to a directory using a for loop


How do I move a file 1 directory up using loop? Can someone maybe help me out?

I tried this:

for dir in */*/
    do mv "$dir"/*


Like that:

for dir in */*/
    echo mv "$dir"* "${dir%/*/}" # Drop the echo after tested it

Note that this will move all files and directories under $dir to the parent directory of the $dir. If you want to move a specific file, replace "$dir"* with "$dir"file where file is the filename of the specific file. Also be very careful where and how you use this code after echo removed (running in the wrong directory would be catastrophic)

Answered By – M. Nejat Aydin

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published