Chapter 14 Unix commands
by Stephanie Djajadi, Kunal Mishra, Anna Nguyen, and Jade Benjamin-Chung
We typically use Unix commands in Terminal (for Mac users) or Git Bash (for Windows users) to
- Run a series of scripts in parallel or in a specific order to reproduce our work
- To check on the progress of a batch of jobs
- To use git and push to github
14.1 Basics
On the computer, there is a desktop with two folders, folder1 and folder2, and a file called file1. Inside folder1, we have a file called file2. Mac users can run these commands on their terminal; it is recommended that Windows users use Git Bash, not Windows PowerShell.
14.2 Syntax for both Mac/Windows
When typing in directories or file names, quotes are necessary if the name includes spaces.
| Command | Description |
|---|---|
cd desktop/folder1 |
Change directory to folder1 |
pwd |
Print working directory |
ls |
List files in the directory |
cp "file2" "newfile2" |
Copy file (remember to include file extensions when typing in file names like .pdf or .R) |
mv “newfile2” “file3” |
Rename newfile2 to file3 |
cd .. |
Go to parent of the working directory (in this case, desktop) |
mv “file1” folder2 |
Move file1 to folder2 |
mkdir folder3 |
Make a new folder in folder2 |
rm <filename> |
Remove files |
rm -rf folder3 |
Remove directories (-r will attempt to remove the directory recursively, -rf will force removal of the directory) |
clear |
Clear terminal screen of all previous commands |
14.3 Running Bash Scripts
| Windows | Mac / Linux | Description |
|---|---|---|
chmod +750 <filename.sh> |
chmod +x <filename.sh> |
Change access permissions for a file (only needs to be done once) |
./<filename.sh> |
./<filename.sh> |
Run file (./ to run any executable file) |
bash bash_script_name.sh & |
bash bash_script_name.sh & |
Run shell script in the background |
14.4 Running Rscripts in Windows
Note: This code seems to work only with Windows Command Prompt, not with Git Bash.
When R is installed, it comes with a utility called Rscript. This allows you to run R commands from the command line. If Rscript is in your PATH, then typing Rscript into the command line, and pressing enter, will not error. Otherwise, to use Rscript, you will either need to add it to your PATH (as an environment variable), or append the full directory of the location of Rscript on your machine. To find the full directory, search for where R is installed your computer. For instance, it may be something like below (this will vary depending on what version of R you have installed):
C:\Program Files\R\R-3.6.0\bin
For appending the PATH variable, please view this link. I strongly recommend completing this option.
If you add the PATH as an environment variable, then you can run this line of code to test:
Rscript -e “cat(‘this is a test’)", where the -e flag refers to the expression that will be executed.
If you do not add the PATH as an environment variable, then you can run this line of code to replicate the results from above:
“C:\Program Files\R\R-3.6.0\bin” -e “cat(‘this is a test’)”
To run an R script from the command line, we can say:
Rscript -e “source(‘C:/path/to/script/some_code.R’)”
14.5 Checking tasks and killing jobs
| Windows | Mac / Linux | Description |
|---|---|---|
tasklist |
ps -v |
List all processes on the command line |
top -o [cpu/rsize] |
List all running processes, sorted by CPU or memory usage | |
taskkill /F /PID pid_number |
kill <PID_number> |
Kill a process by its process ID |
taskkill /IM "process name" /F |
Kill a process by its name | |
start /b program.exe |
Runs jobs in the background (exclude /b if you want the program to run in a new console) |
|
nohup |
Prevents jobs from stopping | |
disown |
Keeps jobs running in the background even if you close R | |
taskkill /? |
Help, lists out other commands |
To kill a task in Windows, you can also go to Task Manager > More details > Select your desired app > Click on End Task.
14.6 Running big jobs
For big data workflows, the concept of “backgrounding” a bash script allows you to start a “job” (i.e. run the script) and leave it overnight to run. At the top level, a bash script (0-run-project.sh) that simply calls the directory-level bash scripts (i.e. 0-prep-data.sh, 0-run-analysis.sh, 0-run-figures.sh, etc.) is a powerful tool to rerun every script in your project. See the included example bash scripts for more details.
- Running Bash Scripts in Background: Running a long bash script is not trivial. Normally you would run a bash script by opening a terminal and typing something like
./run-project.sh. But what if you leave your computer, log out of your server, or close the terminal? Normally, the bash script will exit and fail to complete. To run it in background, type./run-project.sh &; disown. You can see the job running (and CPU utilization) with the commandtoporps -vand check your memory withfree -h.
Alternatively, to keep code running in the background even when an SSH connection is broken, you can use tmux. In terminal or gitbash follow the steps below. This site has useful tips on using tmux.
Deleting Previously Computed Results: One helpful lesson we’ve learned is that your bash scripts should remove previous results (computed and saved by scripts run at a previous time) so that you never mix results from one run with a previous run. This can happen when an R script errors out before saving its result, and can be difficult to catch because your previously saved result exists (leading you to believe everything ran correctly).
Ensuring Things Ran Correctly: You should check the
.Routfiles generated by the R scripts run by your bash scripts for errors once things are run. A utility file is include in this repository, calledrunFileSaveLogs, and is used by the example bash scripts to… run files and save the generated logs. It is an awesome utility and one I definitely recommend using. Before usingrunFileSaveLogs, it is necessary to put the file in the home working directory. For help and documentation, you can use the command./runFileSaveLogs -h. See example code and example usage forrunFileSaveLogsbelow.