Issue
I just updated my file structure for my website and trying to find a fast way to move all files into the new setup. The old set up was:
/CO/001/2022/02-18
/CO/002/2022/02-18
...
/CO/999/2022/02-18
Currently all my images are in that last 02-18 folder. I just updated things so that the new directory listing is:
/CO/001/2022-02-18
/CO/002/2022-02-18
...
/CO/999/2022-02-18
The problem is, I want to move everything I had in the old file structure folders (CO//2022/02-18) into the new folders (CO//2022-02-18) without having to manually go through all 999 old folders and dump the content into the new folders.
I tried the following command already:
mv */2022/02-19 */2022-02-19
But that doesn't seem to be working how intended and gave me a "will not overwrite just-created" error and was trying to put files into the wrong folder (images in 001/2022/02-18 were trying to be put into 002/2022-02-18 instead of 001/2022-02-18)
Solution
We cannot make use of the wild card expansion because
mv */2022/02-18 */2022-02-18
will be expanded as:
mv 001/2022/02-18 002/2022/02-18 ... 999/2022/02-18 "*/2022-02-18"
which will not work as you might expect. Would you please try instead:
#!/bin/bash
for d in /CO/*/2022/02-18/; do # loop over the old directories
if [[ -d $d ]]; then # make sure $d is a dirname
new=$(sed -E 's#([0-9]{4})/([0-9]{2}-[0-9]{2}/)$#\1-\2#' <<< "$d")
# modify "*/2022/02-18/" to "*/2022-02-18/"
echo mv -- "$d" "$new" # rename the dirname
old=${d%/}; old=${old%/*} # extract the parent directory name "*/2022"
echo rmdir -- "$old" # remove the empty parent directory
fi
done
It just prints the commands which will be executed as a dry run. If the result
looks good, drop echo
before mv
and rmdir
then run again. (Please note the dry run will output thousands of lines. You do not have to check all
the lines. Just looking over a few lines will be good enough.)
Answered By - tshiono Answer Checked By - Marie Seifert (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.