Skip to content

Instantly share code, notes, and snippets.

View stevekm's full-sized avatar

Stephen Kelly stevekm

View GitHub Profile
@stevekm
stevekm / Random_String_for_Filenames.sh
Last active September 16, 2015 14:47
Generate a random string of letters and numbers
#!/bin/bash
# I needed a unique random name for files generated by scripts
# so they don't get overwritten on subsequent compiles
# use mktemp to generate a randomly named tmp directory, save the name to a variable
BASE=$(mktemp)
echo $BASE # the output looks like this: /tmp/tmp.XMbgXwwviZ
# we only want the base random string, not the file path
# use these two steps to strip out the file path
BASE2=${BASE///tmp}
@stevekm
stevekm / Match_Pattern_Do_Thing.sh
Last active September 16, 2015 14:44
If pattern exists within files, perform action, else do nothing (or another action)
#!/bin/bash
$PATTERN="MA0007.2_AR" # the gene we are interested in
cd ~/OutputDirectory/ # where your data files are stored
if [[ -n $(grep -Fl $PATTERN *.txt) ]] ; then
# grep; -F fixed pattern, -l ouput file name of matches
# [[ -n ]]; test (see man test), -n nonzero string length
# grep searches files for our pattern in all .txt files,
# and outputs filenames if found
@stevekm
stevekm / If_Directory_Exists.R
Last active August 29, 2015 14:24
Check to see if directory exists. If so, delete the directory and re-create it. If not, make the directory
# the directories we are using
DIRS<-c("/home/user/DataOutput/",
"/home/user/DataOutput/A/",
"/home/user/DataOutput/B/")
# iterate through the vector of directories
for(i in 1:length(DIRS)){
if(dir.exists(DIRS[i])){
# delete the directory
unlink(DIRS[i],recursive = T,force = T)
@stevekm
stevekm / Parrallel_forloop_HOMER.sh
Last active September 16, 2015 14:46
Multiple HOMER motif analysis instances run in parrallel
#!bin/bash
for i in {1..5}
do
( echo $'\n Starting primary UP motif analysis in parrallel' $i >> $WorkDir/HOMER_TIMER.txt
date >> $WorkDir/HOMER_TIMER.txt
sleep $[ ( $RANDOM % 30 ) + 10 ]s
findMotifsGenome.pl $UpARPeaks hg19 $MotifDir/Up/$i -size 200 -p 30
echo $'\n Finished primary UP motif analysis ' $i >> $WorkDir/HOMER_TIMER.txt
date >> $WorkDir/HOMER_TIMER.txt ) &
done
@stevekm
stevekm / Split_7z.sh
Last active September 16, 2015 14:46
Split files >4GB into <4GB archive files for FAT32 storage devices
#!/bin/bash
find -type f -size +4000M | while read FILE; do 7z a /media/user/StorageDevice/"${FILE}.7z" -mx0 -v4000m "$FILE"; done
@stevekm
stevekm / My_Favorite_rsync.sh
Last active September 16, 2015 14:46
Use rsync to backup all your data from a remote server
#!/bin/bash
rsync -avzheR --progress --max-size=500K -e "ssh -p 22" user@server.com:/home/user/ /home/user/ServerBackups/
# "ssh -p 22" = connect via ssh, port 22
# -a, --archive archive mode; same as -rlptgoD (no -H)
# -v, --verbose increase verbosity
# -z, --compress compress file data during the transfer
# -h, --human-readable output numbers in a human-readable format
# -e, --rsh=COMMAND specify the remote shell to use
# -R, --relative use relative path names
# rsync /source/ /destination/
@stevekm
stevekm / concatenated_index_log.sh
Last active September 23, 2015 19:41
Create an index log that lists the names of desired directories and ID values pulled from .csv file within each directory. This version of the script is depricated and has been replaced by dir_name_indexLogger2.sh, but I still like some of the code from this so I wll keep it around
#!/bin/bash
# save directory names and their associated ID's into an index log
# set the proper permissions for all files created by this script
umask 007
# make sure that the log file exists
LOG=/filepath/PARENT_DIR/logs/index_log.tsv
touch $LOG
@stevekm
stevekm / gzip_howto.sh
Last active September 22, 2015 17:20
Gzip a directory
#!/bin/bash
gzip -cvr /path/to/input/dir/ > /path/to/output.gz
# -c std out
# -r recursive through dir
# -v verbose, of course
# pipe to file you want output in
@stevekm
stevekm / dir_name_indexLogger2.sh
Created September 23, 2015 19:38
Generate a log based on directory names, and enter values from each directory into the log's entries
#!/bin/bash
# set the proper permissions for all files created by this script
umask 007
# make sure that the log file exists
LOG=/path/to/log.tsv
touch $LOG
# find the list of directory names to enter into the log, they all start with a 1
FILES=$(find /path/to/dirs/1* -maxdepth 0 -type d)
for i in $FILES; do
@stevekm
stevekm / tar_dir_md5.sh
Last active September 24, 2015 20:13
Tar a directory, generate an md5sum, view files, extract contents. Use this over gzip alone.
#!/bin/bash
INPUT_DIR=/path/to/dir/
OUTPUT_FILES=/path/to/output.tar.gz
OUTPUT_DIR=/path/to/
# create a tar.gz from a directory
tar -cvzf $OUTPUT_FILES $INPUT_DIR
# generate a md5sum for the tar.gz