ls - List files in current directory/folder/Verzeichnisls -lh
man - Manual (documentation) of a command. Also online in Deutsch & English. These are usually referred to as "man pages". Type "q" to quit/exit it.man ls
--help - Common command argument to show quick helpls --help
pwd - Print working directory (folder)
mkdir - Make a new directory (folder)mkdir texts
cd - Change to a new directory. "." (without quotes) represents your current directory, and ".." represents the parent directorycd texts
cd -
wget - Download a file from the web. The command "curl -O" is equivalentwget http://jon.dehdari.org/teaching/uds/text_processing_with_python_and_bash/frankie.txt
cp - Copy a filecp frankie.txt something.txt
mv - Move or rename a file/Dateimv something.txt other.txt
rm - Remove (delete) a file. Use rmdir for empty directoriesrm other.txt
cat - Concatenate (show) a filecat frankie.txt
more - Show a text file one screen at a time. Use the spacebar to go down, q to quitmore frankie.txt
head - Show the first few lines of a text filehead frankie.txt
tail - Show the last few lines of a text filetail frankie.txt
nano - Simple text editornano frankie.txt
echo - Display a message. For example, echo 'hello world'echo 'hello world!'
dos2unix - Convert a Windows-formatted text file to Unix formatdos2unix windows_file.txt
unix2dos - Vice versaunix2dos unix_file.txt
antiword - Convert Word document to plaintext fileantiword file.doc > file.txt
catdoc - Like antiwordcatdoc file.doc > file.txt
pdftk - Very useful tool for working with PDFs. You can join, split, extract specific pages, rotate, encrypt/decrypt, fill-in PDF forms. For example, to join two files:pdftk file1.pdf file2.pdf cat output new.pdf
gzip - Compress a file (also: zip, xz)gzip file.txt
zcat - Display a compressed file (also: xzcat)zcat file.txt.gz
gunzip - Uncompress a file (also: unzip, unxz)gunzip file.txt.gz
wc - Count the number of characters, words, and lines of a text file. --max-line-length is also usefulwc frankie.txt
diff - Show differences between two text filesdiff frankie.txt modified_frankie.txt
sort - Sort the lines of a text filesort frankie.txt
shuf - Shuffle (randomize) the lines of a text fileshuf frankie.txt | head
shuf frankie.txt | head
uniq - Merge repeated lines in a text file (or with --count: show number of repeated lines. Useful on sorted input). For example, to count how many times each word occurs in a text file:tr ' ' '\n' < frankie.txt | sort | uniq -c | sort -rn > word_count.txt
awk - Advanced text-processing language. For example, to reorder the first and second columns in a column-oriented data file:awk -e 'BEGIN {OFS="\t"}; {print $2, $1}' < word_count.txt > word_count2.txt
cut - Extract a column from a text filecut -f 2 word_count2.txt
cut -f 1 word_count2.txt > words.txt
paste - Merge multiple columns from different text filespaste word_count2.txt words.txt | head
tr - Substitute characters in a text filetr 'e' 'E' < frankie.txt # Replace all occurrences of "e" with "E" in frankie.txt
tr -d 'e' < frankie.txt # Delete all occurrences of "e"
tr -d 'aeiou' < frankie.txt # Delete all vowels
tr ' ' '\n' < frankie.txt # Replace all spaces with newlines
grep - Search for a given pattern in a text file. Useful arguments: -r, -i, -c, -e, -v, -o, -w (spells ricevow :-)grep 'north' frankie.txt # Search for lines containing "north" in the text file frankie.txt
grep -i 'north' frankie.txt # Search lines containing "north", ignoring uppercase/lowercase distinction
grep -c 'north' frankie.txt # Count how many lines contain "north"
grep -v 'the' frankie.txt # Search lines not containing "the" in frankie.txt
grep 'm.st' frankie.txt # Search lines containing "m", then any letter, then "st"
grep -o 'm.st' frankie.txt # Show only the matching text containing "m", then any letter, then "st"
grep -w 'north' frankie.txt # The pattern "north" must be an entire word, not part of a word
grep -iw 'north' frankie.txt # You can combine options together
pcregrep - Like grep but more advanced. See the handout on pattern matching (regular expressions).perl -p -e - Advanced substitutions/replacements in a text filerename - Rename many files, according to a pattern.rename ' ' '_' *.txt # Replaces all spaces in a filename with underscores, for all files ending in .txt
In Ubuntu/Debian and MacOS/OSX via Homebrew/MacPorts:
rename 's/ /_/g' *.txt # Replaces all spaces in a filename with underscores, for all files ending in .txt
iconv - Convert text from one encoding to another (eg. ISO-8859-1 to UTF-8)iconv -f ISO_8859-1 -t UTF-8 < file_iso.txt > file_utf8.txt
clear - Clear the screen (but it doesn't clean your monitor :-)
history - See what you've been typing in your shell
exit - Exit command-line shell
command1 | command2 - Pipe: Make the output of one command as the input to another commandcommand > output.txt - Redirect the output of a command into a fileecho 'Hi' > output.txt
cat output.txt
echo 'Bye' > output.txt
cat output.txt
command >> output.txt - Append the output of a command to the end of a (possibly already existing) fileecho 'Hi' > output.txt
cat output.txt
echo 'Bye' >> output.txt
cat output.txt
command1 && command2 - Do command2 only if command1 is successful (no errors)command1 || command2 - Do command2 only if command1 is unsuccessfulx=42 - Save the value 42 to a variable x. You can use the variable later as $xif [[ ... ]]; then command fi - Do command if the thing in square brackets is trueif [[ "$x" == 42 ]]; then
echo "Hi"
else
echo "Bye"
fi
if [[ "$x" == 3 ]]; then
echo "Hi"
else
echo "Bye"
fi
for x in a b c d; do command; done - Loop (wiederholt) over list a b c d, doing command each timefor x in Messer Gabel Schere Licht; do
echo "Oh no my boy! Nicht mit $x spielen!"
done
python3print("Hello world!")
#, and continue till the end of the line:
# This is a comment
Long comments can span mulitple lines, and start (and end) with three ' or " (single or double quotes):
'''
This is a comment
More comments
Even more
'''
a = 3
b = 5
print(a + b)
print(a * b)
print(a / b)
c = a + b
name = "Bugs Bunny"
age = 22.5
We can change variables:
j = 3
j = 5
j = j + 2 # j is now 7
j += 2 # equivalent to above line. j is now 9
j = j - 1 # j is now 8
j -= 2 # similar to above line. j is now 6
j *= 2 # what do you think j is now?
j /= 4 # what do you think j is now?
name = "Dr. " + name # name is now "Dr. Bugs Bunny"
d = 3
if d < 5:
print("d is greater than five")
elif d == 5:
print("d is equal to five")
elif d > 5:
print("d is greater than five")
else:
print("I have no idea!")
shopping_list = ['bread', 'apples', 'chocolate']
print(shopping_list[0]) # prints 'bread'
print(shopping_list[2]) # prints 'chocolate'
a = [5, -3.7, 0, "spam"]
counts = {"the":5033, "blue":218, "waffle":74}
print(counts["blue"]) # prints 218
weather = {"Monday":"sunny", "Tuesday":"cloudy"}
print(weather["Tuesday"]) # prints "cloudy"
weather["Wednesday"] = "partly sunny"
for x in ["Messer", "Gabel", "Schere", "Licht"]:
print("Oh no my boy! Nicht mit", x, "spielen!")
# Prints three through seven
for i in range(3,8):
print(i)
a = 'Hello world'
print(a.upper()) # prints 'HELLO WORLD'
print(a.replace('o','~'))
print(a[2:8])
a + "bye" # ??
a * 3 # ??
b = a.split() # b is ['Hello', 'world']
def hello():
print("Hello world!")
hello()
def hello2(x):
print("Hello", x)
hello2("Lee") # prints "Hello Lee"
def foo(x,y):
a = x * 2
return a + y
foo(3,5) # gives 11
# Reads in a file, line by line:
with open("text.txt") as f:
for line in f:
print(line.upper())
# Writes to a file:
with open("output.txt", "w") as output:
output.write("Hi there")
output.write("Bye!")
pip3 install --user matplotlib
From now on you can use this module by importing it:
import matplotlib.pyplot as plt
# First let's create some data
x = [0.5,-0.5,1,0.5,0,-0.5,-1]
y = [1.0,1.0,0.0,-0.5,-0.5,-0.5,0.0]
plt.plot(x, y, linestyle='None', marker='o')
plt.xlim(-1.5,1.5) # horizontal limits of plot
plt.ylim(-1.0,1.5) # vertical limits of plot
plt.title("Smiley")
plt.grid(True)
plt.show()
Check out some other cool examples.
| English | Deutsch | |
|---|---|---|
@ | at sign | das At-Zeichen / die Klammeraffe |
# | hash / pound / number sign | die Raute / das Doppelkreuz |
^ | caret | das Einfügezeichen / das Caret-Zeichen |
& | ampersand / and | das Et-Zeichen / das Und-Zeichen / das Kaufmannsund |
* | asterisk / star / times | das Sternchen |
- | dash / hyphen / minus | der Bindestrich |
| | pipe / vertical bar | der senkrechte Strich |
/ | slash / forward slash | der Schrägstrich |
\ | backslash | der Backslash / der Rückstrich |
: | colon | der Doppelpunkt / das Kolon |
; | semicolon | der Strichpunkt / das Semikolon |
~ | tilde | die Tilde |
_ | underscore | der Unterstrich |
( ) | parentheses | Runde Klammern |
[ ] | square brackets | Eckige Klammern |
{ } | curly brackets | Geschweifte/geschwungene Klammern |
< > | less-than sign / greater-than sign | Kleiner-als-Zeichen / Größer-als-Zeichen |
| control key | die Steuerungstaste | |
| tab key | der Reiter / der Tabulator |
| English | Deutsch |
|---|---|
| assignment | Zuweisung |
| conditional statement | bedingte Anweisung |
| loop | Schleife |
| string | Zeichenkette |
| integer | Ganze Zahl |
| scope | Umfang |