3. File manipulation

Slides

Exercise: Reading text files

Using the commands you just learned, explore the .gff3 file in the Home directory.

  1. Is the file a text file?
Solution Yes. You can use less, cat, head, or tail and get human-readable info. Note that this doesn’t have anything to do with its file extension.


  1. How many lines does the file have?
Solution
$ wc -l GCF_009858895.2_ASM985889v3_genomic.gff 
67 GCF_009858895.2_ASM985889v3_genomic.gff

There are 67 lines in this file.


  1. Can you read the content of the file using less?
Solution
$ less GCF_009858895.2_ASM985889v3_genomic.gff 

Exercise: Editing text files

Using the commands you just learned, create a file called helloworld.txt and edit it using nano.

  1. Write “Hello world” into the file. Save the file and exit nano.
  2. Create a subdirectory called “test”; move the helloworld.txt file into test.
  3. Create a copy of the helloworld.txt file called helloworld2.txt
Solution
  1. First, use the nano command to open a file called helloworld.txt
$ nano helloworld.txt

Inside the nano editor, write “Hello world”, then use the ^O option to write the changes and ^X to exit.

  1. Create a subdirectory called “test”; move the helloworld.txt file into test.

Use the command mkdir to create this new directory. Then, use mv to move helloworld.txt into this directory.

$ mkdir test
$ mv helloworld.txt test/
  1. Create a copy of the helloworld.txt file called helloworld2.txt.

Change the working directory using cd, then use the cp command to create the copy.

$ cd test
$ cp helloworld.txt helloworld2.txt

Reference material

Here are two cheat-sheets that can be useful to have as a reference for common UNIX/Linux commands: