Last active
February 24, 2025 07:48
-
-
Save eplt/027c99215e37a3a138c32b19a944b05d to your computer and use it in GitHub Desktop.
macOS Cheatsheet - Commonly used commands for my macOS Setup
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
-- Automate your macOS a bit more, schedule Safari to open your favourite sites regularly at 7am to save a few seconds each morning. | |
# In terminal, type the following command which will allow you to edit current scheduled jobs with nano editor | |
# You might need to grant cron full disk access for this to work. https://osxdaily.com/2020/04/27/fix-cron-permissions-macos-full-disk-access/ | |
env EDITOR=nano crontab -e | |
# Then copy and paste the following lines. Control-X to exit nano. The new jobs will be schedulled. | |
# The samples below are to open Safari at 7am each morning with the specified URL. It should placed them in new tab if that's how your safari is setup. It's good to wake up each morning saving a few seconds loading the sites you read daily. You can also use similar schedule to download files, open apps, etc. To learn more about how to specify the time, check out http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/ | |
$ crontab -r where the -r flag removes current crontab configuration. | |
$ crontab -l where the -l to list crontab configuration | |
https://crontab.guru/ is helpful if you can't remember the schedule format | |
0 7 * * * open -a Safari http://www.bbc.com/news | |
0 7 * * * open -a Safari http://money.cnn.com | |
0 7 * * * open -a Safari https://flipboard.com/section/technology-c5o31di9kg9ea7p6 | |
0 7 * * * open -a Safari https://www.macrumors.com | |
0 7 * * * open -a 'Brave Browser' http://www.bbc.com | |
-- If you want it to open in a new window, you can do it with Chrome/Brave with the following | |
open -na "Google Chrome" --args --new-window "https://www.bbc.com" | |
open -na "Brave Browser" --args --new-window "https://www.bbc.com" | |
-- macOS screenshot files location, stop cluttering your desktop folder | |
-- With current release of macOS, you don't need to do this, just open the screenshot app, select the folder to save to, that will become the default | |
# Make a new "Screenshots" folder inside your home folder to store all your new screenshots. | |
# Then run the following command in Terminal. | |
defaults write com.apple.screencapture location ~/Screenshots; killall SystemUIServer | |
-- Fixing disk issues | |
# Find the disk number with the following command | |
diskutil list | |
# Then use the following command on HFS disk | |
sudo /sbin/fsck_hfs -drfy /dev/disk2s3 | |
-- SSH Keys Ref: https://gist.github.com/grenade/6318301 | |
chown root:root ~/.ssh/id_rsa* | |
chown user:user ~/.ssh/id_rsa* | |
chmod 600 ~/.ssh/id_rsa | |
chmod 644 ~/.ssh/id_rsa.pub | |
-- To merge many .ts videos | |
cd ts_files_directory | |
echo {1..500}.ts | tr " " "\n" > tslist | |
while read line; do cat $line >> combined.ts; done < tslist | |
-- To Install MongoDB with tools using homebrew | |
brew tap mongodb/brew | |
brew install mongodb-community | |
brew install mongodb-database-tools | |
-- To download thousands of URLs using CURL, make a long list of urls.txt | |
cat urls.txt | xargs -0 curl -O | |
-- Sometimes, I want to give a particulr app the full process priority | |
sudo renice -20 $(pgrep FaceTime) | |
-- To merge some AVI files without re-encoding, install mencoder, then | |
brew install mplayer | |
mencoder -oac copy -ovc copy 1.avi 2.avi 3.avi 4.avi 5.avi 6.avi -o final.avi | |
-- To loop through directories | |
for dir in *; do | |
echo $dir; | |
done | |
-- To loop through files in a directory | |
for file in *.avi; do | |
echo $file; | |
done | |
-- To delete all files (including subfolder from .) with .flac extension. | |
find . -name '*.flac' -delete | |
-- To open 2 copies of WeChat | |
nohup /Applications/WeChat.app/Contents/MacOS/WeChat > /dev/null 2>&1 & | |
nohup /Applications/企业微信.app/Contents/MacOS/企业微信 > /dev/null 2>&1 & | |
-- Using osascript to send imessage, please change your full international phone number with country code and the + sign, no, not with the squid game phone number. also, if you really need it, "pip install osascript" | |
osascript -e 'tell application "Messages" to send "Hello World!" to buddy "+1192182919191"' | |
-- Counting how many sub-subfolders are over 1mb in size, change max/mindepth to have different folder levels | |
find . -maxdepth 2 -mindepth 2 -type d -exec du -hs {} \; | grep "M\t" | wc -l | |
-- Copy files with certain extensions (pdf, jpg in this example) to a new folder preserving folder structure | |
rsync -a --include '*/' --include '*.pdf' --include '*.jpg' --exclude '*' source_folder/ destination_folder/ | |
-- To remove node and start again. Lesson: avoid installing node with homebrew | |
-- https://gist.github.com/dotcomputercraft/b7283bd52f4b5389e748 | |
-- Also check https://stackabuse.com/how-to-uninstall-node-js-from-mac-osx/ | |
sudo rm /usr/local/bin/npm | |
sudo rm /usr/local/share/man/man1/node.1 | |
sudo rm /usr/local/lib/dtrace/node.d | |
sudo rm -rf ~/.npm | |
sudo rm -rf ~/.node-gyp | |
sudo rm /opt/local/bin/node | |
sudo rm /opt/local/include/node | |
sudo rm –rf /usr/local/bin/node | |
sudo rm -rf /opt/local/lib/node_modules | |
sudo rm -rf /usr/local/bin/npm | |
sudo rm -rf /usr/local/share/man/man1/node.1 | |
sudo rm -rf /usr/local/lib/dtrace/node.d | |
sudo rm -rf /usr/local/lib/node_modules | |
-- check after the above with "which node" | |
————— | |
To cleanup system extensions. | |
sudo rm -rf /Library/Extensions/<kextname.kext> | |
sudo kextcache -i / | |
sudo kextcache --clear-staging | |
sudo reboot | |
————— | |
To convert all SVG to PNG of fixed 1200 height | |
brew install librsvg | |
find . -type f -name "*.svg" -exec bash -c 'rsvg-convert -h 1200 "$0" > "$0".png' {} \; | |
————— | |
Too lazy to start another gist for now, just jotting down my frequently used regex patterns. | |
-- To strip HTML tags | |
\<[^\>]*\> | |
-- To merge a folder of mp4s, same quality, with ffmpeg (or brew install ffmpeg) | |
rm list.txt; for f in *.mp4 ; do echo file \'$f\' >> list.txt; done && sort -V list.txt > list-sort.txt && ffmpeg -f concat -safe 0 -i list-sort.txt -c copy stitched-video.mp4 ; rm list.txt list-sort.txt | |
-- To use socks in terminal | |
export http_proxy="127.0.0.1:7890" | |
export https_proxy="127.0.0.1:7890" | |
-- For Apple Silicon Macs | |
-- If you get "found architecture 'arm64', required architecture 'x86_64'" error | |
Recompile the library from source using x86_64 architecture | |
Add following line to CMakeLists.txt before running the CMake command | |
set(CMAKE_OSX_ARCHITECTURES "x86_64" CACHE INTERNAL "" FORCE) | |
-- Ref: https://stackoverflow.com/questions/69803659/what-is-the-proper-way-to-build-for-macos-x86-64-using-cmake-on-apple-m1-arm | |
-- For Apple Silicon Macs | |
-- If you get "zsh: bad CPU type in executable" error | |
Manually install Rosetta 2 with following command: | |
softwareupdate --install-rosetta | |
-- For Apple Silicon Macs | |
-- If you get misc error with Brew, and you used Migration Assistant from Intel to Apple Silicon, consider the following: | |
Migration Assistant from Intel to Apple Silicon would copy over all x86 based packages which might cause compile issues later. So I am sharing my answer wider to just homebrew. | |
Two options I recommend: | |
1) "Rebuild" your system. Back it up to Time Machine and then use Migration Assistant again, but only for your user folder (do not choose "Applications" and "Other Files & Folders") and maybe System & Network settings. Then reinstall all the software with Apple Silicon versions. | |
2) If you are sure that you ever only used homebrew to install packages, and not macport or direct pkg installers for python/go/etc,etc. Then you can use the following commands to clean out old homebrew and install it again from start. You might need to sudo rm a few stubborn folders after running these 2 commands (or if you are feeling brave, use sudo to run the uninstall.sh). | |
curl -O https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh | |
/bin/bash uninstall.sh --path=/usr/local | |
-- AppleScript to open all tabs on current Brave window to Safari | |
tell application "Brave Browser" | |
set tab_list to every tab in the front window | |
repeat with the_tab in tab_list | |
set the_url to the URL of the_tab | |
tell application "Safari" to open location the_url | |
end repeat | |
end tell | |
-- To generate 5 random passwords | |
for i in $(seq 1 5); do LANG=C && echo $(tr -dc 'A-Za-z0-9!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~' < /dev/urandom | head -c 20); done | |
-- To hide a user account from the login screen and settings | |
-- Useful for selling old computer to someone online, hide an account with Find My enabled. When they have accepted and paid, then remove the Find My lock online. | |
sudo dscl . create /Users/<hiddenuser> IsHidden 1 | |
-- Find dupes and delete | |
brew install fdupes | |
-- Find dupes | |
fdupes -r . | |
-- Find and delete dupes, keeping first one | |
fdupes -rdN . | |
-- Delete pdfs under 1k in filesize | |
find . -type f -name "*.pdf" -size -1k -delete | |
-- iCloud Folders | |
iCloud Drive: $HOME/Library/Mobile\ Documents/com~apple~CloudDocs | |
Mobile Documents: $HOME/Library/Mobile\ Documents | |
-- To quarantine an app which you can't open, try | |
xattr -d com.apple.quarantine /Applications/Lepton.app | |
-- Split a folder with many files into subfolders of 1000 files in each | |
i=0; for f in *; do d=dir_$(printf %03d $((i/1000+1))); mkdir -p $d; mv "$f" $d; let i++; done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment