Learning Linux – Files and Directories

Why Use the Linux System

In my opinion, the Linux operating system and its command line interface are the most suitable for bioinformatics analysis. There are three main reasons:

  • Long-term stability

  • Most software is only available in Linux versions

  • The powerful Bash commands simplify tedious tasks, especially repetitive work

However, for beginners, getting accustomed to and understanding the Linux operating system requires some time and exploration. The biggest unfamiliarity when transitioning from the visual point-and-click operations of Windows to the command-line interface of Linux is not knowing what to do or where the files are. This article aims to help everyone adapt to the Linux system through several examples.

How to Obtain a Linux System

  • If your organization has a shared server, you can try to apply for an account.

  • Install a dual system or virtual machine on your own computer

  • Purchase a cloud server

  • Experiment with the online learning platform Shiyanlou https://www.shiyanlou.com (which also has many Linux tutorials; just click on any one, double-click the Xfce icon on the desktop to start the Linux terminal)

  • Reply to the administrator on the WeChat public account Bioinformatics Handbook to request access

Getting Started with the Linux System

After logging into the Linux system, you will see an interface like this:

Last login: Mon Jun  5 16:56:56 2017 from 219.241.208.209

Welcome to aliyun Elastic Compute Service!

ct@ehbio:~$

First, let’s explain the letters and symbols that appear:

  • ct: username

  • ehbio: if logging into a remote server, this is the name of the host machine

  • ~: represents the home directory; this will change as we enter new directories

  • $: indicates where a regular user inputs commands; for the root user, it is usually #

  • Visit bashrcgenerator.com to visually customize different display styles.

Where is My Computer?

When you open Windows, the first thing you see is the desktop; as someone who doesn’t like to organize files, my desktop has accumulated so many items that it requires two screens to display everything. Another commonly used feature is ‘My Computer’, where I can open the D drive, navigate through the corresponding folders, and then open files.

The file system organization in Linux is slightly different from Windows. Upon logging in, you are in the home directory, which can be considered analogous to the desktop in Windows. In this directory, we can create files and folders just like we do on the desktop.

If you want to see what contents are in the current directory, type the command ls and press Enter (ls can be understood as an abbreviation for the word list). Since there is nothing in the current directory, there will be no output.

ct@ehbio:~$ ls

If you mistakenly typed is instead of ls, you would see the following prompt command not found. If you receive this prompt after entering a basic Linux command, it is likely that the command was input incorrectly; just take a closer look. After typing a command and pressing Enter, be sure to check the terminal output to determine if there are any issues.

ct@ehbio:~$ is
-bash: is: command not found
# Case sensitive
ct@ehbio:~$ lS
-bash: lS: command not found

Since there is only one file in the current directory, we cannot see the effect. We can create a few files and folders to check.

mkdir is used to create a directory (make a directory); data is the name of the directory. If the directory already exists, a prompt will appear stating, “cannot create directory ‘data’: file already exists”. In this case, you can use the -p option to ignore this error.

ct@ehbio:~$ mkdir data
ct@ehbio:~$ mkdir ls
data
ct@ehbio:~$ mkdir data
mkdir: cannot create directory 'data': file exists
ct@ehbio:~$ mkdir -p data

cat is a command primarily used to view files; here it is used in conjunction with <<END to read large amounts of data. After typing cat <<END and pressing Enter, you will see a greater-than sign appear in the terminal, after which you can input content, press Enter to continue inputting, and finally type END (in uppercase, matching the previous input) to finish the input process. The content you entered will then be displayed on the screen.

ct@ehbio:~$ mkdir data
ct@ehbio:~$ cat &lt;&lt;END
&gt; a
&gt; bc
&gt; END
a
bc

`&gt;` is a redirection symbol, meaning that the output of the command before it is written to the file after `&gt;`. As shown below, a new file in Fasta format has been created.
`ls -l` lists detailed information about files; `-l` indicates a command-line option, which is a reserved option for the program that allows for more flexible operations without changing the program. You can use `man ls` to view all command-line options for `ls`, scroll with the up and down arrows, and press `q` to exit the view. (man: manual, handbook)

