DEV Community

Matt Crowder
Matt Crowder

Posted on

I've been using cat and vim for the wrong reasons for so long...

My situation: I have a file that I need to copy the entire contents of it.

In the terminal, I type: cat file.txt, then copy the terminal output.

The easy solution:

pbcopy < file.txt

And now the contents are saved in my clipboard.

Then, if I had to paste those contents somewhere else, I would use vim to create a file and paste those contents in there, and if I was lucky, I wouldn't have to restart my computer from getting stuck in vim.

Instead, the easy solution:

pbpaste > file-two.txt

And now, all of the contents from my clipboard are saved in file-two.txt!

What other bash commands have you found useful? Let me know in the comments!

Top comments (7)

acido_binario profile image
šŸ sadder sCrypt Kiddie š–¤šŸ•·ļø

For copying files:

cp fileToCopy.txt copyOfFile.txt
audioboxer217 profile image
Scott Eppler

I use pbcopy all the time but was unaware of pbpaste (I've used the vim then paste method). So thanks for that!

For those offering suggestions on how to do a direct copy of one file to a new file. These are all great suggestions as well, but sometimes I'm copy to or pasting from something else.

notaduck profile image
notaduck • Edited

An easy way to make a paste from the terminal instead of using paste in is to do;

cat <input> | nc 9999
cookrdan profile image

There are a few commands that copy files. So instead of doing a copy/paste routine you can do like this:

ditto file1.txt file2.txt

Also check out cp

flrnd profile image
Florian Rand

Also, in case you are working in a remote machine

man scp

But I'm trying to figure out why do you need to copy it into the clipboard in the first place.

a simple cat file1.txt > file2.txt would do the job (or the examples already in comments), unless you are in a remote session (that's where scp comes handy :).

edgarortegaramirez profile image
Edgar Ortega • Edited

you could also do

$ cat file1 > file2