User Tools

Site Tools


coding:bash

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 file
  • rm -r <folder name> Removing folder
  • ls display everything in directory
  • ls -l list/long format,shows file permissions,timestamps
  • ll display everything (including hidden folders) in directory
  • pwd present working directory
  • cd <folder> enter a folder
  • cd .. moves back one folder
  • cd goes back to home directory
  • cp <src> <dst> copy file
  • mv <src> <dst> move files from one location to another, only use for small transfers locally
  • mkdir <name> create a directory
  • mkdir -p <path/name> create directory and all parent directories
  • cat <file> print file content to terminal
  • more and less display files
  • head <file> display beginning of file
  • tail <file> display end of file
  • touch <filename> creates a file without opening it
  • history | 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 transfer
  • awk “Excel of the terminal” http://www.grymoire.com/Unix/Awk.html
  • sed “stream editor“ can do insertion, deletion, search, replace, supports regular expression, allows to perform complex pattern matching
  • grep 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 files
  • diff and gvimdiff show differences between two files
  • du -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 order
  • find <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 names
  • tail -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 simulation
  • wc (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 directory
  • tar -tf filename.tar.gz show contents of tar archive without extracting it
  • find . -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. Do sudo apt install xsel first
  • awk 'FNR>1' file1.txt file2.txt file3.txt > merged.txt merge .txt files into a single one except their first line
  • python3 ~/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 it
  • grep –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 installed
  • watch -n 0.5 nvidia-smi gpu output refreshed every 0.5 second
  • top -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 uses
  • xdg-open filename.pdf open the pdf file with pdf viewer
  • gpustat —-watch -i 1 &> gpu_usage.log + bg + disown -h to log the gpu usage of every second. (Need to install it before with pip install gpustat). To stop the log kill $(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)

Useful links

coding/bash.txt · Last modified: 2023/09/01 23:33 by bargun2

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki