Linux for Bioinformatics

Lesson 5 of 14 · 11 min

Permissions and Making Scripts Executable

Every file in Linux carries a set of permissions that decide who may read it, change it, or run it. A script is just an ordinary text file, so before Linux will run it you must switch on its execute permission. Save the two lines below in a file named count_seqs.sh using a text editor such as nano.

bash
#!/bin/bash
grep -c "^>" reads.fasta

The first line is the shebang, written #!, and it tells Linux to run this file with the bash program. FASTA is a plain-text sequence format in which every sequence starts with a header line beginning with the > character, so counting those headers counts the sequences. On the second line grep searches text, its -c flag counts matching lines instead of printing them, and the pattern ^> means a > at the very start of a line. The script assumes a FASTA file named reads.fasta sits in the same folder; the 128 you see when it runs later is the header count for one such example file.

Reading ls -l

bash
ls -l count_seqs.sh
-rw-r--r-- 1 student student 37 Jul 13 09:20 count_seqs.sh

The first column, -rw-r--r--, is the permission string, ten characters long. The first character is the file type, where a dash means a regular file and d means a directory; the next nine are three groups of r (read), w (write), and x (execute) belonging to the user who owns the file, the group, and all other users, with a dash meaning that permission is switched off. This file lets the user read and write and lets everyone read, but no one has an x, so Linux will not run it even though it holds a program.

Granting execute with chmod

The chmod command, short for change mode, edits a file's permissions. In symbolic form you name who and what to change, so chmod u+x count_seqs.sh means for the user (u), add (+) execute (x). In numeric form each permission is a number that you add up per group, where read is 4, write is 2, and execute is 1, giving 644 (rw-r--r--) for ordinary data files and 755 (rwxr-xr-x) for scripts everyone should be able to run.

bash
chmod u+x count_seqs.sh     # symbolic: add execute (x) for the user (u)
ls -l count_seqs.sh
chmod 755 count_seqs.sh     # numeric: rwx for user, r-x for group and others
ls -l count_seqs.sh
./count_seqs.sh             # ./ runs the script in the current folder
-rwxr--r-- 1 student student 37 Jul 13 09:20 count_seqs.sh
-rwxr-xr-x 1 student student 37 Jul 13 09:20 count_seqs.sh
128

Try it yourself: Create a file named greet.sh with two lines, #!/bin/bash on the first and echo "Hello, sequences!" on the second, where echo simply prints text to the screen. Run ls -l greet.sh to confirm it has no x, make it runnable with chmod 755 greet.sh, check that the string now reads -rwxr-xr-x, and launch it with ./greet.sh. As a bonus, keep a data file safely non-runnable with chmod 644 notes.txt.