How to activate a virtual environment via Bash

Create a file named anything.sh and make it executable Paste following code in file Run ./path-to-file.sh cd path/to/env/dir && source bin/activate

November 11, 2021 · 1 min · Saqib Razzaq

How to Import All CSV Files in Directory in MySQL via Bash

Create a file import.sh and make it executable Paste following code in file mysqluser='root' mysqlpass='password' mysqldb='database_name' tablename='mysql_table_name' DIRECTORY='input_directory' for csv in $DIRECTORY/*.csv; do echo "Loading $csv" mysql -u $mysqluser --password=$mysqlpass -D $mysqldb -e "LOAD DATA INFILE '$csv' IGNORE INTO TABLE $tablenameFIELDS TERMINATED BY '\t' ENCLOSED BY '\"' LINES TERMINATED BY '\n' IGNORE 1 LINES;" done

November 11, 2021 · 1 min · Saqib Razzaq

How to quickly login to MySQL using Bash

I know this method isn’t the most secure way. But when I’m working on personal projects, it gets annoying having to enter credentials every time. Hence, I put together this little script to quickly log me in. I create file mysql.sh with following contents mysql -u username --password='password_here'

November 11, 2021 · 1 min · Saqib Razzaq

How to run a script if it is not already running in Linux

Let’s say you have a script called crawl.py and you never want two instances of it running at the same time. The following script can do the job RUNNING="$(ps aux|grep crawl|grep -v grep|wc -l)" if [ $RUNNING -eq 0 ] then python crawl.py else echo "Already running : skipping"; fi

November 11, 2021 · 1 min · Saqib Razzaq

How to ZIP all files in directory and send to another server

First install zip if not already install Ubuntu apt-get install zip Centos yum install zip Then create a file.sh and paste following code OUTFILE=outputfile.zip INPUTDIR=inputdir zip -R $OUTFILE $INPUTDIR scp $OUTFILE usrename@outserver:/path/of/ $OUTFILE -i key.pem

November 11, 2021 · 1 min · Saqib Razzaq