0

I want to right click, copy. Change directory, right click paste. But using the terminal.

Thing is pbcopy < directory_name_here fails with "oh no, this is a directory. I can't possibly do such a complex thing!"

I don't want to know every path of everything so I refuse to do anything like:

cp ~/path/to/source ~/path/to/destination

I'm sure other people are fine with that nonsense, no thank you.

So I did what any reasonable person would do and just created a directory ~/.pbcopy and I'm 100% content to cd to destination then cp -R directory_name_here ~/.pbcopy then cd to destination and cp ~/.pbcopy/ upsie

And here I have a problem. Two actually:

  1. cp did not copy the directory, but only it's children. 100% not what I want.
  2. My improvised paste wants a directory name, I think. Would cp source . work? Had the "clipboard" contained the parent folder?

Okay, so my thing doesn't work. What would work? I'll ask again. I don't want to remember and copy paste paths, I want to copy directory and contents to clipboard and paste entire contents once I've changed to the appropriate directory. How do this?

8
  • 3
    IMHO it's reasonable to use terminal based file manager like vifm, midnight commander, ranger etc Mar 31, 2023 at 14:02
  • 4
    Can I ask why you're using Terminal? Are you creating a script? If you want GIU functionality, why not just use the Finder?
    – benwiggy
    Mar 31, 2023 at 14:04
  • The 'clipboard' of which you speak for file/folder transfers is not the same 'clipboard' mechanism one encounters with text and image copy/paste in other places in macOS. You might want to experiment with the Terminal command mv, which is explained at this Apple Support page.
    – IconDaemon
    Mar 31, 2023 at 14:18
  • 1
    You might want to investigate the popd, pushd and dirs commands to help facilitate remembering all your working directories
    – Allan
    Mar 31, 2023 at 14:21
  • 1
    According to the cp man page, if you do cp -R directory_name target, then it should copy the whole directory. If you do cp -R directory_name/ target with a trailing slash, it will only copy the contents, not the directory itself. Mar 31, 2023 at 16:13

1 Answer 1

7

I don't want to know every path of everything so I refuse to do anything like...

Most of us don't remember long and complex paths either, so this is very relatable. For example, there is an excellent command tool called "Airport Utility" (airport) but it is not found in the PATH. Using find or locate will get you an ungodly path of

/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport

This is impossible to remember, but it’s just one example (I use an alias for this command, by the way). There is no expectation that anyone will remember every directory path. However, we have a number of tools which we can use to make life just a bit simpler. Following is just a sampling of the tools that I use.

Drag and Drop from Finder to Terminal

If you already have the file/folder pulled up, you don't have to remember the entire path. Simply start your command as approprite (copying a folder in this example) cp -R then drag the folder from Finder to Terminal and the path will be automatically populated.

Drag_Drop path from Terminal Animation

Then continue the command as needed. Here I'm copying to my current directory so I use a period or dot . but I could drag another file/folder from Finder as the destination.

Using Shell Builtins

We have several from which to use:

  • Tab Completion. This is availabe in both Bash (default for Mojave and earlier) and Zsh shells (default for Catalina and later). when you are entering a file or folder, simply start with the first few characters, press ⇨Tab and it will automatically complete the rest of the line for you. For example:

     cp - R /Users/al<⇨Tab>
    

    Will automatically fill in the rest of the folder until it needs more input from you to continue:

     cp -R /Users/allan/Desk<⇨Tab>  ← and so on and so on...
    

You can use this for both source and destination. Just tab complete your way through each directory

  • pwd → Outputs the present (current) working directory. It's also available as a variable, $PWD. For example, assuming you're deep into some obscure folder, and want to copy something like a single file or folder to a folder on your Desktop, just issue the command

    cp -R $PWD/folder_to_copy ~/Desktop/destination_folder
    
  • Tilde ~ Expansion → This is the shortcut to your home folder which is also conveniently referenced by the shell variable $HOME. If you wanted to copy all of your Documents to a USB drive, you could simply issue the Terminal command:

    cp -R $HOME/Documents /Volumes/My_Backup_USB/Documents
    

    or equally valid

    cp -R ~/Documents /Volumes/My_Backup_USB/Documents
    

  • pushd, popd, and dirs → Allow you to push directories onto, pop off of and list the contents of the directory stack that is LIFO or Last In/Fist Out. (Think of a stack like a stack of pancakes; you always put the newest on top and take the top one off when eating one).

    • dirs will list all of the directories on your stack (dirs -p shows one entry per line). The first value is always the current working directory. This will remain so, until you "push" some entries onto the stack. Here's an example of a directory stack:

       dirs -lp
      
       /Users/allan/Desktop/Email to Process
       /Users/allan/Desktop/Saved Images to Process
       /Users/allan/Desktop/eBooks to Process
       /Users/allan/Desktop
       /Users/allan
       /Users/allan/Desktop
      

      (Desktop is on there twice because I accidentally pushed it twice, but illustrates this function perfectly)

    • pushd will "push" the present directory or a directory you specify onto the stack. This is how I populated the stack for the previous bullet point. Examples:

       `pushd` 
       `pushd ~/Desktop/My_Folder`
      
    • popd will "pop off" the latest directory entry or the one you specify (can be determined with the dirs command) For example, we can use some command expansion to get the 3rd directory in our stack with the command popd +3 (3rd item from the top) or popd -3 (for 3rd item from the bottom)

      So, once you know where your directories are in the stack, you can use those with command expansion:

       cp -R `popd` ~/Desktop     ← Will copy whatever folder from top of stack to present working directory
      
       cp -R `popd` `popd`        ← Will copy the first folder on the stack to second folder on the stack
      
       cp -R `popd +2` `popd -4`  ← Will copy the 2nd from top to 4th from bottom
      

    Please keep in mind that this is not a comprehensive tutorial on pushd, popd, and dirs. There are many options (flags) that you can use with each to better work with the directory stack.

1
  • Yes, pushd and popd sound exactly like what I need. I'll head over to the man pages, thank you.
    – Kalec
    Apr 3, 2023 at 10:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .