class: center, middle # Linux Toolbox
--- # We will see ...a short **Bash** introduction, to get the necessary intuition for what most tools in this course do. ...how most classic Linux tools **compose**. ...a wide spectrum of available **tools** and what you can do with them. --- # Goals Reduce unknown unknowns (`what's available?`, `what do I search for?`, etc). Give a rough intuition for what classic Linux tools can do. Provide pointers for where to learn the tools. Learn vocabulary. Non-goal: teach you how to use the tools - I would love to, but we don't have time! --- ```bash file=$(ls -tp1 ~/Downloads | grep -v "/$" | head -1) dirs='Images,Videos,Documents,Documents/books,DELETE,OPEN' choice=$(echo $dirs | rofi -dmenu -sep ',' -p "move '$file' where? ") [[ "$choice" = "" ]] && exit [[ "$choice" = DELETE ]] && rm ~/Downloads/"$file" && exit [[ "$choice" = OPEN ]] && xdg-open ~/Downloads/"$file" && exit mv ~/Downloads/"$file" "$choice" ``` safe the above in `/home/nils/filer.sh` and add a few lines of text in `~/.xbindkeysrc`: ``` "bash /home/nils/filer.sh" control+shift+f ``` --- # How to use this talk - pick and choose the tools that seem interesting to you and try to learn them - come back to the slides and use them as a glossary for unknown words - ...or just be amused with what people come up with --- # Some philosophy Many of the classical Linux tools adhere to the "UNIX principle":
Do one thing and do it well.
In a wider, non-programming sense: It's useful to learn things that are small, orthogonal, and compose. --- # Bash Related words: terminal, terminal emulator, shell, command line, ...
In a nutshell: Bash is a tool that let's you interact with your computer via text. **Try it out:** Open a terminal (just search for `Terminal` in your launcher) --- # Bash Commands Bash let's you interact with your computer by typing commands. There are **many** commands. Some examples: `ls`, `pwd`, `echo`, `cd`, `mv`, `sleep`, `alias`, ... Some commands are built into Bash, others can be downloaded. Some commands are really just programs: try typing `firefox` and hitting Enter if you have it installed. --- # Filesystem basics List files and show where we are: ```bash ls pwd ``` Enter or leave directories: ```bash cd my_folder cd .. cd /home/nils/Desktop cd ~/Desktop ``` --- # Bash terminology **terminal** is the window in which bash runs. bash is a kind of **shell**. there are others!
--- # Bash For Programming Bash can even be used as a (terrible) programming language, using commands like `for`, `if`, `while`, `[[` etc. It also makes use of special constructs that aren't commands (e.g. arithmetic expansion: `echo $((5+3))`) We'll only bother with `|` and `>` ("piping" and "redirecting"). --- # | and > Two incredibly useful Bash features! Both redirect command output: - `|` redirects to a new command ("piping") - `>` redirects into a file ("file redirection" or "redirection" for short)
--- # bash summary bash is a different way of interacting with your system bash runs in your terminal. bash is a shell. there are others: Zsh, fish, Dash, csh resources: - our bash guide (https://thealternative.ch/guides/bash.php) - Lhunath and GreyCat's bash guide (http://mywiki.wooledge.org/FullBashGuide) --- # bash power Move all `.png` files from Downloads to Images: ```bash mv ~/Downloads/*.png ~/Images` ``` go to our images, make a new directory, and move newest five images there: ```bash cd ~/Images mkdir newImages ls -1t *.png | head -5 | xargs -I {} mv {} newImages ``` rename all images in a folder to `*_with_family`: ```bash for file in *.png; do mv "$file" "$file_with_family" done ``` --- class: center, middle # Tool Galore We will now talk about multiple tools and how to combine them using bash (mostly). --- # Scripting Languages bash is not ideal as a programming language.for more complex jobs,a real programming language is needed. two main choices: - Python (recommended for beginners) - Ruby (only if you already know programming; compact syntax) these fill niches that bash doesn't, but are still close to the system.
--- # Notifications typically linux has a notification demon integrated. ```bash notify-send "Hello" ``` this is often useful for things that you want to run in the background. Example: Timer ```bash sleep 300; notify-send "Time is up!" ``` --- # Rofi can be used to display a list of things and then let's us choose one of them. ```bash echo "one\ntwo\nthree" | rofi -dmenu ``` on its own is not useful, but can serve as a powerful launcher when combined with other things: ```bash ls -1 ~/Documents/books/*.pdf | rofi -dmenu | xargs -I {} evince {} ``` https://github.com/davatorium/rofi --- # xbindkeys A **keydeamon**! Let's us configure keyboard shortcuts. in file `~/.xbindkeysrc`: ```bash "notify-send test" control+t "ls -1 ~/Documents/books/*.pdf | rofi -dmenu | xargs -I {} evince {}" control+Mod1+b ```
--- ```bash file=$(ls -tp1 ~/Downloads | grep -v "/$" | head -1) dirs='Images,Videos,Documents,Documents/books,DELETE,OPEN' choice=$(echo $dirs | rofi -dmenu -sep ',' -p "move '$file' where? ") [[ "$choice" = "" ]] && exit [[ "$choice" = DELETE ]] && rm ~/Downloads/"$file" && exit [[ "$choice" = OPEN ]] && xdg-open ~/Downloads/"$file" && exit mv ~/Downloads/"$file" "$choice" ``` safe the above in `/home/nils/filer.sh` and add a few lines of text in `~/.xbindkeysrc`: ``` "bash /home/nils/filer.sh" control+shift+f ``` --- # grep a command to look for **regular expressions**. think of regular expressions as being text patterns on steroids: `grep "^[0-9]" data.csv` prints all the lines starting with a number we can try a multiple greps together to chain filters:
--- # sed **s**tream **ed**itor this tool can search for regular expressions like we did with `grep`, and perform various operations on each output line separately --- # find a general purpose tool for finding various files according to search filters. especially nice if used with xargs-style option or regular expressions: Remove all files (`-type f`) that end in .php (`*.php`): ```bash find . -type f -name "*.php" -exec rm {} \; ``` Display all files that contain a comment that contains `TODO`: ```bash find ~/Documents -type f -exec grep -l "//.*TODO" {} \; ``` --- # file conversion sometimes you want to convert between similar file formats. there are a lot of tools for this. most are specific for some kind of file content.here are some common ones: - `ffmpeg` for audio and video - `pandoc` for many hierarchical text formats (mark down, html, latex) - `pandoc` for image files
Using a bash for-loop, convert all `wav`-files to `mp3`: ```bash for file in *.wav; do ffmpeg -i "$file" "${file%.*}.mp3" done ``` (what `ffmpeg` sees: `ffmpeg -i song.wav song.mp3`) --- # wget mirror webpage ```bash wget --mirror --convert-links --page-requisites \ --no-parent -e robots=off https://thealternative.ch ``` --- # git Git is a version control system: Keep track of who changed what, when and why, and revert changes easily. ```bash git init git add * git commit -m "Initial commit" git remote add git@gitlab.ethz.com/thealternative/courses.git git push -u origin --all ``` *Visit the git course next week!* --- # ssh `Euler` is an ETH supercomputer: ```bash ssh nilsl@euler.ethz.ch ``` Connect with SSH and execute commands in the console of the server. --- # tmux Execute long-running processes in the background while your terminal is closed. Especially useful in combination with SSH. ```bash tmux new -s new_session tmux attach new_session ``` --- # xdotools Automate key presses & mouse movements. ``` xdotool key ctrl+c key alt+Tab key ctrl+v xdotool mousemove 20 100 click 1 sleep 2 mosemove 100 20 ``` --- # Summary
Categories: - downloading (like wget, ssh, rsync) - text processing (like vim, sed, bash) - file conversion (like ffmpeg, pandoc, convert) - searching (like sed, grep, find) - glue (like xdotools, rofi, xbindkeys, notify-send, python, bash) --- # Longlist of interesting tools **stow** lets you manage config files easily. **ranger** is a console-based file manager. **i3** is an alternative window-manager. **borg** is a backup tool. **nano** is an easy to use console text-editor. **rsync** to copy files between machines. --- # Longlist of interesting tools with not helpful descriptions **ranger** is a cringe file manager. **i3** is gnome for vegans. **rm -rf /** saves storage space.