0

In my current directory, I have multiple subdirectories that contain files. I'm trying to move those files from the subdirectories to my current directory. To this end, I've created this inline bash program:

for i in */ ; do for j in $f* ; do mv "$i$j" $PWD ; done ; done

When I run this program, however, I'm prompted by the correct use format:

usage: mv [-f | -i | -n] [-v] source target
       mv [-f | -i | -n] [-v] source ... directory

I'd appreciate any clarification as to why this is happening.

Thanks!

  • 2
    where is `$f` coming from? You should quote `$PWD`. – pLumo Apr 19 '21 at 19:33
  • 1
    Change your script so that instead of ... `do mv "$i$j" $PWD ;' it says 'do echo mv "$i$j" $PWD ;'` This may enlighten you as to why the script is doing what it is doing. Also, see [this question](https://unix.stackexchange.com/questions/533005/moving-a-file-based-on-specific-filename-to-specific-folder-based-on-filename/533019#533019) to read why this is an important technique to avoid performing a slew of `mv` commands that are *WRONG*. – Jim L. Apr 19 '21 at 19:45
  • 1
    You forgot to show the file names that `i` and `j` have and the value of `f` and `PWD`. There are two plausible explanations, both covered by our [reference question](https://unix.stackexchange.com/q/131766): `PWD` contains special characters such as whitespace, or a value of `i` starts with a dash. If you can't figure out the problem after reading our reference question, edit your question to show all the file names involved and a full transcript of running your script under `bash -x`, then flag your question and ask for it to be reopened. – Gilles 'SO- stop being evil' Apr 19 '21 at 20:29
  • @JimL. Thanks for suggesting I use echo - it was really helpful in figuring out what was happening. – Kiol Fe Apr 19 '21 at 21:57
  • Happy to help. Then once you have a script that `echo`s a series **100% correct** `mv` commands (and *nothing else*), all you have to do is pipe the output of your script to a shell like `sh` or `bash`, and the commands will be executed. So you have a built-in "dry-run" feature where you can **see** what the script is going to do, before it actually does it. – Jim L. Apr 19 '21 at 22:13

0 Answers0