Imamadmad's Test Wiki
Register
Advertisement

This is a page of syntax for the UNIX command line tool known as Bash. It bears no relation to the violent action of bashing.

Navigation[]

That's not important right now.

Dealing with files[]

wget <URL> ##  Retrieves files from the internet and saves to the current directory
cut -d "<delimitor (default = \t)>" -f <columns to use>
sort ## sort alphabetically from first column
sort -k 2n ## sort numberically from 2nd column
sort -t$'\t' -k3 -nr ## sort with delimiter \t (tab) the 3rd row reverse numerically.
uniq ## cuts out duplicates (after sorting)
uniq -c ## counts the number of duplicates (after sorting)
uniq -u ## only prints unique lines
uniq -d ## only prints duplicate lines
tail -1 ## Get last lign
tail -n +2 ## Get everything from line 2
head -4 ## Get the top 4 lines
egrep '<regex string>' ## Finds all instances
egrep -i '<regex string>' ## Case insensitive
cat <filename/path> ## prints out contents of file
less <filename/path> ## prints out contents in a more navigable format
wc ## Prints word/line/character count
wc -l ## line count only
wc -c ## character count only
wc -w ## word count only
paste file1 file2 ## Outputs contents of files in two tab separated columns
paste -d '<delimitor>' file1 file2 ## Used chosen delimitor to separate columns
paste --- < file1 ## file is organised into 3 columns
tr 'a-z' 'A-Z' ## Replaces first block with second block (in this case, capitalises all)
tr -d 'a' ## Deletes set
tr -d -c 'aeiou' ## Deletes everything not in set

Note: files must be redirected into tr

sed -E 's/regex1/regex2/g' ## Replaces regex1 with regex2 wherever it is in the file
sed -E -i "s/regex1/regex2/g" ## As above, but case insensitive
tar -cvzf backup.tgz /home/user/project
tar -xvzf backup.tgz

awk[]

Select rows based on column properties:

awk '$2 > 6' filename ## selects rows where column 2 is greater than 6
awk '$1 == "string"' filename

Reorder columns:

awk '{ print $2, $1; }' filename

Output and input[]

Receive input from a file:

tr '\n' '\t' < filename

Send the output of one operation to the next using a pipe:

cut -f 2,3 filename | sort | uniq | less

Put standard output into a file:

...| cat > filename.extention

Append standared output to end of file:

...| cat >> filename.extension

Concatenate files:

cat file1 file2 > file3

Help and other useful things[]

man <whatever> ## opens man page for topic
whatis <whatever> ## Gives one line description of topic
exit ## finishes processes and closes shell session
Advertisement