```bash
ct@ehbio:~$ cat &lt;&lt;END &gt;data/test.fa
&gt; &gt;SOX2
&gt; ACGTCGGCGGAGGGTGGSCGGGGGGGGAGAGGT
&gt; ACGATGAGGAGTAGGAGAGAGGAGG
&gt; &gt;OCT4
&gt; ACGTAGGATGGAGGAGAGGGAGGGGGGAGGAGAGGAA
&gt; AGAGTAGAGAGA
&gt; &gt;NANOG
&gt; ACGATGCGATGCAGCGTTTTTTTTTGGTTGGATCT
&gt; CAGGTAGGAGCGAGGAGGCAGCGGCGGATGCAGGCA
&gt; ACGGTAGCGAGTC
&gt; &gt;mYC HAHA
&gt; ACGGAGCGAGCTAGTGCAGCGAGGAGCTGAGTCGAGC
&gt; CAGGACAGGAGCTA
&gt; end
&gt; END
## Note the space between the command and the parameters
ct@ehbio:~/data$ ls-l
-bash: ls-l: command not found
ct@ehbio:~$ ls -l
Total usage 4
## d: dir; indicates that data is a directory
drwxrwxr-x 2 ct ct 4096 Jun  8 14:52 data
ct@ehbio:~$ ls -l data
Total usage 4
## The leading `-` indicates that test.fa is a file
-rw-rw-r-- 1 ct ct 284 Jun  8 14:48 test.fa

To view the contents of the written file, use cat filename; note that the file’s directory is the current directory by default. For example, the first command below will prompt cat: test.fa: No such file or directory because the file test.fa does not exist in the current directory. (Note the ‘end’ at the end of the file)

ct@ehbio:~$ cat test.fa
cat: test.fa: No such file or directory
ct@ehbio:~$ cat data/test.fa
&gt;SOX2
ACGTCGGCGGAGGGTGGSCGGGGGGGGAGAGGT
ACGATGAGGAGTAGGAGAGAGGAGG
&gt;OCT4
ACGTAGGATGGAGGAGAGGGAGGGGGGAGGAGAGGAA
AGAGTAGAGAGA
&gt;NANOG
ACGATGCGATGCAGCGTTTTTTTTTGGTTGGATCT
CAGGTAGGAGCGAGGAGGCAGCGGCGGATGCAGGCA
ACGGTAGCGAGTC
&gt;mYC HAHA
ACGGAGCGAGCTAGTGCAGCGAGGAGCTGAGTCGAGC
CAGGACAGGAGCTA
end

The test.fa file is located in the data directory. You can first enter the data directory and then view the file, similar to how you would click on a folder in Windows before opening a file.

cd (change directory) is used to switch directories.

head is used to view the first few lines of a file, defaulting to 10 lines, but you can specify the number of lines to view using -n 6 to see the first 6 lines.

ct@ehbio:~$ cd data
ct@ehbio:~/data$ head -n 6 test.fa
&gt;SOX2
ACGTCGGCGGAGGGTGGSCGGGGGGGGAGAGGT
ACGATGAGGAGTAGGAGAGAGGAGG
&gt;OCT4
ACGTAGGATGGAGGAGAGGGAGGGGGGAGGAGAGGAA
AGAGTAGAGAGA

Additionally, less and more can also be used to view files, especially when the file content is particularly large.

ct@ehbio:~/data$ less test.fa
# q: exit
# Use the up and down arrows, space to scroll

Several commands used earlier have options like ls -l, head -n 6, etc. It is important to note that there must be spaces between the command and the options.

Running man ls in the terminal allows you to view all available options for ls, scroll with the up and down arrows, and press q to exit the view. (man: manual, handbook)

ct@ehbio:~/data$ man ls
NAME
       ls - list directory contents

SYNOPSIS
       ls [OPTION]... [FILE]...

DESCRIPTION
       List  information  about  the  FILEs  (the current directory by default).
       Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

       Mandatory arguments to long options are mandatory for short options too.

       -a, --all
              do not ignore entries starting with .

       -A, --almost-all
              do not list implied . and ..

       --author
              with -l, print the author of each file

       -b, --escape
              print C-style escapes for nongraphic characters
       ....

Two interesting commands are tac: which reverses the file; and rev: which reverses each column.

ct@iZ8vb3e9jtel4m99ss6e7eZ:~/data$ cat &lt;&lt;END | tac
&gt; first
&gt; second
&gt; third
&gt; END
third
second
first
ct@iZ8vb3e9jtel4m99ss6e7eZ:~/data$ cat &lt;&lt;END | rev
&gt; abcde
&gt; xyz
&gt; END
edcba
zyx

Note:

  • Here we assume that all files and folders are placed in the home directory

  • Strictly speaking, the Linux home directory may be similar to C:\Users\ct in Windows

Leave a Comment