bash error renaming files with spaces - mv target is not a directory

后端 未结 2 683
说谎
说谎 2021-02-12 15:48

I\'m trying to rename a bunch of files which contain spaces in them, getting rid of the spaces. I thought I found the correct bash command:

for f in *.txt; do m         


        
相关标签:
2条回答
  • 2021-02-12 16:03

    Because this is the first thing that came up on google when googling this error, I thought I'd add a bit.

    This error occurs if you have more than two arguments and the last result is not a directory.

    This works:

    mv file1.txt output.txt
    

    This does not:

    mv file1.txt file2.txt
    

    In my case I was doing: mv prefix_* output_file_name to ensure a downloaded file had a consistant name, but another file had appeared in the directory, restulting in the "mv target is not a directory" error

    0 讨论(0)
  • 2021-02-12 16:18

    Try:

    for f in *.txt; do mv "$f" "${f// /}"; done
    

    Three points:

    1. The quotes around a shell variable should not be escaped.

    2. In general, it is a good idea to put double-quotes around every reference to a shell variable.

    3. ${f/ /} removes just the first occurrence of a space. To remove all spaces, use ${f// /}.

    What went wrong

    $ touch {a,b}" .txt"
    $ ls *.txt
    a .txt  b .txt
    $ for f in *.txt; do mv \"$f\" ${f/ /}; done
    mv: target `a.txt' is not a directory
    mv: target `b.txt' is not a directory
    

    The expression \"$f\" does not behave like it is double quoted. It expands to two arguments, such as "a and .txt", where the double-quotes are treated as normal characters, just like the a is a normal character. Because there are three arguments to mv ("a and .txt" and a.txt), mv believes that you are trying to move the first two arguments to the third and the third is required to be a directory. Since the third is not a directory, it issues an error message.

    0 讨论(0)
提交回复
热议问题