0

By mistake, I have copied 100 files using the cp command as

$ cp file1 "/data/ file1"
$ cp file2 "/data/ file2"

note:- there is a space between /data and file2, file1

Within the /data directory, I can see the files but not able to copy or move. File not found the error is while accessing/copy/move those files.

$ cd /data/ ; ls
file1 
file2
$ cp file1 /data1/
can not stat file1 

How to resolve the issue.

muru
  • 72,889

3 Answers3

0

The space is now part of the file name, you need to include it in the filename when using cp or mv:

mv " file1" /data1/

Or better this, to get rid of that space:

mv " file1" /data1/file1

Do it for all files in one go using Perl rename tool: (Take care, there are several different rename tools)

prename -n 's#^ #/data1/#' " file"*

(Remove the -n if your happy with the result)

pLumo
  • 22,565
  • Can we write a script to do the same for 100 files . while read -r file; do mv " $file" "$file" done <filename.txt – user1726453 Nov 06 '19 at 13:59
  • with files, a for loop is usually the better option, but sure you can do it ... See @bxm's answer for an idea on how to do it. – pLumo Nov 06 '19 at 14:02
0

You could use a globbing selection to match the offending files such as:

 for f in ./*file?
 do
   echo "[$f]"
 done
Dfaure
  • 243
0

The space is the problem, you'd need to escape it in references to the files. To fix all the files in one go (subject to your shell -- works in Bash at least):

for FILE in /data1/" "* ; do mv "${FILE}" "${FILE# }" ; done
bxm
  • 4,855