Anda di halaman 1dari 10

125- Linux shell tips and tricks

1- SSH debug mode


ssh -vvv user@ip_address

2- SSH with .pem key


ssh user@ip_address -i key.pem

3- Get complete directory listing to local directory with wget


wget -r --no-parent --reject "index.html*" http://hostname/ -P /home/user/dirs

4- Create multiple directories


mkdir -p /home/user/{test,test1,test2}

5- List processes tree with child processes


ps axwef

6- Suspend process
Ctrl + z

7- Move process to foreground


fg

8- Generate random hex number where n is number of characters


openssl rand -hex n

9- Execute commands from a file in the current shell


source /home/user/file.name

10- Substring for first 5 characters


${variable:0:5}

11- Quickly create a backup of a file


cp some_file_name{,.bkp}

12- Access Windows share


smbclient -U "DOMAIN\user" //dc.domain.com/share/test/dir

13- Run command from history (here at line 100)


!100

14- unzip to directory


unzip package_name.zip -d dir_name

15- cat multiline text (CTRL + d to exit)


cat > test.txt

16- Create empty file or empty existing one


> test.txt

17- Update date from Ubuntu NTP server


ntpdate ntp.ubuntu.com

18- Make .war file


jar -cvf name.war file

19- Test disk write speed


dd if=/dev/zero of=/tmp/output.img bs=8k count=128k conv=fdatasync; rm -rf
/tmp/output.img

20- Test disk read speed


hdparm -Tt /dev/sda

21- Get md5 hash from text


echo -n "text" | md5sum

22- Check .xml syntax


xmllint --noout file.xml

23- Extract tar.gz in new directory


tar zxvf package.tar.gz -C new_dir

24- Get http headers with curl


curl -I http://www.example.com

25- Modify timestamp of some file or directory (YYMMDDhhmm)


touch -t 0712250000 file

26- Download from ftp using wget


wget -m ftp://username:password@hostname

27- Generate random password (16 char long in this case)


LANG=c < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c$"{1:-16}";echo;

28- netstat show all tcp4 listening ports


netstat -lnt4 | awk '{print $4}' | cut -f2 -d: | grep -o '[0-9]*'
29- Convert image from qcow2 to raw
qemu-img convert -f qcow2 -O raw precise-server-cloudimg-amd64-disk1.img precise-server-
cloudimg-amd64-disk1.raw

30- Run command repeatedly, displaying it's output (default every two seconds)
watch ps -ef

31- List all users


getent passwd

32- Mount root in read/write mode


mount -o remount,rw /

33- Mount a directory (for cases when symlinking will not work)
mount --bind /source /destination

34- Send dynamic update to DNS server


nsupdate <<EOF
update add $HOST 86400 A $IP
send
EOF

35- List all supported kill signals


kill -l

36- Generate random password (16 characters long in this case)


openssl rand -base64 16

37- Do not log your last session in bash history


kill -9 $$

38- Scan network to find open port


nmap -p 8081 172.20.0.0/16

39- Move all files with "txt" in name to /home/user


find -iname "*txt*" -exec mv -v {} /home/user \;

40- Put file lines side by side


paste test.txt test1.txt

41- Progress bar in shell


pv data.log
41- Send data to Graphite server with netcat
echo "hosts.sampleHost 10 `date +%s`" | nc 192.168.200.2 3000

43- Test iops


ioping -RLD /dev/vda

44- Recursively grep all directories


grep -r "some_text" /path/to/dir

45- List ten largest open files


lsof / | awk '{ if($7 > 1048576) print $7/1048576 "MB "$9 }' | sort -n -u | tail

46- Show free RAM in MB


free -m | grep cache | awk '/[0-9]/{ print $4" MB" }'

47- Open Vim and jump to end of file


vim + some_file_name

48- Print out the last cat command from history


!cat:p

49- Run your last cat command from history


!cat

50- Find all empty subdirectories in /home/user


