Skip to content

Instantly share code, notes, and snippets.

@akottr
Last active June 19, 2016 18:04
Show Gist options
  • Save akottr/5375188 to your computer and use it in GitHub Desktop.
Save akottr/5375188 to your computer and use it in GitHub Desktop.

Linux commands

(that I can not remeber)

Find in files recursive

grep -Hir "some text" *

Show disk usage

du -shc *
# including hidden files
du -shc .[!.]* *

Find in file and higlight

grep --color=auto "some text" myFile.log

Find in file and show only file names

grep -lr "some text" *

Find file and delete

find . \( -name "\.classpath" -o -name "\.project" \) -exec rm -rf '{}' \;

find . -maxdepth 5 -type d \( -name "target" -o -name ".settings" \) -exec rm -rf '{}' \;

set timezone

sudo dpkg-reconfigure tzdata

Map Process to port

netstat -antupl | grep 8080
lsof -i TCP:8080

Run without alias

# use a backslash as first character. e.g \ls
\ls

Show bash file without comments

egrep -v "^#|^$" httpd.conf

bulk rename

rename 's/^unwanted//' *.jpg

create ssl key + cert

http://www.akadia.com/services/ssh_test_certificate.html

#key
openssl genrsa -des3 -out server.key 1024
# csr
openssl req -new -key server.key -out server.csr
# remove password
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key
# generate cert
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

Single history for all terminals

vim ~/.bashrc
export HISTCONTROL=ignoredups:erasedups  # no duplicate entries
export HISTSIZE=100000                   # big big history
export HISTFILESIZE=100000               # big big history
shopt -s histappend                      # append to history, don't overwrite it

# Save and reload the history after each command finishes
export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"

Show git branch in terminal

parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/[\1] /'
}
export PS1="\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \[\033[01;31m\]\$(parse_git_branch)\[\033[00m\]$\[\033[00m\] "

Map PgUp PgDown to history (after typing in)

Somehow Ctrl+arrowleft / Ctrl+arrowright break after doing that. So the last two lines are for fixing that.

vim ~/.inputrc
# for arrow up down
"\e[A": history-search-backward
"\e[B": history-search-forward
# or instead pgup pgdn
#"\e[5~": history-search-backward
#"\e[6~": history-search-forward
# fix ctrl left/right
"\e[1;5C": forward-word   # ctrl + right
"\e[1;5D": backward-word  # ctrl + left

@see http://stackoverflow.com/questions/1030182/how-do-i-change-bash-history-completion-to-complete-whats-already-on-the-line

To reload ~/.inputrc

bind -f ~/.inputrc

change hwaddress (mac address)

# view mac address
ifconfig -a | grep HWaddr

# change mac address
ifconfig wlan0 down
ifconfig wlan0 hw ether 00:80:48:BA:d1:30
ifconfig wlan0 up
ifconfig wlan0 |grep HWaddr

scan network

nmap -v 192.168.0.*

install kernel

dpkg -l | grep linux-image-.*-generic
sudo apt-get install --reinstall linux-image-3.X.Y-ZZ-generic

create swap

fallocate -l 4G /swapfile && \
chmod 600 /swapfile && \
mkswap /swapfile && \
swapon /swapfile && \
echo "/swapfile   none    swap    sw    0   0" >> /etc/fstab

other stuff

# Securely forward TCP connections on local port 1234 to remote2 port 4321
ssh -L 1234:http://remote2.example.com:4321  username@remote.example.com

# Show which services are set to start on Red Hat based system at any runlevel.
chkconfig --list | grep :on

Mail commands (that I can not remeber)

read mails in console (gmx pop3)

openssl s_client -ssl3 -starttls pop3 -crlf -connect pop.gmx.net:110

user myUserName
pass myPass

stat
list
top 1 0   # first mail, only header
top 1 10  # first mail, header + first 10 lines
retr 1
dele 1
quit

Vim commands (that I can not remeber)

http://vim.wikia.com/wiki/Best_Vim_Tips

copy & paste

start selection

v

select complete line

V

delete until end of document

dG

go to next occurence of character in line

f
example:
'fx' goes to next 'x'
';' to go forward
',' to go backward

go to next occurence of word

*
example:
'*' and cursor is on the word 'foo', cursor will jump to next occurrence of 'foo'
go to next occurence of character in line

Substitute

:%s/foo/bar/g
Find each occurrence of 'foo' (in all lines), and replace it with 'bar'.

:s/foo/bar/g
Find each occurrence of 'foo' (in the current line only), and replace it with 'bar'.

:%s/foo/bar/gc
Change each 'foo' (case insensitive) to 'bar'; ask for confirmation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment