Wednesday, September 30, 2015

Move only subfolders or only files to another location using bash script

If you want to move only subfolders from your folder to another locations the following bash code

for x in *; do
        if [ -d "$x" ]; then
        echo "$x"
        mv -- "$x" /path/to/folder/
        fi
done

If you want to move files and not subfolders to another location use:
for x in *; do
        if ! [ -d "$x" ]; then
        echo "$x"
        mv -- "$x" /path/to/folder/
        fi
done


Tuesday, September 29, 2015

Forward standard output and standard error to a file

If you are executing a bash script in unix and need to save screen output in a file, you can redirect both standard output and standard error to a file using $>

source script_file $> output_file 

Moving through text in Nano

If you need to move through text in Nano editor in Unix you can use arrows but this can be frustrating as it is slow.
Use ctrl + space to move word by word
alt + space to go backwards word by word

Thursday, September 17, 2015

How to mount password protected network attached storage on Ubuntu

To mount NAS (network attached storage) to a Unix server such as Ubuntu 14.04.1 LTS do the following. First install cifs-utils package.


sudo apt-get install cifs-utils


Then, create a folder where the NAS will be mounted, e.g.

mkdir /home/diskstation


Next, create a file with username and password.

touch ~/.smbcredentials

Add to the file your username/password to access the NAS.

username=username
password=password

Change credentials to the file.

chmod 600 ~/.smbcredentials


Open fstab file.

sudo nano /etc/fstab

Add the following line (substitute ip.address with the IP of NAS and folder with the name of your shared folder).

//ip.address/folder /home/diskstation cifs credentials=~/.smbcredentials,iocharset=utf8,sec=ntlm 0 0



Mount the NAS for the current session.

sudo mount -a

Since you modified fstab file NAS should auto-mount every time you restart the server.

In case you want to add additional storage to the NAS through USB, you can do the same to mount it.

Add the line to stab (Substitute ip.address with your IP address of NAS, and usbshare1-2 with the folder where your usb was mounted in NAS). Before this create folder /home/diskstation2 where USB drive will be mounted on the Ubuntu server.

//ip.address/usbshare1-2 /home/diskstation2 cifs credentials=~/.smbcredentials,iocharset=utf8,sec=ntlm 0 0



Saturday, September 12, 2015

Mounting an external drive onto Ubuntu Server

Download and install gdisk
http://sourceforge.net/projects/gptfdisk/

Find if your drive is e.g. the second disk, dev/sdb and then use gdisk

sudo gdisk /dev/sdb

Command (? for help): ?
b back up GPT data to a file
c change a partition's name
d delete a partition
i show detailed information on a partition
l list known partition types
n add a new partition
o create a new empty GUID partition table (GPT)
p print the partition table
q quit without saving changes
r recovery and transformation options (experts only)
s sort partitions
t change a partition's type code
v verify disk
w write table to disk and exit
x extra functionality (experts only)
? print this menu


Then:

n       # create a [n]ew partition
[enter] # choose default partition number
[enter] # choose default first sector
[enter] # choose default last sector
[enter] # choose default, linux filesystem (8300)
v       # verify
c       # change the name of the partition, e.g. MY_BACKUP_3TB
p       # print to ensure the renaming is to your liking
w       # write the changes to disk

Now use mkfs.ext4 command from the e2fsprogs package to format the drive. If the name of the partition you have just created is sdb1 use:

sudo mkfs.ext4 /dev/sdb1
Then

mkdir /mount/point
mount /dev/sdb1 /mount/point


Tuesday, September 8, 2015

Find specific files in all subfolders and copy them to another location

If you have to copy files from multiple subfolders and they do have a certain pattern in their names, you can use a Unix find command, with the parameter -iname (case insensitive).

sudo find ./  -iname'*HCASMC*transcriptID.txt' -exec cp -r {} ~/tquertermous/tquertermous/ \;

For example, in this case transcript featureCounts files from multiple subfolders were copied, thus saving me time from going into each folder and issue a copy command.

