Related questions to missing files after mv
don't appear to answer my question. The following commands were run within the working directory where the files are located, resulting in missing files.
Step 1
I have a sorted array of file names $layers
created with layers=$(ls cropped* | sort -nk1.9)
:
IFS=$'\n';for file in $layers; do echo $file; done
cropped_1.png
cropped_2-a.png
cropped_2-b.png
cropped_2.png
cropped_3-a.png
cropped_3-b.png
cropped_3.png
cropped_4-a.png
cropped_4-b.png
cropped_4-c.png
cropped_4-d.png
cropped_4-e.png
cropped_4-f.png
cropped_4-g.png
cropped_4.png
cropped_5-a.png
cropped_5-b.png
cropped_5.png
--More--
Step 2
I ran the following loop to replace everything following the _ in each file name:
i=1;IFS=$'\n';for file in $layers; do echo ${file/#*_*/cropped_$((i++)).png}; done
cropped_1.png
cropped_2.png
cropped_3.png
cropped_4.png
cropped_5.png
cropped_6.png
cropped_7.png
cropped_8.png
cropped_9.png
cropped_10.png
cropped_11.png
cropped_12.png
cropped_13.png
cropped_14.png
cropped_15.png
cropped_16.png
cropped_17.png
--More--
Step 3 - Danger, Will Robinson!
This looked good so far. So I attempted to rename each file with mv
using the following loop, which I expected would result in something like mv cropped_2-a.png cropped_2.png
, then mv cropped_2-b.png cropped_3.png
, and so on:
i=1;IFS=$'\n';for file in $layers; do mv $file ${file/#*_*/cropped_$((i++)).png}; done
This gave a single error for the first file.
mv: ‘cropped_1.png’ and ‘cropped_1.png’ are the same file
I assumed it would give this error for each file name where mv SOURCE DEST
matched. Instead, it only gave the error on the first file in the loop.
The strange part is that a number of files are now missing. The only remaining files are as follows:
ls cropped* | sort -nk1.9
cropped_1.png
cropped_141.png
cropped_142.png
cropped_143.png
cropped_144.png
cropped_145.png
cropped_146.png
cropped_147.png
cropped_148.png
cropped_149.png
cropped_150.png
cropped_151.png
cropped_152.png
cropped_153.png
cropped_154.png
cropped_155.png
Question
Where did the missing files move to?
The were 140 files before I created a few duplicates with names such as cropped_2-a.png
resulting in 155 files in total. If files 141-155 moved successfully, where are files 2-140? Why are they not move to the expected destination; the current working directory?
Notes
If I do not add files with such names as cropped_2-a.png
, then the loop will display the error message for each file and the files remain unchanged:
i=1;IFS=$'\n';for file in $layers; do mv $file ${file/#*_*/cropped_$((i++)).png}; done
mv: ‘cropped_1.png’ and ‘cropped_1.png’ are the same file
mv: ‘cropped_2.png’ and ‘cropped_2.png’ are the same file
mv: ‘cropped_3.png’ and ‘cropped_3.png’ are the same file
mv: ‘cropped_4.png’ and ‘cropped_4.png’ are the same file
mv: ‘cropped_5.png’ and ‘cropped_5.png’ are the same file
--More--
I've tested with file names such as cropped_2a.png
(no -
) and it duplicates the missing files problem.
When I try with mv -v
to see what is going on, it appears to be renaming the files as expected, yet when I check, the files are missing. Wrapping the parameter substitution1 ${file/#*_*/cropped_$((i++)).png}
in double quotes does not solve this.
i=1;IFS=$'\n';for file in $layers; do mv -v $file "${file/#*_*/cropped_$((i++)).png}"; done
‘cropped_2-a.png’ -> ‘cropped_2.png’
‘cropped_2-b.png’ -> ‘cropped_3.png’
‘cropped_2.png’ -> ‘cropped_4.png’
‘cropped_3-a.png’ -> ‘cropped_5.png’
‘cropped_3-b.png’ -> ‘cropped_6.png’
‘cropped_3.png’ -> ‘cropped_7.png’
‘cropped_4.png’ -> ‘cropped_8.png’
‘cropped_5.png’ -> ‘cropped_9.png’
‘cropped_6.png’ -> ‘cropped_10.png’
--More--
Testing a simpler mv
command using parameter replacement as the desitnation argument works:
testy="cropped_2-b.png"
mv -iv $testy ${testy/#*_*/cropped_999.png}
This also works:
i=998
testy="cropped_2-b.png"
mv -iv $testy ${testy/#*_*/cropped_$((i++)).png}
‘cropped_2-b.png’ -> ‘cropped_998.png’
testy="cropped_3-a.png"
mv -iv $testy ${testy/#*_*/cropped_$((i++)).png}
‘cropped_3-a.png’ -> ‘cropped_999.png’
I have not been able to determine why it fails during the for loop after the same file error is displayed or where the files actually go.
Aucun commentaire:
Enregistrer un commentaire