154

Hi I'm trying to copy my rails_projects directory from haseebjaved/Desktop/rails_projects to my home directory, which is haseebjaved.

How can I do this via the Command Line?

Also, can I see my home directory on the UI or only via the Command Line in Mac OS X?

Is it possible to copy directories to and from my home directory via the UI? Or only via Command Line?

Thank you

Ian Ringrose
  • 51,220
  • 55
  • 213
  • 317
hjaved
  • 1,585
  • 2
  • 10
  • 10

2 Answers2

336

Is there something special with that directory or are you really just asking how to copy directories?

Copy recursively via CLI:

cp -R <sourcedir> <destdir>

If you're only seeing the files under the sourcedir being copied (instead of sourcedir as well), that's happening because you kept the trailing slash for sourcedir:

cp -R <sourcedir>/ <destdir>

The above only copies the files and their directories inside of sourcedir. Typically, you want to include the directory you're copying, so drop the trailing slash:

cp -R <sourcedir> <destdir>
George Stocker
  • 57,289
  • 29
  • 176
  • 237
Andy Friese
  • 6,349
  • 3
  • 20
  • 17
  • Thanks Peter. I'm trying to copy three sub-directories within rails_projects to my home directory haseebjaved. When I issue the following command: cp -r ~/Desktop/rails_projects haseebjaved , the command effectively copies the three subdirectories from rails_projects to haseebjaved and makes a new folder named haseebjaved in my home directory haseebjaved. What I want is the directory rails_projects to show up under my home directory haseebjaved just like other folders such as Downloads, Desktop, etc. – hjaved Mar 21 '12 at 00:45
  • I'm using the search function with Finder now and it's much better but I don't see my home directory there in Favorites or anywhere else. Very new to Mac OS X and Rails. – hjaved Mar 21 '12 at 00:46
  • `cp -r ~/Desktop/rails_projects ~` is what you want – Andy Friese Mar 21 '12 at 00:49
  • At least you should have Macintosh HD in your favorites. Start there and navigate to /Users. There should be your homedir inside. Drag&Drop it to your favorites. – Andy Friese Mar 21 '12 at 00:52
  • Thanks Peter. There was no Mac HD in favorites but I found the home dir and made it a favorite. Any other such tips for a newbie? – hjaved Mar 21 '12 at 03:38
  • Well... you know already how to copy directories and how to see them. Is there any more one could do with a Mac? ;-) Seriously, glad that I could help you. – Andy Friese Mar 21 '12 at 06:21
  • 5
    Here is the manual from `cp`: ```-R If source_file designates a directory, cp copies the directory and the entire subtree connected at that point. **If the source_file ends in a /, the contents of the directory are copied rather than the directory itself.** This option also causes symbolic links to be copied, rather than indirected through, and for cp to create special files rather than copying them as normal files. Created directories have the same mode as the corresponding source directory, unmodified by the process' umask.``` – Xiao Oct 01 '14 at 05:40
  • 2
    Just two cents about copy folders from command line: ditto command http://ss64.com/osx/ditto.html PS. Unlike cp -R, if the destination folder already exists, the existing contents will be merged with the contents of the folder being copied. – Alexander Hramov Feb 06 '15 at 07:00
5

tl;dr

cp -R "/src/project 1/App" "/src/project 2"

Explanation:

Using quotes will cater for spaces in the directory names

cp -R "/src/project 1/App" "/src/project 2"

If the App directory is specified in the destination directory:

cp -R "/src/project 1/App" "/src/project 2/App"

and "/src/project 2/App" already exists the result will be "/src/project 2/App/App"

Best not to specify the directory copied in the destination so that the command can be repeated over and over with the expected result.

Inside a bash script:

cp -R "${1}/App" "${2}"
Gary Davies
  • 920
  • 15
  • 12