27 Sept 2013

Linux shell tips 2

Linux shell tips 2


Mount a directory (for cases when symlinking will not work):
mount --bind /source /destination
Send dynamic update to DNS server:
nsupdate < <EOF
update add $HOST 86400 A $IP
send
EOF
Recursively grep all directories:
grep -r "some_text" /path/to/dir
List ten largest open files:
lsof / | awk '{ if($7 > 1048576) print $7/1048576 "MB "$9 }' | sort -n -u | tail
Show free RAM in MB:
free -m | grep cache | awk '/[0-9]/{ print $4" MB" }'
Open Vim and jump to end of file:
vim + some_file_name
Git clone specific branch (master):
git clone git@github.com:name/app.git -b master
Git switch to another branch (develop):
git checkout develop
Git delete branch (myfeature):
git branch -d myfeature
Git delete remote branch:
git push origin :branchName
Git push new branch to remote:
git push -u origin mynewfeature
Print out the last cat command from history:
!cat:p
Run your last cat command from history:
!cat
Find all empty subdirectories in /home/user:
find /home/user -maxdepth 1 -type d -empty
Get all from line 50 to 60 in test.txt:
< test.txt sed -n '50,60p'
Run last command (if it was: mkdir /root/test, below will run: sudo mkdir /root/test):
sudo !!
Create temporary RAM filesystem - ramdisk (first create /tmpram directory):
mount -t tmpfs tmpfs /tmpram -o size=512m
Grep whole words:
grep -w "name" test.txt
Append text to a file that requires raised privileges:
echo "some text" | sudo tee -a /path/file
List all supported kill signals:
kill -l
Generate random password (16 characters long in this case):
openssl rand -base64 16
Do not log last session in bash history:
kill -9 $$
Scan network to find open port:
nmap -p 8081 172.20.0.0/16
Set git email:
git config --global user.email "me@example.com"
To sync with master if you have unpublished commits:
git pull --rebase origin master
Move all files with "txt" in name to /home/user:
find -iname "*txt*" -exec mv -v {} /home/user \;
Put the file lines side by side:
paste test.txt test1.txt
Progress bar in shell:
pv data.log
Send the data to Graphite server with netcat:
echo "hosts.sampleHost 10 `date +%s`" | nc 192.168.200.2 3000
Convert tabs to spaces:
expand test.txt > test1.txt
Skip bash history:
< space >cmd
Go to the previous working directory:
cd -
Split large tar.gz archive (100MB each) and put it back:
split b 100m /path/to/large/archive /path/to/output/files
cat files* > archive
Get HTTP status code with curl:
curl -sL -w "%{http_code}\\n" www.example.com -o /dev/null
Set root password and secure MySQL installation:
/usr/bin/mysql_secure_installation
When Ctrl + c not works:
Ctrl + \
Get file owner:
stat -c %U file.txt

No comments:

Post a Comment