find /home/user -maxdepth 1 -type d -empty

51- Get all from line 50 to 60 in test.txt


< test.txt sed -n '50,60p'

52- Run last command (if it was: mkdir /root/test, below will run: sudo mkdir /root/test)
sudo !!

53- Create temporary RAM filesystem - ramdisk (first create /tmpram directory)
mount -t tmpfs tmpfs /tmpram -o size=512m

54- Grep whole words


grep -w "name" test.txt

55- Append text to a file that requires raised privileges


echo "some text" | sudo tee -a /path/file

56- Convert tabs to spaces


expand test.txt > test1.txt
57- Skip bash history
<space>cmd

58- Go to the previous working directory


cd -

59- 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

60- Get HTTP status code with curl


curl -sL -w "%{http_code}\\n" www.example.com -o /dev/null

61- Set root password and secure MySQL installation


/usr/bin/mysql_secure_installation

62- When Ctrl C does not work


Ctrl \

63- Get file owner


stat -c %U file.txt

64- List block devices


lsblk -f

65- Find files with trailing spaces


find . -type f -exec egrep -l " +$" {} \;

66- Find files with tabs indentation


find . -type f -exec egrep -l $'\t' {} \;

67- Print horizontal line with "="


printf '%100s\n' | tr ' ' =

68- Check if remote port is open with bash


echo >/dev/tcp/8.8.8.8/53 && echo "open"

69- Generate pass and get first 20 characters


echo -n "test" | sha512sum | base64 | cut -c 1-20

70- Using xargs and variables


command | awk '{print $1}' | xargs --verbose -I IFACE ip link set IFACE mtu 9000
71- Set MTU
ip link set eth0 mtu 9000

72- Lines of text in all files in a directory


find deployment -type f -exec wc -l {} \; | awk '{total += $1} END{print total}'

73- Find common path for list of files and directories


cat test.txt | sed -e 's,$,/,;1{h;d;}' -e 'G;s,\(.*/\).*\n\1.*,\1,;h;$!d;s,/$,,'

74-List dir by used space


du -a /var | sort -n -r | head -n 10

75- Create empty partition on whole disk


parted -sa optimal /dev/xvdb mklabel gpt mkpart primary 0% 100%

76- Trim http or https from URL variable with echo


