Most important bash commands for managing processes, Git, Python, R, SQL/SQLite and LaTeX for researchers and data scientists.
Most important bash commands for managing processes, Git, Python, R, SQL/SQLite and LaTeX for researchers and data scientists. This cheat sheet only focusses on bash commands run from the terminal.
ps + enter
kill -9 <PID>
OR:
control + C
(twice if needed)crontab -e
in the terminal<minutes> <hours> <day of month> <month> <day of week>
6 0 * * 1-6 cd /home/annerose/Python/continuousscraper/ && python processcontrol.py
For more information, see
ps -e
in the terminal to see all existing processes.kill -9 <processid>
Tmux allows to keep processes running after ending an ssh session. For more detailed explanation, see here.
tmux
into the shellCtrl+B
and then D
You can now safely logoff from the remote machine, your process will keep running inside tmux. When you come back again and want to check the status of your process you can use tmux attach
to attach to your tmux session.
If you want to have multiple session running side-by-side you should name each session using Ctrl-B
and $
. You can get a list of the currently running sessions using tmux list-sessions
.
Some more useful tmux commands (see also this video):
Command | Significance |
---|---|
control + -b <command> |
to tell the shell that it’s for tmux and not just normal shell. |
control + -b p |
previous window |
control + -b n |
next window |
control + -b c |
create window |
control + -b w |
list windows |
control + -b % |
split window vertically into two parts |
control + -b |
split-horizontally : split window horizontally |
tmux - new s <sessionname> |
create a new tmux session |
control + -x |
close (kill) tmux pane |
control + -b d |
detach from tmux session. (without stopping the process) |
tmux list-sessions |
List all tmux sessions |
tmux attach -t <sessionname> |
attach to a certain tmux session |
tmux attach |
attach all tmux sessions/ any tmux session |
touch ~/.bash_profile; open ~/.bash_profile
touch
creates the file, so no need to run this command when the file already exists. Alternative:
open ~/.bashrc
For editing the .bash_profile. opens in a text editor. See here
git clone https://github.com/your-name/repository-name.git
git add .
This recurses into sub-directories. Alternative: git add
or git commit -a
git commit -m “your commit message”
. Commit the changes.git push
. Push the changes.To see the status of your repository: git status
.
See this useful blog.
Branches are very important when you collaboratively work on Github.
This github page contains useful information on how to create a new branch and how to manage branches on github.
git checkout -b [name_of_your_new_branch]
git push origin [name_of_your_new_branch]
git branch
. (If there is only the master branch, it will return * master
.)git remote add [name_of_your_remote]
. A remote (URL) is Git’s fancy way of saying “the place where your code is stored.” (see here)git push [name_of_your_new_remote] [name_of_your_branch]
git fetch [name_of_your_remote]
git checkout master
. Then simply type git merge [name_of_your_branch]
.See here
Create a global .gitignore file (file types to be excluded from every git project):
git config --global core.excludesfile ~/.gitignore_global
The file is found under Documents/Username (as a hidden file). Open it in a text editor to edit it and add files you don’t want to sync with git/GitHub.
In the terminal, go to the working directory of the project you want to commit to github.
touch .gitignore
The file is found locally in the working environment of the project. Open it in a text editor to edit it and add files.
The following procedure should help you considerably to prevent conflicts in collaborative Github and Git project.
Before you start working: pull
Once you’ve made any changes to the project:
To summarize: pull, commit, pull, clean, push
See this Stackoverflow post: http://stackoverflow.com/questions/5599122/problems-with-entering-git-commit-message-with-vim
If there is a conflict between your local version of the project and the version on Github, a window of the VIM editor will open after you’ve tried to commit your local changes. In this case, you should proceed as follows:
i
into the VIM editor, which opens the editing (insert”) modeEsc
to be sure to have left insert mode:wq
followed by Enter
, which writes the current file and then closes it.See this helpful page on how to push commits from the terminal when using two-factor authentification on Github:
https://gist.github.com/wikimatze/9790374
Important: You need to use your personal access token, not your Github password to push commits from the terminal.
workon <name of virtual environment>
workon annerose2015-11 # python 2 environment
workon annerose_python3_2016-07 # python 3 environment
How to set up and manage virtual environments in Ubuntu: http://askubuntu.com/questions/244641/how-to-set-up-and-use-a-virtual-python-environment-in-ubuntu
touch ~/.pycharmrc; open ~/.pycharmrc
See here.
Then set the shell Preferences->Tools->Terminal->Shell path to
/bin/bash --rcfile ~/.pycharmrc
pip freeze
Start scrapy project for webscraping: enter the following command in the terminal (in the directory where you want to start your project).
scrapy startproject name_of_project
open -n -a "rstudio"
in terminalAdd the following commands in shell after having created the project in Github:
git remote add origin https://github.com/your-name/repository-name.git
git config remote.origin.url git@github.com:your-name/repository-name.git
git pull origin master
git push origin master
Render/compile an R Markdown file from Terminal:
render("yourfile.Rmd")
This resource on R Markdown is helpful.
An R Markdown cheatsheet is available from RStudio here.
Set options, even options that aren’t defined by default. This can be useful for example for setting your consumer key, consumer secret etc. of your Twitter app:
> options(<name of new option> = "")
> options(consumer_key = "xyz")
> getOption("consumer_key")
[1] "xyz"
How to repair db database: see stackoverflow
echo '.dump'|sqlite3 corrupt.db|sqlite3 corrupt_fixed.db
cat <( sqlite3 corrupt.db .dump | grep "^ROLLBACK" -v ) <( echo "COMMIT;" ) | sqlite3 corrupt_fixed.db
Leaving out duplicates:
SQLiteBrowser is well suited for viewing and editing database files compatible with SQLite.
If you want to view several databases side by side, you have to open a new SQLiteBrowser window from terminal (it doesn’t seem to be possible to open a new window from within SQLiteBrowser). To this end, go to the directory where your applications are stored (in Mac). Normally, this should be:
cd # go to home directory
cd ../../.. # go three levels up
cd Applications # go to Applications
Thereafter, type the command to open a new SQLiteBrowser window:
open -n sqlitebrowser.app
detex
or (2) texcount
detex
: detex <document_name>.tex | wc -w -c -l
or just detex <document_name>.tex | wc
pdftotext <document_name>.pdf - | wc -w
texcount
: texcount -1 <document_name>.tex
texcount
texcount -1 -incbib <document_name>.tex
texcount -1 -incbib <main_document>.tex <appendix>.tex
texcount
, see this website