- navigation tricks with cd, pushd, popd
- some keyboard shortcuts
- using history as turbo
- source vs sh
- running processes in background, nohup
- 50 shades of root - su vs sudo
- how to kill... processes with grace
- how to find files effectively
- some networking commands
- how to pimp your shell
leave +50
echo $((2+2))
Go to your home directory with cd
mkdir workspace
Switch to the newly created directory: cd !$
mkdir -p {dev,ppe,prod}/{src,test}
Create a file in all the directories: touch !$/script
Optionally: brew install tree
tree .
More advanced brace expansion: echo a{A{1,2},B{3,4}}b
Some useful keyboard shortcuts:
ctrl+a
- move to the beginning of the linectrl+l
- clearctrl+c
- SIGINTctrl+d
- EOF
cd dev/src
cd ../..
cd dev/src
Go back to the previous directory with: cd -
pushd dev/src
popd
Modern shells provide some more sophisticated autocomplete mechanisms, so most of commands I am going to show are useless in them.
history
history | grep cd
Run the nth command with !<n>
Rerun the command containing a string with !<string>
, alternatively you can navigate through the history with ctrl+r
.
Reuse the previous command in present command with !!
:
sudo !!
When you call source
(or its alias .
), you insert the script in the current bash process. So you could read variables set by the script.
When you call sh
, you initiate a fork (sub-process) that runs a new session of /bin/sh
. In this case, environment variables set by the sub-script would be dropped when the sub-script finishes.
Init:
echo "sleep 30" > dev/src/script
cat dev/src/script
chmod +x !$
Simply run this script as a background process with: ./!$ &
;)
Return the process to the foreground: fg 1
But what if we for example log out from SSH. Then our process will disappear.
To prevent from this we can use: nohup ./script &
.
Bonus: to print stdout both to terminal and a file use ls -al | tee test
The su
command is used to start a shell as another user.
Login to root with su -l
The -l
may be abbreviated as -
. This means that the user’s environment is loaded and the working directory is changed to the user’s home directory.
The sudo command is like su with some additional capabilities.
The administrator can configure sudo to allow a user to execute commands as a different user (usually root).
Usually, it is controlled by the /etc/sudoers
file.
See what privileges are granted by sudo
with: sudo -l
.
Differences:
sudo
does not require access to the superuser’s password- it does not start a new shell, nor loads another user’s environment
By default, systems like Mac OS X or Ubuntu disable logins to the root account (by failing to set a password for the account).
You cannot do su
.
But you can do sudo su
;)
sudo chown root script
sudo chmod 700 script
./script &
sudo ./script &
sudo chown hslojewski script
./script &
kill <PID>
If no signal is specified on the command line, then the TERM signal is sent.
If a program is still “alive” enough to receive signals and does not ignore the TERM singal, it will terminate.
Otherwise, we have to use the KILL signal (9).
The KILL signal is never actually sent to the target program. Rather, the kernel immediately terminates the process. When a process is terminated in this manner, it is given no opportunity to “clean up” after itself or save its work. For this reason, the KILL signal should be used only as a last resort when other termination signals fail.
How to kill more than one process?
./script &
./script &
./script &
killall sleep
cd workspace
find . -name script
find . -type f
find . -size +1c
find . -type f -size +1c
find . -name scri*
find . -empty
find . -mmin -100
find . -mindepth 3
find . -type f -size +1c -exec cat '{}' ';'
// The Most Useful Find Contest
grep -r sleep .
grep -r '\s\d' .
Check what listens on different ports:
lsof -n | grep LISTEN
Python HTTP server
python -m SimpleHTTPServer 8000 &
python3 -m http.server 8000 &
Check what listens on the port 8000:
lsof -n -i:8000 | grep LISTEN
Check if domain is resolved in DNS:
host -t NS google.com
host -a google.com
Alternatives: nslookup
(not supported), dig
(much more complicated)
Print a summary of the current activity on the system, including what each user is doing:
w
Sniffing packets: sudo tcpdump
Extend the startup file (.bashrc
or .bash_profile
) with:
export CLICOLOR=1
PS1="\[\033[0;33m\][\!]\`if [[ \$? = "0" ]]; then echo "\\[\\033[32m\\]"; else echo "\\[\\033[31m\\]"; fi\`[\u.\h: \`if [[ `pwd|wc -c|tr -d " "` > 18 ]]; then echo "\\W"; else echo "\\w"; fi\`]\$\[\033[0m\] "; echo -ne "\033]0;`hostname -s`:`pwd`\007"
https://gist.github.com/Hoobie/052dc307aadd5677372d71d1ab1a7d10
Source it!
Test it: echo -e "\033[0;32m HELLO WORLD! \033[0;30m"
-e
tells echo to enable backslash escapes.
- Use shebang:
#!/usr/bin/env bash
to tell your shell which program it should be executed by. - Use strict bash with
set -e
since the default strategy is “On Error Resume Next” (sic).