Linux intro for n00bs

general things about the unix shell

-> - TAB

The tabulator key acts as an auto completition function.

$ cd /ho press tab

$ cd /home/

If there are multiple options to complete the word, it will list you the possibilites.

$ sudo apt-get up press tab a few times

$ sudo apt-get up
update upgrade
$ sudo apt-get up

^C - CTRL + C

The ^C shortcut is the universal cancel or stop command to stop a running process. There are other programms who use the ESC or q (quit) keys to stop the process.


| - pipe

This is not a command, but an instruction that tells the shell to forward standard output to the standard input of another programm. This way, we can chain commands to each other.

examples

$ echo -e "Hello, \nWorld!" | grep -i "hello"
$ Hello,

> - single arrow

Appending an arrow to a command means, that the output of this command shall be forwarded into a file. The single arrow overwrites the contents of the destination file.

examples

$ echo "Hello, World!" > hello.txt creates hello.txt and writes Hello, World! into it

$ ./script.sh > /dev/null ignores the output of script.sh by forwarding the output into /dev/null


>> - double arrow

Appending a double arrow is like using a single arrow. But this time, the forwarded text does not overwrite the contents of the destination file. The double arrow appends the forwarded text to the destination file.

examples

$ python ./foo.py >> foo.log appends all output generated by foo.py to foo.log


folders and files

synopsis meaning
/ root
. current working directory
.. the parent directory
~ home directory of the current user
* all files
*.foo all .foo files

examples

$ ls /home list the contents of the home directory (starting at root /)

$ cd .. change to the parrent directory

$ rm -rf *.txt delete all .txt files

$ ./script.sh execute the file script.sh inside the current folder

$ cd ~/Downloads change to the Downloads foler in your home directory


Text editors

There is litterally a war on which text editor is better. namo is a simple lightweith editor that works for most thihngs. vim or vi is more complex but offers mor functionality. emacs is another complex one.

nano

$ nano [file]

key command
^X quit
^O save
^V next page
^Y previous page
^W search

vim or vi

$ vim [file]

There is a normal mode where you can navigate with the arrow keys and an insert mode. Press i to enter the insert mode and write stuff where the cursor is.

Ah fuck it, I hate vim! Try the tutorial if you want to know more about it.

$ vimtutor


some essential tools

cd - change directory

this is used to change your position in the file system

synopsis

$ cd [directory]

examples

$ cd .. change to the parent folder

$ cd /home/user change to a users home directory


ls - list folder contents

this command displays the files and folders inside a folder

synopsis

$ls [option] [directory]

examples

$ ls is the same as $ ls . displays the contents of the current folder

$ ls -l displays the contents in a more detailed list

$ ls -la displays all the content (including . and ..) in a list

$ ls /var/www displays the contents of /var/www

( sl - steam locomotive )

Only if sl is installed on the system. When you mistype ls, something funny happens.


mkdir - create directory

This command creates a folder.

synopsis

$ mkdir [path]

examples

$ mkdir foo create the folder foo inside the current folder $ mkdit /home/user/Public/foo create the folder foo inside the home/Public folder


touch - create file

This command actually changes timestamps, but is also used to create files.

synopsis

$ touch [options] [files]

examples

$ touch foo.txt bar.txt creates the files foo.txt and bar.txt in the current ddirectory


cp - copy

synopsis

$ cp [options] [source] [destination]

examples

$ cp hello.txt /var/www/html copies hello.txt to /var/www/html

$ cp -r ~/Downloads /mnt/usb recursively copies all the contents inside Downloads to /mnt/usb

$ cp hello.txt hello.txt.backup makes a copy of hello.txt at the same place


mv - move (rename) files

synopsis

$ mv [options] [source] [destination]

examples

$ mv hello.txt /var/www/html moves hello.txt to /var/www/html

$ cp hello.txt hello-world.txt renaming a file


rm - remove / delete files

synopsis

$ rm [options] [files]

examples

$ rm foo.sh deletes foo.sh

$ rm -r /home/user/folder recursively deletes folder and its contents

$ rm -rf folder recursively removes folder without asking (forcefully)


man - manual

Almost every command has a manual on how to use it. All available options are explained there.

synopsis

$ man [command]

You can quit the man page by pressing q.

examples

$ man ls shows the man page to the command ls


chmod - change the file mod bits

Each file has permissions. There are three types of permissions, r, w and x. They stand for read, write and execute. You can see the permissions for example when you use the ls -l command. The permissions are set by three bits like so.

permission r w x
bit 4 2 1

Also you have to set the bits for each group. There are again three groups: user, group and public.

The maximum permission you can give is 777, so everyone can do everything. Or 755 - everyone can execute the file, but only you can change (write) it.

synopsis

$ chmod [mode] [file]

examples

$ chmod +x foo.sh make foo.sh executable

$ chmod -w foo.sh remove write permissions for foo.sh

$ chmod 731 foo.sh be more specific on the permissions for foo.sh


sudo - super user do

Sometimes sou need root privileges to do something. That's the moment you use sudo. It grants you root privileges for only the command you are about to execute.

synopsis

$ sudo [command]

examples

$ sudo chmod +x foo.sh change the permissions on foo.sh with root privileges

execute the previous command with root privileges

$ mkdir /etc/bar
$ mkdir: cannot create directory ‘/etc/bar’: Permission denied
$ sudo !!

cat - print files to the standard output

This one prints out the contents of the file to the standard output (into the terminal).

synopsis

$cat [option] [file]

examples

$ cat foo.txt just prints the text inside foo.txt to the console

$ cat -n foo.txt also numbers all outputted lines


grep - print lines matching a pattern

Grep is a very good tool to filter output to highlight only the stuff that is interessting to you.

synopsis

$ grep [options] [pattern] [file]

examples

$ grep "user" foo.txt prints out only the lines inside foo.txt containing the word user

$ grep user foo.txt does the same thing as the line above

$ grep -i "password" bar.txt the -i option ignores upper case / lower case

$ grep --color "alias" ./bashrc prints all the aliases defined in .bashrc and highlights all occurrences

$ ps aux | grep root prints all processes started by root


wget - download

You can download files from the internet using the wget command. This programm is capable of many things like posting data before downloading or saving just the request / response headers without downloading the file.

synopsis

$ wget [options] [url]

examples

$ wget https://www.debian.org/ downloads the index.html of debians's frontpage

$ wget -O logo.png https://www.debian.org/Pics/openlogo-50.png with the -O option, a filename can be set


Other commands

change the password

$ passwd or $ passwd user

who am i?

$ whoami

print the current working directory

$ pwd

zip some files

$ zip [filename.zip] [files]

$ zip test.zip foo.txt bar.txt

$ zip -r home.zip /home/user/ (-r recursively)

delete a folder

$ rmdir folder

install /update a package

$ sudo apt-get install package

$ sudo apt-get update package

run a python file

$ pyhton file.py

run something in the background

$ nohup [command] > /dev/null 2>&1

clone something from github

$ git clone https://github.com/...

print running processes

$ ps aux

$ ps aux | grep user

execute commands right after each other using &&

$ rm -rf /var/www/html/index.html && cp -rp index.php /var/www/html