Last active
February 16, 2024 12:23
-
-
Save mcxiaoke/79350c572a3c7a7a57b0c1c1857a2a04 to your computer and use it in GitHub Desktop.
Linux and Macos Tips
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Disable suspend and hibernation | |
systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target | |
# How to run commands at shutdown on Linux | |
[Unit] | |
Description=Run mycommand at shutdown | |
Requires=network.target | |
DefaultDependencies=no | |
Before=shutdown.target reboot.target | |
[Service] | |
Type=oneshot | |
RemainAfterExit=true | |
ExecStart=/bin/true | |
ExecStop=mycommand | |
[Install] | |
WantedBy=multi-user.target | |
#Execute script after successful network connection | |
[Unit] | |
Description=YOUR DESCRIPTION | |
Requires=network-online.target | |
After=network-online.target | |
[Service] | |
Type=oneshot | |
RemainAfterExit=yes | |
ExecStart=/YOUR/PATH/TO/SCRIPT | |
[Install] | |
WantedBy=multi-user.target | |
# fix Kernel Panic - not syncing: VFS: Unable to mount root fs on unknown | |
You are missing the initramfs for that kernel. | |
Choose another kernel from the GRUB menu under Advanced options for Ubuntu | |
and run sudo update-initramfs -u -k version to generate the initrd for version | |
(replace version with the kernel version string such as 4.15.0-36-generic) | |
then sudo update-grub | |
# find and move files | |
find . -type f -exec mv {} /home/jack \; | |
find . -type f | xargs -i '{}' mv {} /home/jack | |
# gathering all json info from smartctrl status | |
for arg in sd[a-e]; do /usr/sbin/smartctl --info --all --json --nocheck standby /dev/$arg > /var/log/smartctl/test-results-$arg.json; done | |
# check all hard disk smartctl error status info | |
for sd in /dev/sd[a-e]; do echo "Device Name: $sd"; smartctl -a $sd | grep Model; smartctl -a $sd | grep Serial; smartctl -A $sd | head -7 | tail -1; smartctl -A $sd | grep Error_Rate | grep -v Multi | grep -v G-Sense; smartctl -A $sd | grep Sector; echo ; done | |
# detect filesystem name label and type | |
apt install fsarchiver | |
fsarchiver probe -v | |
# make Tab auto-completion case-insensitive in Bash? | |
# add option to /etc/inputrc to enable case-insensitive tab completion for all users | |
echo 'set completion-ignore-case On' >> /etc/inputrc | |
# you may have to use this instead if you are not a superuser: | |
echo 'set completion-ignore-case On' | sudo tee -a /etc/inputrc | |
# config and switch python version alternative | |
# Ubuntu 20.04 Python version switch manager | |
sudo update-alternatives --list python | |
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 1 | |
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 2 | |
sudo update-alternatives --list python | |
sudo update-alternatives --config python | |
sudo update-alternatives --config python3 | |
python -V | |
# How to Add User to Sudoers in Ubuntu | |
su - | |
usermod -aG sudo username | |
sudo whoami | |
# A one-liner to convert all deprecated keys to the new format. | |
# https://askubuntu.com/questions/1407632/key-is-stored-in-legacy-trusted-gpg-keyring-etc-apt-trusted-gpg | |
for KEY in $( \ | |
apt-key list \ | |
| grep -E "(([ ]{1,2}(([0-9A-F]{4}))){10})" \ | |
| tr -d " " \ | |
| grep -E "([0-9A-F]){8}\b" \ | |
); do | |
K=${KEY:(-8)} | |
apt-key export $K \ | |
| sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/imported-from-trusted-gpg-$K.gpg | |
done | |
#Display git branch name | |
Add following lines to your ~/.bash_profile | |
parse_git_branch() { | |
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/' | |
} | |
export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ " | |
# adb disable doze mode | |
# Disable doze mode with adb, it fixed all my problems with notifications delay. | |
adb shell dumpsys deviceidle disable | |
# adb force stop appilication | |
# | |
adb shell am force-stop com.your.app.package | |
# force stop phone manager on MIUI to change Launcher | |
adb shell am force-stop com.miui.securitycenter | |
# Run git pull over all subdirectories | |
find . -maxdepth 1 -type d -print -execdir git --git-dir={}/.git --work-tree=$PWD/{} pull origin master \; | |
# wireguard install | |
apt update | |
apt install wireguard | |
# use custom config file | |
cp wg0.conf /etc/wireguard/wg0.conf | |
systemctl start wg-quick@wg0 | |
systemctl status wg-quick@wg0 | |
systemctl enable wg-quick@wg0 | |
#xiaomi miui backup extrac | |
#重点是跳过前面41个字节,MIUI加的文件头要去掉 | |
dd if=微信\(com.tencent.mm\).bak of=backup.db bs=512M skip=41 iflag=skip_bytes | |
java -jar abe-all.jar unpack backup.db backup.tar | |
# rename file name extension using bash | |
for f in *.txt; do mv -- "$f" "${f%.txt}.text"; done | |
find . -type f -name "*.txt" -exec rename 's/\.txt$/.newext/' '{}' \; | |
# How do I undo the most recent local commits in Git | |
# https://stackoverflow.com/questions/927358 | |
git commit -m "Something terribly misguided" # (0: Your Accident) | |
git reset HEAD~ # (1) | |
# [ edit files as necessary ] # (2) | |
git add . # (3) | |
git commit -c ORIG_HEAD # (4) | |
# Change the Python3 default version in Ubuntu | |
# https://unix.stackexchange.com/questions/410579 | |
# install python3.9 for ubuntu 16.04 | |
sudo apt install software-properties-common | |
sudo add-apt-repository ppa:deadsnakes/ppa | |
sudo apt update | |
sudo apt install python3.9 python3.9-venv python3.9-dev | |
# config as default | |
sudo update-alternatives --config python3 | |
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1 | |
sudo update-alternatives --config python3 | |
# ffmpeg split by start/end/duration | |
# https://trac.ffmpeg.org/wiki/Seeking#Cuttingsmallsections | |
# https://stackoverflow.com/questions/18444194/ | |
ffmpeg -ss 00:01:00 -i input.mp4 -to 00:02:00 -c copy output.mp4 | |
# Explanation of the command: | |
# -i: This specifies the input file. In that case, it is (input.mp4). | |
# -ss: Used with -i, this seeks in the input file (input.mp4) to position. | |
# 00:01:00: This is the time your trimmed video will start with. | |
# -to: This specifies duration from start (00:01:40) to end (00:02:12). | |
# 00:02:00: This is the time your trimmed video will end with. | |
# -c copy: This is an option to trim via stream copy. (NB: Very fast) | |
# The timing format is: hh:mm:ss.SS | |
# How can I keep my SSH sessions from freezing | |
# https://unix.stackexchange.com/questions/200239 | |
# client | |
/etc/ssh/ssh_config | |
Host * | |
ServerAliveInterval 100 | |
# server | |
/etc/ssh/sshd_config | |
ClientAliveInterval 60 | |
TCPKeepAlive yes | |
ClientAliveCountMax 10000 | |
## ffmpeg adjust video and audio speed | |
# sync the audio speed with the video speed 2x | |
ffmpeg -i input.mp4 -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]" output.mp4 | |
# mount windows share samba on ubuntu/debian | |
# https://linuxize.com/post/how-to-mount-cifs-windows-share-on-linux/ | |
# https://access.redhat.com/solutions/448263 | |
apt install cifs-utils | |
mount -t cifs -o username=<share user>,password=<share password> //WIN_PC_IP/<share name> /mnt | |
# How to check if a string contains a substring in Bash | |
# https://stackoverflow.com/questions/229551 | |
string='My string'; | |
if [[ $string =~ "My" ]]; then | |
echo "It's there!" | |
fi | |
# ExifTool Tips | |
# Move Photos/Pictures to folders by Date | |
# See https://ninedegreesbelow.com/photography/exiftool-commands.html#move | |
# https://gist.github.com/rjames86/33b9af12548adf091a26 | |
exiftool '-Directory<CreateDate' -d /media/dest_folder/%y/%y%m%d -r /media/source_folder | |
# ubuntu/debian disable unattended-upgrades AutomaticSecurityUpdates | |
# Choose No and hit ENTER to disable unattended upgrades. | |
sudo dpkg-reconfigure unattended-upgrades | |
# it is safe to remove unattended upgrades package. | |
sudo apt remove unattended-upgrades | |
# make ubuntu network route prefer ethernet over wifi if both present | |
#https://superuser.com/questions/501406 | |
route -n | |
apt install ifmetric | |
ifmetric wlan0 200 | |
# mount NTFS writable | |
sudo umount /Volumes/UNTITLED | |
sudo mount -t ntfs -o rw,auto,nobrowse /dev/disk3s1 ~/ntfs-volume | |
# make Tab auto-completion case-insensitive in shell | |
echo 'set completion-ignore-case On' >> ~/.inputrc | |
# or /etc/inputrc | |
echo 'set completion-ignore-case On' >> /etc/inputrc | |
#ubuntu change timezone | |
sudo dpkg-reconfigure tzdata | |
# convert raw to jpg on macos | |
for i in *.NEF; do sips -s format jpeg $i --out "${i%.*}.jpg"; done | |
# file counter | |
find . -type f | wc -l | |
# Recursively Delete .DS_Store Files | |
find . -type f -name '.DS_Store' -ls -delete | |
# find and move files | |
find . -iname "*.mp4" -exec mv -t ../Videos/ {} \; | |
# Show Application Using a Certain Port | |
sudo lsof -i :80 | |
# Enable (Default) | |
sudo spctl --master-enable | |
# Disable | |
sudo spctl --master-disable | |
#So, to process all compatible files and sub-folders within the top level folder called Music: | |
exiftool -r "-FileName<CreateDate" -d "%Y-%m-%d_%H-%M-%S - %%f.%%e" "~/Music" | |
#To process only .mp3 files and sub folders within the top level folder called Music: | |
exiftool -r "-FileName<CreateDate" -d "%Y-%m-%d_%H-%M-%S - %%f.%%e" "~/Music" -ext .mp3 | |
#To process only .mp3 files and sub folders within the top level folder called Music, while excluding a sub directory called Other: | |
exiftool -r "-FileName<CreateDate" -d "%Y-%m-%d_%H-%M-%S - %%f.%%e" "~/Music" -ext .mp3 -i "~/Music/Other" | |
# from https://exiftool.org/forum/index.php?topic=8552.0 | |
# rename video using metadata created date and time | |
exiftool "-FileName<CreateDate" -d "VID_%Y%m%d_%H%M%S.%%e" | |
# add extension to filename | |
# https://stackoverflow.com/questions/6114004/add-file-extension-to-files-with-bash | |
for f in *; do mv "$f" "$f.jpg"; done | |
# fix macos app install broken | |
sudo xattr -rd com.apple.quarantine /Applications/YourApp.app | |
# or use | |
xattr -c /Applications/YourApp.app | |
# or use, sign your app | |
sudo codesign --force --deep --sign - /Applications/YourApp.app | |
# sed to modify text file on macos | |
sudo sed -i "" "s|#Banner none|Banner /etc/sshd_banner|" /etc/sshd_config | |
# sed to add prefix | |
# If you want to edit the file in-place | |
sed -i -e 's/^/prefix/' file | |
# If you want to create a new file | |
sed -e 's/^/prefix/' file > file.new | |
# add extension for no extension file | |
find . -type f ! -name "*.*" -exec mv {} {}.mp4 \; | |
# rename using shell and bash | |
# rename small part of multiple files using bash | |
brew install rename # for macos | |
rename 's/ABC/XYZ/' *.dat # support regex | |
# example rename regex | |
# Friends - 6x03 - Tow Ross' Denial.srt => S06E03.srt | |
rename -n 's/(\w+) - (\d{1})x(\d{2}).*$/S0$2E$3\.srt/' *.srt | |
# delete folder with large amount files and subfolders | |
# Using rsync is surprising fast and simple. | |
mkdir empty_dir | |
rsync -a --delete empty_dir/ yourdirectory/ | |
# or using perl | |
cd yourdirectory | |
perl -e 'for(<*>){((stat)[9]<(unlink))}' | |
# How to pipe the results of 'find' to mv in Linux | |
# https://stackoverflow.com/questions/22388480/how-to-pipe-the-results-of-find-to-mv-in-linux | |
find ./ -name '*article*' -exec mv {} ../backup \; | |
find ./ -name '*article*' | xargs -I '{}' mv {} ../backup | |
# geo tagging photo exif from gpx file using exiftool | |
# https://exiftool.org/geotag.html | |
exiftool -geotag "c:\gps logs\track.log" c:\images | |
exiftool -geotag a.log -geotag b.log -r dir | |
exiftool -geotag "logs/*.log" dir | |
# exiftool rename file by meta Title tag | |
exiftool '-filename<${title;}.%e' . | |
# find and convert all mp3 files to m4a aac audio files | |
find ./ -name '*.mp3' -exec bash -c 'ffmpeg -i "$0" -vn -c:a libfdk_aac "output/${0/mp3/m4a}"' {} \; | |
# cutting video using ffmpeg faster way | |
# https://trac.ffmpeg.org/wiki/Seeking#Cuttingsmallsections | |
# -i: This specifies the input file. In that case, it is (input.mp4). | |
# -ss: Used with -i, this seeks in the input file (input.mp4) to position. | |
# 00:01:00: This is the time your trimmed video will start with. | |
# -to: This specifies duration from start (00:01:40) to end (00:02:12). | |
# 00:02:00: This is the time your trimmed video will end with. | |
# -c copy: This is an option to trim via stream copy. (NB: Very fast) | |
ffmpeg -ss 00:01:00 -i input.mp4 -to 00:02:00 -c copy output.mp4 | |
# linux/armbian connect wifi command line | |
iw dev wlan0 scan | |
iw dev wlan0 link | |
iw wlan0 connect network_name | |
# using netcat to test network | |
# connect ip port | |
nc -v www.microsoft.com 80 | |
# test port is open | |
nc -vz www.microsoft.com 80 # for tcp | |
nc -vz -u 8.8.8.8 53 # for udp | |
# port scan | |
nc -vz 192.168.1.110 1-1000 | |
# disable ipv6 on ubuntu/debian/linux | |
sudo nano /etc/sysctl.d/99-sysctl.conf | |
# add following lines | |
net.ipv6.conf.all.disable_ipv6 = 1 | |
net.ipv6.conf.default.disable_ipv6 = 1 | |
net.ipv6.conf.lo.disable_ipv6 = 1 | |
# commit changes | |
sudo sysctl -p | |
# disable ipv6 for apt | |
sudo nano /etc/apt/apt.conf.d/99force-ipv4 | |
# add line: Acquire::ForceIPv4 "true"; | |
# python find local ip address | |
import socket | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
# s.connect(("114.114.114.114", 80)) | |
s.connect(("8.8.8.8", 80)) | |
print(s.getsockname()[0]) | |
s.close() | |
# or more rubost | |
# https://stackoverflow.com/questions/166506/finding-local-ip-addresses-using-pythons-stdlib | |
import socket | |
def get_ip(): | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
try: | |
# doesn't even have to be reachable | |
s.connect(('10.255.255.255', 1)) | |
IP = s.getsockname()[0] | |
except Exception: | |
IP = '127.0.0.1' | |
finally: | |
s.close() | |
return IP | |
# github search api | |
curl -G -u userid:token -H "Accept: application/vnd.github.v3+json" --data-urlencode "sort=stars" --data-urlencode "per_page=100" --data-urlencode "page=1" --data-urlencode "q=stars:>1000 language:Kotlin" "https://api.github.com/search/repositories" | |
# Mac OS X temporary files are stored in /tmp and /private/var/folders. Your specific Cache and Temporary folders can be found by running the command and/or | |
echo $TMPDIR | |
usr/bin/getconf DARWIN_USER_CACHE_DIR | |
# How to split an image vertically using the command line? | |
# ImageMagick | |
convert -crop 100%x20% +repage image.png image.png | |
# replace or add extension to all files | |
for i in *; do mv $i ${i%.*}.pdf; done | |
# How to find out the processor model on Mac and Macbooks? | |
# best way | |
sysctl -n machdep.cpu.brand_string | |
# another way | |
system_profiler SPHardwareDataType | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment