Linux

Sorting and merging files

The following script merges several files into one file.

#!/bin/bash
echo "Please specify a directory"
read folder
cd $folder
ls | sort -n
for i in *.fasta
    do
    cat $i >> virus
done

Line 5 creates a sorted list of directory contents. Line six loops through all files with .fasta extension. Line 8 appends contents of every file to the file virus.

Changing file extensions

Suppose I need to change the file extensions of certain files in a directory.

#!/bin/bash
echo "Please specify a directory: "
read folder
cd $folder
for i in *.txt
    do
    mv "$i" `basename $i txt`fasta
done

The directory address specified by the user is read into the script in line 2. This address is used to cd to the directory. This script changes the extensions of .txt files to .fasta.

Displaying directory contents

For security reasons, most servers on the Internet do not show the contents of a directory if the directory address is typed. The user is either redirected to index.whatever, a default page, page not found or access denied. Thus the list of files in the directory are not displayed.

If you need to show the contents of your directory, you can use the following code:

Redirection

UNIX processes generally take input from standard input (keyboard), write output to standard out (screen) and may write to standard error if there are any error message which need to be displayed. Among the various powerful functions of Linux systems is the ability to redirect both input and output of commands.

The > symbol redirects output of a command. For example, cal command prints the calendar to screen. Using > we can save this calendar into a file.

bash - printing parameters

A parameter is an entity that stores value and is available to a program. There are many system variables available to BASH. They could be retrieved easily by prefixing a $ sign to the variable. Alternately, you can also surround them with curly braces and prefix a $ sign

#!/bin/bash
#: Title       : print parameters
#: Version     : 1.0
#: Description : print parameters
#: Options     : None

printf "%s " "Current Working Directory"
echo $PWD
printf "%s " "Home Directory"
echo ${HOME}
printf "%s" "Command Directories $PATH"

Output of the script

Ubuntu: could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName

Error: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName

Solution: 1. Open httpd.conf for editing

sudo vi /etc/apache2/httpd.conf

You might be surprised to see that the file is empty

  1. type the following:

    ServerName localhost

  2. Save file and restart apache server

    service apache2 restart

Linux vs Linux Distributions

What is the difference between Linux and Linux distribution?

Linux is the core of the operating system i.e. the kernel. Kernel manages the physical resources of the computer and performs tasks such as starting and stopping programs, allocating memory, managing the processor, and managing network connections.

Creating users and groups in Linux

Note: To try out the examples in this document, you would need root access.

To create a new user you use the adduser command. To add a user by called linuxjunky:

$ adduser linuxjunky

If you get a command not found error

$ /usr/sbin/adduser linuxjunky

To assign a password to your new user

$  passwd linuxuser

To grant ownership of a file to the new user

$ chown linuxjunky filename

To change grant ownership of a directory to the new user

split command

In Linux you can use split and join commands to split large files into smaller files or join many smaller files into a large file. This kind of operations are often necessary when you are dealing with large quantities of data.

split

Following is the default functionality of split. It splits a large file every thousand lines and creates new files.

$ split largefile.txt

$ ls
largefile.txt  xaa xab  xac  xad

$ wc -l *
3285  largefile.txt
1000  xaa
1000  xab
1000  xac

You can also define the number of lines you want in each file

Pages