Bash
Some directories have special names:
/ # Root
~ # Home
. # Current
.. # Parent
Your first dozen commands:
$ pwd # Where am I?
$ ls # List files
$ cd ./path/to/dir/ # Change directories
$ touch file.txt # Create a file
$ rm file.txt # Remove a file
$ mkdir myDir # Create a directory
# -p or --parents makes this recursive, and silences "file exists" errors
$ rmdir myDir # Remove a directory
$ mv file1.txt file2.txt # Rename a file
$ cp file2.txt file2.txt # Copy a file
$ man someCommand # Open a man page
$ cat file.txt # Display the contents of a file
$ history # Display a list of previous commands
Filename Manipulation:
# Remove path
$ basename ./path/to/file.txt # file.txt
# Remove path and extension
$ basename ./path/to/file.txt .txt # file
# Remove last non-slash component (and trailing slash)
$ dirname ./path/to/file.txt # ./path/to
$ dirname ./path/to # ./path
$ dirname ./path # .
Redirection:
$ ls | sort # Pipe connects standard in of one program and standard out of another
$ ls > file.txt # *Overwrite* with standard *output* (same as 1>)
$ ls >> file.txt # *Append* with standard *output* (same as 1>>)
$ ls 2> file.txt # *Overwrite* with standard *error*
$ ls 2>> file.txt # *Append* with standard *error*
$ ls 1>&2 # Redirect standard *output* to standard *error*
Nifty utilities:
$ wc --lines file.txt # Print number of lines, same as -l
$ wc --words file.txt # Print number of words, same as -w
$ wc --chars file.txt # Print number of characters, same as -m
$ wc --bytes file.txt # Print number of bytes, same as -c
$ nproc # number of processors
$ some-utility --dump-data-to-terminal | less # Ease navigation of large output
$ du -h file.txt # Get the *disk usage* of a file (not the actual size of the file; -h makes it readable)
$ time some-utility # Prints the time (in seconds) taken to run a utility to standard out.
Command construct: $(command)
$ $(command) # Runs command and treats the output as a set of commandline arguments
Executing multiple commands in succession:
$ make; make install # Execute make, followed by make install
$ make && make install # Only execute make install if make succeeds
$ make || cleanup # Only execute cleanup if make fails
Copy/Remove a directory and all its contents:
$ cp -r ./dir1 ./dir2
$ rm -r ./dir1
Clearing the screen and scroll history:
$ reset # Ubuntu
Ctrl + k # OS X
Nice things to add to ~/.bashrc
:
PS1="$ "
alias ls='ls -1Fa --color'
And to ~/.inputrc
:
## arrow up
"\e[A":history-search-backward
## arrow down
"\e[B":history-search-forward
Echo exit value of previous command:
$ echo $?
Find a file:
$ find ./ -name nameOfFile.txt # By name
$ find ./ -type f -name "*.py" # With a particular extension
Find a string:
$ grep -RIn "String" # I: Ignore binaries; n: Print line numbers; R: Search recursively
Navigation:
Ctrl + a # Go to beginning of line
Ctrl + e # Go to end of line
Ctrl + u # Clear everything before cursor
Ctrl + d # Close terminal
Ctrl + c # Send SIGINT (abort application)
Ctrl + z # Send SIGTSTP (suspend application)
$ fg # Bring suspended application back to foreground
Extracting Pages from a PDF:
$ pdftk original_file.pdf cat 12-15 output output_file.pdf # Extract Pages 12 to 15
List shared libraries linked to by a binary:
$ ldd <binary> # Linux
$ otools -L <binary> # Mac
Text replacement:
# For each line in file.text, replace the first instance of 'day' with 'night'
# Send to standard out
# Note that file.txt is unmodified
$ sed s/day/night/ file.txt
# The same result can be obtained by pipine the file to sed
$ cat file.txt | sed s/day/night/
# The result can of course be piped to a new file:
$ sed s/day/night/ file.txt > new_file.txt
# Alternatively, the modifications can be made in-place.
$ sed -i s/day/night/ file.txt
# Often, you will want to process by instance rather than by line.
$ sed -i s/day/night/g file.txt
Shell Options:
# List shell options (e.g., those containing "glob")
# -u means "on", -s means "off"
$ shopt -p | grep "glob"
# shopt -u dotglob
# shopt -s extglob
# shopt -u failglob
# shopt -u globstar
# shopt -u globasciiranges
# shopt -u nocaseglob
# shopt -u nullglob
# Turn on a shell option (e.g., globstar)
$ shopt -s globstar
# Turn off a shell option (e.g., globstar)
$ shopt -u globstar
Best Practices:
set -u # Exit if you attempt to use an unset variable
set -o nounset # Same thing, more verbose
set -e # Exit if any command fails
set -o errexit # Same thing, more verbose
set -o pipefail # Fail if any command in a pipe fails, not just the last
# Documentation!
: "
You can use this syntax to write a multiline comment with no effect.
"
readonly FILE="some_file_that_will_never_change.txt" # Be const-correct!
Converting video files with ffmpeg :
# Convert one type of movie to another
$ ffmpeg -i input.avi output.mov
# Set video quality (1 = highest quality)
$ ffmpeg -i output.mov -qscale:v 1 output.wmv
# Convert pngs matching a pattern to a video
$ ffmpeg -framerate 7 ./directory/%d.png -qscale:v 1 output.ogg