Skip to content

Instantly share code, notes, and snippets.

@ackman678
Last active January 25, 2018 01:30
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ackman678/6290329 to your computer and use it in GitHub Desktop.
Brief command shell tutorial. Contains a few unix tips and tricks for working with and examining data files for science. A couple of the examples assume that the working environment is Mac OS X (e.g. pbcopy).

Tags: unix, os x, linux, command line, terminal, bash, shell
Date: 2013-08-20 23:09:02

Command line tutorial

To learn more, try the command line basics and UNIX tutorials found at http://www.ee.surrey.ac.uk/Teaching/Unix/ and http://www.westwind.com/reference/OS-X/commandline/navigation.html.

Requirements

Open a command line shell:

  • use 'Terminal' on OS X or Linux
  • use cygwin on Windows (need to install first)

Examples

List files

ls

Change directory

cd Desktop

Change up one directory level

cd ..

Print current directory location

pwd	

Go to root of drive

cd /

Go back to user home folder

cd ~

Read in the Lord Of The Rings (LOTR) calendar with concatenate (should work on most Linux and Mac OS X systems):

cat /usr/share/calendar/calendar.lotr

Read help for cat

man cat

Use b and spacebar to move forward and backwards a page at time when viewing man help pages. Press q to quit.

Find all lines about 'Frodo' by piping the output from 'cat' into the 'grep' find program with the pipe operator |:

cat /usr/share/calendar/calendar.lotr | grep 'Frodo'

Same exact result as giving grep both original inputs:

grep 'Frodo' /usr/share/calendar/calendar.lotr

Find all lines ending with 'Gandalf' using a 'regular expression' search pattern. To do this use the flag -e and the $ in the search pattern to indicate the line ending position:

cat /usr/share/calendar/calendar.lotr | grep -e 'Gandalf$'

Save this output to a file on the Desktop. Notice the usage of both a pipe | and a redirection of output >:

cd ~/Desktop
cat /usr/share/calendar/calendar.lotr | grep -e 'Gandalf$' > testfile.txt

Append something to the file using the redirection with append marker >>:

echo 'Double Rainbow!' >> testfile.txt

Useful examples

Print file usage

List usage in human-readable format for a directory

du -sh * 

List file creation timestamps in seconds and filenames from ls command

ls -lT *.tif | sort | awk '{ print $10, $8 }' > frametimes.txt
find . -name '*.tif	' | xargs ls -lT | sort | awk '{ print $10, $8 }' | sed -Ee 's/^.\///g' > ~/Desktop/frametimes.txt

List date times for .tif CCD movies for pasting into a spreadsheet

ls -lTU *.tif | awk '{ print $6, $7, $8, $9}' > ~/Desktop/frametimes.txt   #U is file creation date-- file modification time might be better...
ls -lT *.tif | awk '{ print $6, $7, $8, $9}' | pbcopy
ls -lT *.tif | awk '{ print $10}' > ~/Desktop/frametimes.txt

List filenames without pathnames

ls -lT *.tif | awk '{ print $10}' | pbcopy  
ls -lTU *.tif | awk '{ print $6, $7, $8, $9 }' > ~/Desktop/movietimes.txt

Print tiff header information

tiffutil -info filename.tif

Copy files only less than 500MB in size

rsync -av --max-size=500M FilePathToSource FilePathToDestination
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment