Home > database >  How to move files from subdirectory to a directory using a for loop
How to move files from subdirectory to a directory using a for loop

Time:11-04

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"/*
done

CodePudding user response:

Like that:

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

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)

  • Related