sudo find ./  -iname'*HCASMC*transcriptID.txt' -exec cp -v {} ~/tquertermous/tquertermous/ \;
‘./HCASMC_1522_6hr_TGFB/Processed/RAPiD_2_0_0/featureCounts/HCASMC_1522_6hr_TGFB.exon.transcriptID.txt’ -> ‘/home/mpjanic/tquertermous/tquertermous/HCASMC_1522_6hr_TGFB.exon.transcriptID.txt’
‘./HCASMC_1522_1hr_PDGF/Processed/RAPiD_2_0_0/featureCounts/HCASMC_1522_1hr_PDGF.exon.transcriptID.txt’ -> ‘/home/mpjanic/tquertermous/tquertermous/HCASMC_1522_1hr_PDGF.exon.transcriptID.txt’
‘./HCASMC_1522_6hr_TNF/Processed/RAPiD.2_0_0/featureCounts/HCASMC_1522_6hr_TNF.exon.transcriptID.txt’ -> ‘/home/mpjanic/tquertermous/tquertermous/HCASMC_1522_6hr_TNF.exon.transcriptID.txt’
‘./HCASMC_2989_1hr_PMA/Processed/RAPiD.2_0_0/featureCounts/HCASMC_2989_1hr_PMA.exon.transcriptID.txt’ -> ‘/home/mpjanic/tquertermous/tquertermous/HCASMC_2989_1hr_PMA.exon.transcriptID.txt’
‘./HCASMC_2989_1hr_TGFB/Processed/RAPiD_2_0_0/featureCounts/HCASMC_2989_1hr_TGFB.exon.transcriptID.txt’ -> ‘/home/mpjanic/tquertermous/tquertermous/HCASMC_2989_1hr_TGFB.exon.transcriptID.txt’
‘./HCASMC_2989_6hr_SF/Processed/RAPiD_2_0_0/featureCounts/HCASMC_2989_6hr_SF.exon.transcriptID.txt’ -> ‘/home/mpjanic/tquertermous/tquertermous/HCASMC_2989_6hr_SF.exon.transcriptID.txt’
‘./HCASMC_2989_6hr_PDGF/Processed/RAPiD_2_0_0/featureCounts/HCASMC_2989_6hr_PDGF.exon.transcriptID.txt’ -> ‘/home/mpjanic/tquertermous/tquertermous/HCASMC_2989_6hr_PDGF.exon.transcriptID.txt’
‘./HCASMC_2989_6hr_TNF/Processed/RAPiD.2_0_0/featureCounts/HCASMC_2989_6hr_TNF.exon.transcriptID.txt’ -> ‘/home/mpjanic/tquertermous/tquertermous/HCASMC_2989_6hr_TNF.exon.transcriptID.txt’
‘./HCASMC_1522_6hr_SF/Processed/RAPiD_2_0_0/featureCounts/HCASMC_1522_6hr_SF.exon.transcriptID.txt’ -> ‘/home/mpjanic/tquertermous/tquertermous/HCASMC_1522_6hr_SF.exon.transcriptID.txt’
‘./HCASMC_1522_1hr_TGFB/Processed/RAPiD_2_0_0/featureCounts/HCASMC_1522_1hr_TGFB.exon.transcriptID.txt’ -> ‘/home/mpjanic/tquertermous/tquertermous/HCASMC_1522_1hr_TGFB.exon.transcriptID.txt’
‘./HCASMC_2989_6hr_TGFB/Processed/RAPiD_2_0_0/featureCounts/HCASMC_2989_6hr_TGFB.exon.transcriptID.txt’ -> ‘/home/mpjanic/tquertermous/tquertermous/HCASMC_2989_6hr_TGFB.exon.transcriptID.txt’
‘./HCASMC_1522_1hr_PMA/Processed/RAPiD.2_0_0/featureCounts/HCASMC_1522_1hr_PMA.exon.transcriptID.txt’ -> ‘/home/mpjanic/tquertermous/tquertermous/HCASMC_1522_1hr_PMA.exon.transcriptID.txt’
‘./HCASMC_2989_1hr_PDGF/Processed/RAPiD_2_0_0/featureCounts/HCASMC_2989_1hr_PDGF.exon.transcriptID.txt’ -> ‘/home/mpjanic/tquertermous/tquertermous/HCASMC_2989_1hr_PDGF.exon.transcriptID.txt’
‘./HCASMC_1522_6hr_PDGF/Processed/RAPiD_2_0_0/featureCounts/HCASMC_1522_6hr_PDGF.exon.transcriptID.txt’ -> ‘/home/mpjanic/tquertermous/tquertermous/HCASMC_1522_6hr_PDGF.exon.transcriptID.txt’
‘./HCASMC_1522_1hr_TNF/Processed/RAPiD.2_0_0/featureCounts/HCASMC_1522_1hr_TNF.exon.transcriptID.txt’ -> ‘/home/mpjanic/tquertermous/tquertermous/HCASMC_1522_1hr_TNF.exon.transcriptID.txt