Check out a free preview of the full Introduction to Bash, VIM & Regex course:
The "Combining and Copying Files" Lesson is part of the full, Introduction to Bash, VIM & Regex course featured in this preview video. Here's what you'd learn in this lesson:

James introduces to commands for files: The cat command, which concatenates many files into one file, and cp command, which can copy a file to another directory or file name.

Get Unlimited Access Now

Transcript from the "Combining and Copying Files" Lesson

[00:00:00]
>> James Halliday: The cat command was originally intended to concatenate files, which you can still use it for that purpose. So, all of the files that you specify as arguments to the cat commands will be concatenated. So, if I make two files really quick.
>> James Halliday: I'll make one called beep, and one called boop.

[00:00:23] Now, if I type cat beep.txt, boop.txt, I see the contents of both of those files written to standard output. You can put as many, or as few files as you want. If you don't specify any commands, then it reads from standard input, so you have to type stuff.

[00:00:45] If you ever get stuck in a program that you don't know how to quit from, like this, because it's reading from the standard input on the keyboard. You can usually do CTRL+D, which ends gracefully, or CTRL+C if it's like, get me out of this thing, I want this program to exit.

[00:01:06]
>> James Halliday: But another useful thing you can use the cat command for is if you just wanna display the contents of a file. You can do cat, just the name of that file, and you can see what's in it. So this is really useful if you're moving around in your directory structure CD and LS, that kind of stuff.

[00:01:24] You can see what's in your files. If you accidentally do something like cat a JPEG file, so I can show you. What that looks like, or a PNG file, you'll get something like this, not very helpful. Also, your terminal might get a little bit stuck. If that happens, type reset, even though you might not be able to see the characters that you're typing anymore, because some odd characters can get printed to your screen.

[00:01:53] If you type reset and then press Enter, it'll usually fix that.
>> James Halliday: That's the cat command.
>> James Halliday: Another useful command to try out is the copy command which is cp. So how it works is, for example, you can pass in a source file, is the first argument to the copy command.

[00:02:22] So I'm gonna copy beep.txt to a file called message.txt. What this is gonna do, is it's gonna copy the contents of beep.txt and it will create a new file called message.txt. And now if I cat message.txt, it has the contents of the first file.
>> James Halliday: You can also copy into directories if you want.

[00:02:48] So all of the rules about paths apply, so you can use things like ../ if I wanna copy for example this file to the parent directory.
>> James Halliday: Because I don't specify a file name, what the copy command is gonna do is it's gonna copy beep.txt into the parent directory into a file called beep.txt.

[00:03:14] So it's the same as doing this. These are the same, both of these commands.
>> James Halliday: Now, if I go up a directory, I see beep.txt.
>> James Halliday: Oops.
>> James Halliday: So you can also use the copy commands to copy multiple files into a directory. So if I have a directory like, I'll call it cool, oops didn't mean to do that.

[00:04:02]
>> James Halliday: So here I have a directory called cool, and if I wanna copy both of these text files, so I can copy beep.txt and boop.txt into the directory called cool. And I don't have to include the slash at the end. What the copy command is gonna do is it's gonna look at the last argument in this argument list of beep.txt, boop.txt and cool.

[00:04:28] If the last thing is a directory, then it will copy all of the preceding files into that place. So now, if I go into the cool directory, I see two files, beep.txt and boop.txt. And I can cat them both again. All right, so that's the copy command, and that's cat.

[00:04:50] And there's some other things, I think. Great, so we're here. Another really useful thing, so this is the first example of flag. A flag is a special kind of argument that starts with a dash, or sometimes they start with two dashes. It's also called the switch sometimes. So flag and switch are sort of interchangeable terms for this kind of thing.

[00:05:19] It's sort of a way to customize the behavior of a command. So in this case, the copy commands takes a flag called -r, which means recursively copy. So what you can do is you can recursively copy an entire directory structure to someplace else with this command. So if I go up a couple of directories, then I can recursively copy this directory, day1 into a place called the called great.

[00:05:56] And what it's going to do is it's going to go into day1, it's gonna see all the files, it's gonna copy them into this second directory. It's also gonna see every directory in the day1 directory, and it's gonna recursively copy those as well. So if I go into the great directory now, it has all of the files and also all of the directories.

[00:06:15] Oops, so it's a very handy switch, or flag. If you wanna read more about these flags and switches to every command, you can use the man command which is short for manual. So if you do man space cp, you can list out all of the different switches that any command that you wanna learn about ticks.

[00:06:39] So if I scroll down, i can see the -r. So there's also more than one way to run cp -r. You can use a lower case r or a capital R, or you can pass in --recursive, which is a longer way of specifying what you wanna do. Sometimes it's nice to use the longer form, cuz if you have a shell scripts and it's kind of an obscure one, then someone reading your shell script might have a picture of what that flag does.