I have a test file which contains a space-separated files that I want to move/delete.
Example line:
'/data/owncloud/A0934095JG 1231/files/Photos/Paris 2016 08 28.jpg'
I want to move the files away to another directory in a for loop:
for i in $(cat marktodelete);do mv $i /data/willbedeleted/;done
But it gives me an error.
mv: cannot stat ‘'/data/owncloud/A0934095JG 1231/files/Photos/Paris 2016 08 28.jpg'’: No such file or directory
Why?
2nd problem after this how could I move actually this file to the new place with included the original directory somehow in the name, like the above file would like like this in the new place:
/data/willbedeleted/Paris 2016 08 28.jpg-/data/owncloud/A0934095JG 1231/files/Photos/
or because of the slash can be like this also, no problem
/data/willbedeleted/Paris 2016 08 28.jpg-data-owncloud-A0934095JG-1231-files-Photos
Advertisement
Answer
Answering the 1st question, there are two problems related to the whitespaces
in the pathname. One is in the for .. in ..
loop and the other is in the mv
command. The word-spltting is caused unless you double-quote the filenames. On the other hand, if you enclose the filenames with quotes in the resource file marktodelete
, the quoting character is interpreted as a part of the filename.
Assuming one line in the file contains one filename, please try instead:
while IFS= read -r i; do i=${i#'} # remove leading single quote i=${i%'} # remove trailing single quote mv -- "$i" /data/willbedeleted/ done < marktodelete
As of the 2nd question, you can change the destination filenames with:
while IFS= read -r i; do i=${i#'} # remove leading single quote i=${i%'} # remove trailing single quote basename="${i##*/}" # extracts filename orgdir="${i%/*}" # extracts directory name newname="${basename}-${orgdir//[/ ]/-}" mv -- "$i" "/data/willbedeleted/$newname" done < marktodelete
Hope this helps.