What is the command to copy a file from a directory to another directory in macOS?

The command that I'm trying to execute for copying a file to a directory is given below :

cp /Users/anish/.m2/repository/org/apache/commons/commons-lang3/3.8.1/commons-lang3-3.8.1 /Volumes/Local Disk/Software Development/Courses/Maven/maven-learn-workspace/hello/target

After executing this, I'm getting this message :

usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file target_file
       cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file ... target_directory

Can anybody tell what needs to be added so that this command executes ?


Solution 1:

You need to protect any whitespace in the names with either "" or \

cp foo 'path/with spaces/to/target'
cp foo path/with\ spaces/to/target

Additionally

  • Using Tab Completion in bash and other shells will automatically insert \ where necessary
  • Drag&drop of a file/directory from Finder into a Terminal window will insert the full path of said file/directory, with appropriate escapes
  • ditto is an alternative to cp which is better at handling metadata, resource forks and other specialities of macOS-specific filesystems.
  • If you want/need to use environment variables as part of a path (path/to/$TARGET/directory), use "" instead of '' but look out for $ as part of directory/file names