Sunday, January 3, 2016

BASH cheat sheet

Traditionally I'll put here some well known BASH commands for keeping those in one place in case I'll forget them :)

echo $BASH - should be /bin/bash - make sure that U're using bash :)

nano (vim) ~/.bash_history - shows all history of commands in bash
reset - resets terminal after crashing (eg trying to output binary file with less command etc.)

ls - list of content in current directory
ls -a - list of content in current directory including hidden files
ls -l - list of files in long format
ls -a /bin - list of all files in bin directory
ls -a Documents/ - list of all files in Documents directory without entering into that
ls -t - list by modification date
ls -S - list by size
ls -R - recursively list files in subdirectories
ls -d - don't go into subdirectories just list them
ls | wc -l - show how many files in the current directory

man - help for comands (Manual pages)
man ls - shows help related to ls command

Navigation inside man:
space - scrolls page down
b - scrolls page up
/ <option> - performs search inside help; for next occurrence of searched term press / + enter
q - exits help

pwd - Path to working directory
cd - changing directory / if without arguments navigates to Home directory
cd / - navigates to Root directory

file <filename> - shows information about file without opening it
file * - shows information about all files in current directory
open <filename> - opens file with default (specified in system) application
open <filename> -a <application> - opens file with wanted application
open . - opens current directory in Finder
cat <filename> - print content of file into console
less <filename> - show content of file with possibility of navigation (like inside of man)

tail <filename> - show end of the file
tail <filename> -n 5 - show last five lines of file
tail <filename> -f - show latest (realtime) updates in the file, suitable for debugging
head <filename> - show beginning of the file

mkdir <name> - creates a directory
touch <filename> - create an empty file (updates modification and access dates in case file already exists)
touch {a,b,c,}.txt ==> touch a.txt b.txt c.txt
touch {a..c}{1..3}.txt ==> touch a1.txt a2.txt ... c2.txt c3.txt

cp <filename with relative path> . - copy file to current directory
cp <filename with relative path> <new file name> - copy file to current directory with different name
cp -R <source directory / file> <target directory> - copy source directory / directories / files into target directory
rm <filename> - removes a file
rm -r <directory> - removes a whole directory
rmdir <directory> - removes an empty directory
mv <filename> - move a file (also can be used during renaming)
mv <filename>.. <directory> - move file / files into specified directory
mv file.{txt,jpg} dir/ ==> mv file.txt file.jpg dir/
cp / mv / rm -i - security switch - will ask if you are sure to perform an action

ls > <filename> - write content of directory into file - will override file  if exists
ls >> <filename> - write content of directory into file - will append information at the end of the existing file, otherwise will create a new file

ls | less - get output from first command and send it to input of second command

cut -f 3 <filename> | grep 4 | wc -l - get 3 rd column from file (assuming it's csv) then get all rows which contains 4  and then count how many lines contains that number

CTRL-r - reverse search in bash history - search for the latest occurrence. If wee need previous one just press CTRL-r again and so on. To exit from search mode without execution command we can use arrow (left, right) key

sort -nk2 <filename> - sort file by using second column (in space-separated file) as a key
sort -r <filename> - sort in reversed order
sort <filename> | uniq -c - sort file then counts how many time each line appeared
wc <filename> - show number of lines, words and bytes of the file
wc -l <filename> - show opnly number of lines

grep <search term> <filename> - search for all occurences in the file
grep <search term> *<filename pattern> - search for all occurences in several files
grep -i <search term> <filename> - search for all occurences in the file ignoring case
grep -v <search term> <filename> - invert search

find /bin - search for all files in bin directory
find /bin -name test - search for all files in bin directory with name test

ps ax - show all running processes
ps aux - show all running processes with owner
top - show more detailed information about running processes
CTRL-z bg - put program into background
CTRL-z fg - put program into foreground

open -a <program name> - start a program from shell
kill -KILL <process id> - hard kill for process which not responding

As usually I'm not pretending for full list of BASH options - as it's really huge but those listed above are the most common for daily usage :)