Bash Tips
Basics
-
cd
alone goes to the home directory. -
ls -lt
gives the long format and sorted by modification time. -
file foo.txt
gives the file type. -
less foo.txt
gives the content of the file. -
We can double click a filename to copy it.
-
cp
copies the files. -
mv
moves the files or renames the files. -
ln
creates links. -
Use
|
to combine commands. -
Control A goes to the beginning.
-
Control R goes to revere searching in the history.
-
zip -r foo.zip foo
zips the file/directory.unzip foo.zip
unzips. -
grep regex foo.txt
searches in foo.txt .- -i for case-insensitive
- -v for inversion
-
find . -name "foo*"
finds the files with the name foo* . -
exec zsh
andexec bash
switch between the two shells. -
Control N clears the current line.
Writing Scripts
-
Put
#!/bin/bash
at the beginning of the script. -
Use
chmod 755 foo.sh
to make the script executable for everyone. (700 for the owner only.)
Advanced Tricks
-
touch {a..z}{0001..0100}.txt
creates multiple files at once. -
grep -r "hello" .
searches for hello recursively in the current directory. -
wget --no-parent -r https://cnds.jacobs-university.de/courses/os-2019/src/
gets the content recursively. -
find . -name "*html*" -delete
removes all files containing html. -
command &
to run the command in the background.