Linux 命令备忘

经常会用到的一些命令备忘。

1 命令行控制声卡选择,实时切换音频输出设备

https://askubuntu.com/questions/71863/how-to-change-pulseaudio-sink-with-pacmd-set-default-sink-during-playback

1
2
3
4
pacmd list-sinks
pacmd set-default-sink 1
pacmd list-sink-inputs
pacmd move-sink-input 5 1

2 ubuntu 查找端口的进程

port=80; pid=$(sudo netstat -nlp | grep "\:$port " | sed 's/.*LISTEN      \(.*\)\/.*$/\1/'); ps $pid | grep $pid

3 ubuntu 一句话清除不用的固件

dpkg --list | grep linux-image | awk '{ print $2 }' | sort -V | sed -n '/'`uname -r`'/q;p' | xargs sudo apt-get -y purge

ref: https://askubuntu.com/questions/2793/how-do-i-remove-old-kernel-versions-to-clean-up-the-boot-menu

4 使用gimp 将pdf转换为jpg

  1. 在~/.gimp-2.8/scripts 创建pdf-to-jpg.scm
    1
    2
    3
    4
    5
    6
    (define (pdf-to-jpg filename outfile)                                          
    (let* ((image (car (file-pdf-load RUN-NONINTERACTIVE filename filename)))
    (drawable (car (gimp-image-get-active-layer image))))

    (file-jpeg-save RUN-NONINTERACTIVE image drawable outfile outfile
    0.75 0 1 1 "GIMP" 0 1 0 0 )

    (gimp-quit 0)))

  2. 命令行执行:
    gimp -b '(pdf-to-jpg "/path/to/input/pdf" "/path/to/output/jpg")'

5 强大的ssh

https://superuser.com/questions/588591/how-to-make-ssh-tunnel-open-to-public

sudo -u user /usr/bin/autossh -M 5678 -N -R <remote port>:<local ip>:<local port> user@gateway.com -p <ssh port>

6 非root用户帧听1024以下端口

sudo setcap 'cap_net_bind_service=+ep' <path/to/elf/file>

7 监控dhcp数据包

7.1 方法1

sudo tcpdump -i <network-interface> port 67 or port 68 -e -n

7.2 方法2

sudo dhcpdump -i <network-interface>

8 用Raid1实现系统热备份

https://serverfault.com/questions/481678/using-raid1-to-assist-in-offsite-backups

I have successfully accomplished offsite backups using raid 1. I suggest you use mdadm under linux for this process. mdadm is idea because no matter how many years down the road you will always be able to mount and retrieve your data under linux. Build a raid1 set just like you said. Each month mark the drives (use a sticker on the physical drive) and the date that it was inserted or removed. This will guarantee that you always pull the older drive. Before pulling the drive you should do these steps

  • Check the status of the array with mdadm –detail /dev/md0
  • force the system to re-verify parity of your data with echo "check" > /sys/block/md0/md/syncaction
  • shutdown the machine, pull the drive, put in the new one and add it back into the set

If you can't have your data go down, you can mount the drive in readonly mode for a period and then hot yank the drive. If you are using sata drives then you can add and remove them while the system is running. Here is a link with more of the specific commands:

http://www.howtoforge.com/replacing_hard_disks_in_a_raid1_array

9 Linuxmint 蓝牙问题

https://forums.linuxmint.com/viewtopic.php?t=231616

1
2
3
4
5
6
7
8
9
Hi this worked for me

root@edison:~# rfkill unblock bluetooth
root@edison:~# bluetoothctl
[bluetooth]# agent KeyboardDisplay
[bluetooth]# default-agent
[bluetooth]# scan on
[bluetooth]# scan off
[bluetooth]# pair <device>

10 git 强制用一个分支覆盖另一个分支

git checkout seotweaks
git merge -s ours master
git checkout master
git merge seotweaks

The result should be your master is now essentially seotweaks.

(-s ours is short for –strategy=ours)

https://stackoverflow.com/questions/2862590/how-to-replace-master-branch-in-git-entirely-from-another-branch

11 org-mode agenda export to shell usage

13 i3 HiDPI 解决方案

13.1 ~/.Xresources

Xft.dpi: 150
Xft.autohint: 0
Xft.lcdfilter:  lcddefault
Xft.hintstyle:  hintfull
Xft.hinting: 1
Xft.antialias: 1
Xft.rgba: rgb

13.2 ~/.xinitrc

xrdb -merge ~/.Xresources

https://wiki.archlinux.org/index.php/HiDPI

14 i3 运行peek录制gif

先运行 compton, peek才能创建透明窗口

15 设备间复制文件

接收端

netcat -l -p 10000 > desktop.jpg

发送端

nc A.B.C.D 10000 < desktop.jpg

https://unix.stackexchange.com/questions/171456/download-n-upload-files-via-telnet-session