Section 4: Making, Moving, Copying, Editing, and Removing Files! - Green-Biome-Institute/AWS GitHub Wiki
Back to Section 3: Behind the Scenes
Let’s move onto some more fun objectives. In this section we will be learning some practical command line skills. These will include making, moving, editing, copying, and removing files and directories.
Before we continue, it’s important to note that you all have been paired up in twos or threes. Each pair or group has been assigned the same EC2 instance, so all the work you’ve been doing so far has actually been on the same computer system as the other people in your group! That is a powerful aspect of working in the cloud, you are able to log in multiple users from multiple different locations and work in the same environment.
Learning Points for Section 4: Making, Moving, Copying, Editing, and Removing Files!
mkdir
is used to make directories (you can also use “rmdir” to remove empty directories!)touch
creates empty files with whatever name you give themmv
both moves files to other locations and can rename themnano
opens up the nano editor, where you can make changes to filescat
is used to read and combine filescp
copies files (you can copy files in your working directory or make a copy in a different directory!)rm
deletes files and directories
Organizing our workspaces with directories
Since we’re all working on the same computer system as other people, let’s make our own designated workspace before we continue. To do this we will use the make directory command, mkdir
, to make directories using our names. If you want to make a file or directory that has a space in it, use a dash -
or underscore _
, because file and directory names cannot have spaces! So my directory will be flints-dir
:
$ mkdir [your-name]-dir
Ex for me: $ mkdir flints-dir
Now, listing by using ls
:
$ ls
We can see that our directory was created. You may also see the folder with the name of the other person who is working on this computer at the same time. That will be important to note later on. Next, let’s move into our directory and list again:
$ cd [your-name]-dir
Ex for me: $ cd flints-dir
then
$ ls
You can see that it is currently empty in this newly generated directory with your name. Let’s change that.
Let’s add a subdirectory called share-dir
. Now that we have created our own individual directory as a workspace, we don’t have to worry about using our names to identify our files. This is just like how you can’t have two files on your computer desktop named my-text.txt
, they have to be named something different. However if you have two folders on your desktop: folder-1
and folder-2
, they could both have a text file within them with the same name. To do this, use the mkdir
command again:
$ mkdir share-dir
Then to see that the folder was generated use the list command again:
$ ls
Creating files
Now that we have created a directory to organize things in, let’s create a file. To do this, we can use the touch
command:
$ touch myfile.txt
then
$ ls
Moving files around
Our high-level directory with our name in it now consists of a directory called share-dir
and a text file named myfile.txt
. In order to put this text file into the share-dir directory, we can use the move command, mv
. This command, mv
, requires two arguments. It looks like this:
$ mv [the-file-you-want-to-move] [the-location-you-want-to-move-it-to]
The reason I am using dashes instead of spaces is to remind you that file and directory names can’t have spaces in them. Now to move the myfile.txt
file into the directory share-dir
, we will use the following command:
$ mv myfile.txt share-dir
Then to see that the file was moved, use the list command again:
$ ls
When you list the contents now, the file myfile.txt
is gone! It has moved into the folder share-dir
. Let’s confirm that by going into the share-dir
directory and listing the contents:
$ cd share-dir
Then
$ ls
There’s our file again!
Changing file and directory names
But myfile.txt
isn’t really a great name, is it? Let’s change it. To do this, we actually also use the mv
command. You can think of this as moving the contents of the file myfile.txt
into another file, with a name that you specify. For this purpose the two arguments for mv
look like:
$ mv [the-file-with-the-name-you-want-to-change] [the-name-you-want-that-file-to-have]
Let’s try that out by changing the myfile.txt.
to have our names in it again:
$ mkdir myfile.txt [your-name]-file.txt
Ex for me: $ mv myfile.txt flints-file.txt
Then
$ ls
Great, we renamed our file! It’s important to have specific names for data, files, and directories. This will help you dramatically in the future when you are looking back through your experiments. For example, having a data file named data001.fastq
is substantially less helpful than one named data-090221-lambdaphage-assembly-kmer75.fastq
, where we see the date of the data and information relevant to it, even if the second is a bit longer!
nano
editor
Adding text and data to files with the It’s great that we have a better-named file, but it doesn’t have anything in it! Let’s edit this file. To do this, there are two main commands we can use:
nano
vim
We are going to begin by learning aboutnano
, because it is easier to use and get started with. However,vim
is more powerful. There will be a tutorial on usingvim
in the GitHub, however we will not be using it right now. If you would like a better method for editing files, you can check that out.
To edit our file with nano
we will use the command like this:
$ nano [your-name]-file.txt
Ex for me: $ nano flints-file.txt
You will see that the program nano
has taken over your CLI! There is an empty black screen with some commands at the bottom. You can see one of these commands is ^G Get Help
. Let’s try that first by pressing (control + G)
on our keyboards.
After pressing (control + G)
, your screen will be filled with lots of text. This is the help screen for the nano editor (the page you are currently on). Lets just look at the first couple options. First is ^G
, which we have already used. Next is ^X
, which is used to either exit the current help screen, or to exit the nano editor. After that is ^O
, which is used for saving the file (to “write the current file to disk” means to write the text you have entered into the location that the computer is storing this file). Let's try those.
First, exit the help screen:
Press (control + G)
Now, we are back to the black screen. Let’s enter some simple text:
Type Hello!
into the editor.
Now that we've put some simple text into the file, let’s exit the nano editor.
Press (control + X)
Ah, but we forgot to save! This is a nice feature of nano editor, that it won’t let you leave the file without choosing to save or not save if you have edited it! For now, let’s cancel exiting the file by pressing (control + C)
. We are back in the nano editor. Let’s add some more text.
Press enter
twice to create two new lines.
Then type the name of your favorite plant or a plant you’ve worked with! (Mines Sequoia Sempervirens). For example, my text file now looks like:
Hello!
Sequoia Sempervirens
Let’s save this by pressing (control + O)
and then when it asks you File Name to Write: [your-name]-file.txt
, press enter
. Your changes are saved. Now we can exit using (control + X)
Reading files from the command line
To read a file without entering it using the nano command, we can use a command that was used in the beginning example, cat
, followed by the filename:
$ cat [your-name]-file.txt
Ex for me: $ cat flints-file.txt
The CLI will output the text that is inside of your file! This command can also be used to combine several files when listed one after the other, like this:
$ cat file1.txt file2.txt file3.txt
which will print it to your screen. You can also save the combined file into a new txt file like this:
$ cat file1.txt file2.txt file3.txt > file_combined.txt
Copying and sharing files
Now, let’s try sharing a copy of this file. In order to do this, we first have to copy it! The copy command is cp
. Similar to the mv
command, the cp
command takes in two arguments like this:
$ mv [the-file-you-want-to-copy] [the-name-you-want-the-copy-to-have]
Let’s just add the word “share” into the name so that the copy will read “[your-name]-file-share.txt”:
$ cp [your-name]-file.txt [your-name]-file-share.txt
Ex for me: $ cp flints-file-share.txt
Then
$ ls
Now when we list, there are 2 files in our directory share-dir
. Let’s read the second one with the cat command:
$ cat [your-name]-file-share.txt
Ex for me: $ cat flints-file-share.txt
You can see that this is an exact copy of the first file with the same text inside of it!
Okay, now we want to share this file into the other person’s folder who is using this EC2 instance! To do this, we could use both the mv
or cp
commands. If we used the cp
command, it would create a new copy in the other person’s directory. If we use mv
, it will simply move the desired file into the other person’s directory. Since we already copied the file, let’s use the mv
command.
Before we can do this, we will need the path to the other person's folder from our current working directory. So let’s back out of our current directory and our higher level directory using the cd ..
command. In order to go back several directories at once, we can stack the ..
symbols with a /
between them. So to back up twice we will do:
$ cd ../..
Remember, to get back to the home directory, we can also use cd
by itself!:
$ cd
Now that we are here, we can list the contents of our home directory:
$ ls
And we see that the other person using this EC2 instance has a folder with their name in it. Let’s enter that folder using cd
:
$ cd [their-name]-dir
Now that we are in their folder, let’s go into their share folder:
$ cd share-dir
We want to find the path to this location. Use the pwd
command:
$ pwd
This will return the following:
home/ubuntu/[their-name]-dir/share-dir
Ex for me: home/ubuntu/flints-dir/share-dir
Now that we have the path to their directory, let’s go back to our own, where we’ve stored the text file to share:
$ cd ../..
Then to go straight into our share-dir
directory:
$ cd [your-name]-dir/share-dir/
Ex for me: $ cd flints-dir/share-dir/
Okay, let's share the file with them using the mv
command!:
$ mv [your-name]-file-share.txt home/ubuntu/[their-name]-dir/share-dir
Ex for sharing with me me: $ mv [your-name]-file-share.txt home/ubuntu/flints-dir/share-dir/
Ex for me sharing with them: $ mv flints-file-share.txt home/ubuntu/[their-name]-dir/share-dir/
Now, let’s see what happened! Use the list command:
$ ls
There should be someone else’s text file in your share-dir
directory. Let’s find out what their favorite plant is by reading is using cat
:
$ cat [their-name]-file-share.txt
Ex for me: $ cat flints-file-share.txt
Great job!
Deleting files
Our very last command for this module will be to remove files. For whatever reason, maybe you really don’t like that person’s favorite plant. You don’t want it in your share directory! Let’s get rid of it using the remove command, rm
. The remove command can remove both files and directories and has a variety of different options. Sometimes you want to delete large directories with lots of files in them! For now, we just want to delete one file, so we use the syntax rm [filename]
. In this case that will look like:
$ rm [their-name]-file-share.txt
Ex for deleting my file: $ rm flints-file-share.txt
Then list again:
$ ls
And it’s gone! You now only have your own file in your share-dir
directory.
We’ve gone through a lot of material, so this is where this training module will end! Let’s recap the material from in this section:
Review Questions:
What command do you use to create a new directory?
- You can use the
mkdir [directory-name]
command to build new directories.
What command do you use to create empty text or data files?
- You can use the
touch [filename]
command to create empty text or data files
How do you move files to different locations?
- You use the
mv
command to move files or directories to different locations.
How can you get the path of a location that you want to move a file into?
- You can either use
/..
to signify that you want to move the file into the parent directory, or you can navigate into the directory you want to move the file into and usepwd
to find its path. Then you put a ~ at the beginning of the path because that will tell the computer that it should look for that path within the home directory.
How do you edit files?
- To edit files you can open them in the Nano editor by using the command
nano [filename]
.
How do you read files into the CLI window?
- You use the command
cat [filename]
to read a file.
How do you copy files?
- You use the command
cp [filename1] [filename2]
copyfilename1
into a new file with the namefilename2
.
How do you delete files?
- You use the command
rm [filename]
to delete files