Table of Contents
Bash command line tool
The terminal, bash, or shell has been around for a long time. It is a powerful tool that allows people to do complex things with just a few keystrokes. More importantly, it helps you combine existing programs in new ways and automate repetitive tasks so you aren’t typing the same things over and over again. Use of the terminal is fundamental to using a wide range of other powerful tools and computing resources (including “high-performance computing” supercomputers).
On a mac and or ubuntu, the command line tool is called 'Terminal' On mac: go to the launchpad and type 'terminal' to find it. On ubuntu: go to 'show applications' and type 'terminal' to find it or press Ctrl+Alt+T. Windows has a terminal too, called 'PowerShell'. Don't use it unless absolutely necessary, it's weird.
Carefully go through one of the tutorials linked in the last section, either this or this one and try it out for yourself as you go.
The terminal has 'tab' completion, meaning you can start typing a command and hit 'tab' and it will try to complete it for you, which is very useful for file/folder names. You can use the 'up/down-arrows' for last commands, or Ctrl + R
to search in previous commands.
All commands and tools have manuals, you can read them by typing man <command-name>
. Also, all of them have a short help on how to use them, use <command-name> -h
to access it. All of the commands/programs/tools in the terminal can be put together into scripts, so called 'bash-scripts'. The usual convention is use the file ending '.sh' for those scripts.
You can also “chain” different commands, by piping (|
) one output into the next command, for example:
ls . | grep .txt
ls .
displays all files in the current (.
) directory. This output is taken and given to grep
, which does pattern matching and only displays all files ending in .txt
.
Useful commands
rm <filename>
Removing filerm -r <folder name>
Removing folderls
display everything in directoryls -l
list/long format,shows file permissions,timestampsll
display everything (including hidden folders) in directorypwd
present working directorycd <folder>
enter a foldercd ..
moves back one foldercd
goes back to home directorycp <src> <dst>
copy filemv <src> <dst>
move files from one location to another, only use for small transfers locallymkdir <name>
create a directorymkdir -p <path/name>
create directory and all parent directoriescat <file>
print file content to terminalmore
andless
display fileshead <file>
display beginning of filetail <file>
display end of filetouch <filename>
creates a file without opening ithistory | grep <keyword>
searches for previous command with 'keyword' in it. History won't recall a command written in a separate terminal if the terminal is not closed since the command is written.
Useful more complicated commands
rsync -arv <src> <dst>
copy file or folders, src or dst can be remote. rsync is a powerful file copying tool, can check for differences, and only transfer the changed files (-a archive/keep file permissions, -v verbose, -r recursive/copy all subfolders/files, -z compression during file transfer –exclude <pattern> excludes files matching the pattern)tar/zip
compress data into an archive, useful for long term storage, backups, and file transferawk
“Excel of the terminal” http://www.grymoire.com/Unix/Awk.htmlsed
“stream editor“ can do insertion, deletion, search, replace, supports regular expression, allows to perform complex pattern matchinggrep
search for text using regular expressions (-n display line numbers -R recursive search following symlinks –color color highlighting) Find outputs their path relative to where you ran find. Provides advanced “filters” and allows you to run commands on each of those filesdiff
andgvimdiff
show differences between two filesdu -sh <directory>/*
list all files/folders in <directorywith their size in human readable format (-s sum -h human readable)du -h -d 1 | sort -h
list file sizes (including their content) in the directory in increasing orderfind <foldername> -type f -printf '%s %p\n' | sort -nr | head -10
find the 10 largest files in the directory <foldername> and print their size and namestail -n 10 -f <filename>
prints the last 10 lines to the terminal and updates the output (-f follow), useful for checking output of a running simulationwc
(counts words, newlines, or bytes of each input file, and outputs the result)ls . | grep .pdf | wc -l
counts number of pdf files in current directorytar -tf filename.tar.gz
show contents of tar archive without extracting itfind . -type f -maxdepth 1 -size +100M -exec mv {} large_files/ \;
move all the files larger than 100MB to another folder (-100M for smaller)python3 my_script.py -i $(ls .)
to give the file(s) in the directory as input.-i $(ls ./bonds*)
also possible.pwd | tr -d '\n'| xsel -ib
copy the current directory to the clipboard. Dosudo apt install xsel
firstawk 'FNR>1' file1.txt file2.txt file3.txt > merged.txt
merge .txt files into a single one except their first linepython3 ~/scripts/plotter.py -i $(ls a160?_*energy* | grep -v 'a1601_0.energy.dat')
exclude a file (a1601) from the arguments.
Commands for 'finding things'
Often, you might be wondering where a particular script is, or a variable/line of code etc. For code, it is very useful to open the entire folder ('add project folder') with atom and then 'search in Project' to find and trace variables/functions. For scripts that might be all over the place in different sub-folders or different projects, there are some bash command-line searches one can use:
grep -rni “string_you_want” /directory
search all the text files in the folder 'directory' and list all the lines with the word 'protein' in it (better than -rnw, don't use rnw)grep -rnw /directory -e protein
search all the text files in the folder 'directory' and list all the lines with the word 'protein' in itgrep –include=\*.{c,h} -rnw '/path/to/somewhere/' -e “pattern”
same as above, but only in files ending with.h
or.c
find . -type f -name “*wrong*” -exec mv {} wrongs/ \;
move all files containing 'wrong' in their file name to the folder 'wrongs'.grep –include=\*.{hoomd,py} -rnw /Users/statt/ -e “import freud”
finds all hoomd and python scripts that include the package 'freud'.grep slurm-*.out -e 'error'
find all occurrences of the word 'error' in all slurm output files
'sysadmin'-type command line tools
top -c
displays currently running processes, interactive (check for CPU and RAM usage)kill -signalNumber -p <PID>
kills a running process (usually you can kill a process with CTRL +C in a terminal) signal Number can be: 9(kill) 15(terminate) 2(interrupt), …ps aux
shows all currently running processes as a list- System logs are stored in
/var/log/*
- boot log:
journalctl -b
nvidia-smi
shows graphics cards info if nvidia drivers are installedwatch -n 0.5 nvidia-smi
gpu output refreshed every 0.5 secondtop -b -n 1000 -d 5 -p “65250” | grep “65250” > log.txt
prints the line of process with PID 65250 from top to a log file every 5 seconds 1000 times - useful for checking how much resources (RAM) a particular process usesxdg-open filename.pdf
open the pdf file with pdf viewergpustat —-watch -i 1 &> gpu_usage.log
+bg
+disown -h
to log the gpu usage of every second. (Need to install it before withpip install gpustat
). To stop the logkill $(ps aux | grep '[g]pustat' | awk '{print $2}')
(for details : https://stackoverflow.com/questions/3510673/find-and-kill-a-process-in-one-line-using-bash-and-regex ) useful to monitor “out of memory” hoomd simulations
terminal file editors
- vi/vim is a highly configurable text editor for efficiently creating and changing any kind of text. Included as “vi” with most UNIX systems and with Apple OS X. “vim” is easier to use (arrow-keys).
- emacs full fledged text editor with features for programming
- nano simple text editor (Nano is good for editing a config file but if you are going to program you'll be better off using Vim or Emacs)