echo ${URL#*//}

77- Find all files starting with dot


find . -name ".[^.]*"

78- Echo as sudo (tee -a to append)


echo "/dev/xvdb1 /data ext4 defaults 0 2" | sudo tee -a /etc/fstab

79- Get ip address for the host


hostname --ip-address

80- Convert mkv audio to ac3


ffmpeg -i infile.mkv -vcodec copy -acodec ac3 -b:a 640k outfile.mkv

81- Check limits for process


cat /proc/<pid>/limits

82- Postgres dump and restore


pg_dump dbname > /tmp/dbname.sql
pg_dump -h localhost -d dbname -U dbuser > /tmp/dbname.sql
psql dbname < /dbname/sar.sql

83- Fio iops test


# Random 4k read:
fio -ioengine=libaio -bs=4k -direct=1 -thread -rw=randread -size=1G -filename=/tmp/fio-testing
-name="test" -iodepth=32 -runtime=200
# Random 4k write:
fio -ioengine=libaio -bs=4k -direct=1 -thread -rw=randwrite -size=1G -filename=/tmp/fio-testing
-name="test" -iodepth=32 -runtime=200

84- Grep replace hostname


grep -rl --include "config.xml" test.example.com . | sudo xargs sed -i
's/test.example.com/test2.example.com/g'

85- To sort IP addresses


sort -t . -k 3,3n -k 4,4n

86- Do not run the same script if running already


flock -n /tmp/test.lock -c "/home/sysadmin/test.sh > test.log"

87- Add ssh key to knwon hosts


ssh-keyscan github.com

88- Get encoding of file


file -I file.txt

89- Change encoding


iconv -f utf8 -t WINDOWS-1250 org_file new_file

90- Delete all files older than 14 days


find /home/archive/mysql -type f -mtime +14 -delete

91- Bandwidth monitor


bwm-ng -I eth0 -d

92- Clear cache


echo 3 > /proc/sys/vm/drop_caches

93- List all users


getent passwd

94- Kill all active screen sessions


screen -X quit

95- Check fragmentation on XFS


xfs_db -c frag -r /dev/sdf1

96- List dirs and sort by timestamp in dirs name


find test/* -type d -exec basename {} \; | sort -r | head -1
ls -1 test/ | sort -r | head -1
97- Check if there is available dirs staring with dir
ls test/dir-* &>/dev/null

98- Convert date to seconds


date -d"2014-02-14T12:30" +%s
date -d"20140214" +%s

99- Pkill bash script


pkill -9 -f "test.sh"

100- Get process usage info by name


ps -C name -o %cpu,%mem,cmd

101- Check which processes are using old libraries


lsof -o / | awk '$4 == "DEL" && $8 == "/usr/lib/test.so.1.0.0" {print $2}'

102- Get logs for a service on a system running systemd (--no-pager to list all logs, and -S since
date)
journalctl -u test.service --no-pager -S 2017-07-04

103- Populate a file from environment variables (template should have environment variable
names)
envsubst < file-template.txt > file-values.txt

104- Remove duplicate lines


awk '!NF || !seen[$0]++' file.txt > new_file.txt

105- Bash find replace tips


${VARIABLE// /$'\n'} # Replace spaces with new line
${VARIABLE/-[a-z]*/} # Get prefix, text before first - (e.g. test-this-is-a-test, result would be
'test')
${VARIABLE#*-} # Get suffix, everything after first - (e.g. test-this-is-a-test, result would be 'this-
is-a-test'

106- Memory usage by processes (with cache)


ps -e -o pid,vsz,comm= | sort -n -k 2

107- Print human readable time format in dmesg log


dmesg -T

108- Virsh send remote command


virsh -c qemu+tcp://testnode.example.com/system

109- List targets in systemd


systemctl --all -t target

110- Check domain records with dig


dig techbar.me +nostats +nocomments +nocmd

111- Jinja2 find replace -%} with %}


grep -rl "\-\%\}" . | xargs sed -i 's/\-\%\}/\%\}/g'

112- Add new line to the end of file if not exists


find files/. -type f | xargs -I FILE sed -i -e '$a\' FILE

113- Resize qcow2 image


qemu-img resize ubuntu-server.qcow2 +10GB
114- Stress test
stress-ng --cpu 16 --io 4 --vm 1 --vm-bytes 1G -v --timeout 60s --metrics-brief

115- Sysbench CPU test


sysbench --num-threads=48 --test=cpu --cpu-max-prime=200000 run

116- Find with regex


find /path -regextype posix-extended -regex '.*img.*lz4'

117- Find pci bus for NVIDIA card


lspci | grep NVIDIA

118- Check bus info


lspci -n -s 05:00

119- Show available pkgs on rhel system


yum --showduplicates list ceph | expand

120- Zap disk


sgdisk --zap /dev/sda

121- Test if jumbo packets are enabled/working with ping


ping -M do -s 8972 <ip_address>

122- Check interface info:


ethtool eth1

123- Force NFS umount


umount -f -l /mnt/nfs

124- Check if SSL crt, key and csr matches


openssl x509 -noout -modulus -in certificate.crt | openssl md5
openssl rsa -noout -modulus -in privateKey.key | openssl md5
openssl req -noout -modulus -in CSR.csr | openssl md5

125- Export SSL private key from jks file


keytool -importkeystore -srckeystore server.jks -destkeystore server.p12 -srcstoretype jks -
deststoretype pkcs12
openssl pkcs12 -in server.p12 -nodes -nocerts -out server.pem
openssl rsa –in server.pem -out server.key

+Ashutosh