Posts

Showing posts from February, 2021

Pause a program which is using too much CPU

Sometimes your battery is running low and you need to pause a program without quitting it or force-quitting it, so that you can preserve battery but not lose the job that the program is doing. To do this,  Step 1. open a terminal (Utilities: Terminal) Step 2. type:  ps ax | grep -i "<program name>" so for example, if it's photoshop you need to pause, replace "<program name>" with "photoshop" Step 3. Next to the results on the far left you'll see a number, usually with 5 digits. E.g. 12345. Step 4. Type:  kill -STOP 12345 where 12345 are the numbers obtained in Step 2-3. Step 5. When you want to resume the program, type kill -CONT 12345 It should then continue to operate or run as usual without problems.

youtube-dl syntax

Technically, youtube-dl performs an illegal action which is to download or potentially download copyrighted videos. However, if you own the video, and want to take a backup copy, it is useful to do this. I did not write the below, when I find the source I will credit it. The general syntax is: youtube-dl -x --audio-format mp3 --audio-quality 320K youtube-dl will output in the current directory To list available formats youtube-dl --list-formats URL To download an entire user/channel youtube-dl -citw ytuser:<USER> or youtube-dl -citw URL To download all videos on a playlist youtube-dl -citw <PLAYLIST URL> To download a YouTube video as an MP3 youtube-dl -x --audio-format mp3 --audio-quality 320K <VIDEO_URL> To download best available video (mp4) and best available audio (m4a) and then merge them together youtube-dl -citw -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]' <VIDEO_URL> To set the output file title youtube-dl SOURCEURL --restrict-filenames --output ...

adding integers in BASH

 Addition in Bash is notoriously tricky and it almost never works as expected; it usually appends/joins two items as if they were strings regardless of whether they are in fact integers. This example works. Create a textfile containing lines of text, e.g.  a b c d Call it " list.txt ". Save this into a bash script and run it: #!/bin/bash counter=0 for i in `cat list.txt` ;  do   echo $counter   ((counter=counter+1)) done

non-interactive password change

 type (in BASH): echo -e " new_password  \n  new_password " | (passwd --stdin $USER) note the spaces around \n are there for readability, do not put them in.

managing virtualbox on the command line

You might want to manage virtualbox (the PC emulator) on the command line. The use case would be if you want to make a virtual machine (VM) on your machine which does not appear on-screen or which is started automatically. Suppose the virtual machine instance is called 'ubu'. The following commands will be useful. They are self-explanatory. The command VBoxManage should be in /Applications/VirtualBox/Contents/Resources/MacOS/ or similar. VBoxManage startvm "ubu" --type headless VBoxManage controlvm "ubu" pause --type headless VBoxManage controlvm "ubu" resume --type headless VBoxManage controlvm "ubu"  savestate VBoxManage controlvm "ubu" poweroff --type headless VBoxManage startvm "ubu" --type gui

Command line security tool on Mac to stop Root from damaging things

On the newer versions of Mac OS X (10.13 onwards, I think), there's a stricter permissions setting called "System Integrity" which stops even the root user from doing certain things on the command line, e.g. messing with the operating system itself. This can be very annoying if you are used to doing things as root. It can also stop you from installing some unix-type utilities. From the Apple menu, choose “Restart…” > “Restart“. Simultaneously press and hold the “Command” and “R” buttons. This will start you in Recovery Mode. Select “Utilities” > “Terminal“. Type one of the following, then press “Enter“: Disable System Integrity Protection:  csrutil disable Warning: your system is now less secure. If you get hacked, it was your decision to do this.

splitting a pdf

Suppose you want to take a 1000-page pdf called "input.pdf" and extract page 1, and 3 onwards, removing only page 2. Type the following:  pdftk  input.pdf  cat 1 3-end  output output.pdf

stop autoplay videos in safari on mac

Open Terminal Type  defaults write com.apple.Safari IncludeInternalDebugMenu 1 That enables the Debug menu in Safari.  Go to Safari Check this menu item: Debug -> Media Flags -> Video Needs User Action Voila, no more auto-play videos.

which cloud solution?

Introduction I've experimented with the four major cloud solutions: Dropbox Google drive iCloud files feature OneDrive I prefer Dropbox because it has a good undelete history. I also use ownCloud, because then I feel safe in that I know where my files are. Main feature choices All of them allow automatic file upload, and particularly the desktop/documents/downloads folders can be automatically backed up, as well as your pictures. The main advantage to google drive and onedrive in particular are they let you edit documents online. Dropbox does as well, but I don't know anyone who uses it. Warnings I've had bad experiences with iCloud. Like all of the solutions, it offers an option to upload and store files in the cloud and clear them off your drive, leaving a link instead, which when you click it, downloads the file to your machine. Dropbox offers you this feature when you install it (these days). I recommend against doing this. The reason is when you backup your primar...

rsync

Rsync is a useful utility to compare two folders and copy the differences. It works over ssh as well as on local disks, and it can also copy changes to files rather than whole changes. If you use it to copy one folder to another, it can also carry on where it leaves off, if you interrupt it. It is therefore better than other copy utilities. The general syntax for comparing folder A to folder B and copying the differences is: rsync -avu A/ B/ --progress If folder B is on a server, let's say, www.mywebsite.com, then,  rsync -avu A/ username@www.mywebsite.com:/var/www/B/ --progress --rsh="ssh -p22" Assuming ssh answers on port 22 and your folder B is located in /var/www/ Note that it is imperative that you put a forwardslash (/) after both folder names. If you don't do that, it will copy one folder into the other. Note that you can also delete files that are not in the target. This is dangerous. Say you have folder A and B, and say folder A has items 1,2,3,4, and folder ...

kernel extensions

When the system starts up, sometimes it loads kernel extensions which add features to the operating system. They are stored in /Library/Extensions/ If you install one (copy it into the folder) and do not want to reboot your machine to load it, just type: sudo kextcache -i /

resetting mac password from the command line

This post is not original. I copied it from elsewhere. When I find the original I will reference it here. hold down command-S on startup. Run mount -uw /. (fsck -fy is not necessary.) In 10.7 and later, run: launchctl load /System/Library/LaunchDaemons/com.apple.opendirectoryd.plist or in 10.6 and earlier: launchctl load /System/Library/LaunchDaemons/com.apple.DirectoryServices.plist Run  passwd username  and reset the password. Run  reboot A second way to reset the login password of an account is to use Reset Password.app from the recovery partition: Hold command-R on startup. Open Terminal from the Utilities menu. Run resetpassword , which opens the Reset Password application. Select the volume and account and reset the password. A third way is to create a new admin account: Hold command-S on startup. Run  mount -uw / Run  rm /var/db/.AppleSetupDone Run  reboot Go through the steps of creating a new account. Reset the password of the old account fro...

managing and making writable ntfs drives on mac

 You need the package ntfs-3g to manage ntfs drives on linux. When you attach an NTFS drive which was recently used on a windows computer, it might contain a sleep image and refuse to let you mount it writable. brew install ntfs-3g sudo ntfsfix /dev/disk2s4 sudo mkdir /Volumes/ntfs; sudo ntfs-3g  /dev/disk2s4 /Volumes/ntfs/ -o remove_hiberfile assuming the drive is your second drive partition 4

Bulk delete files with spaces in their name

 Different methods: xargs -d "\n" -I {} rm \"{}\" find . -name "*.txt" -print0 | xargs -0 rm find . -name "*.txt" -delete find . -name "*.txt" -exec rm {} +

find files on command line by specific modification dates

find /path/to/dir -newermt "date" find /path/to/dir -newermt "Feb 07" find /path/to/dir -newermt "yyyy-mm-dd" ## List all files modified on given date find /path/to/dir -newermt yyyy-mm-dd ! -newermt yyyy-mm-dd -ls # Assuming you want to find perl files: ### print all *.pl ### find /path/to/dir -newermt "yyyy-mm-dd" -print -type f -iname "*.pl"

finding email addresses in mbox files

 Assuming the mbox has been saved into a file called thefile.txt: grep -E -o "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b" thefile.txt

Enable appleshare and sample iptables syntax

Install netatalk if it is not installed already:  apt-get install netatalk [Debian-based] or  yum install netatalk [Redhat-based] service netatalk start The following syntax will enable appleshare over IP (linux only) iptables -A INPUT -i eno1 -p tcp --dport 548 -m state --state NEW,ESTABLISHED -j ACCEPT iptables -A OUTPUT -o eno1 -p tcp --sport 548 -m state --state ESTABLISHED -j ACCEPT  

Find and delete when found

On the terminal, if you want to delete a lot of items at once without dragging them to the Finder's trashcan icon, type: find [source folder] -name "*[whatever word you want to find]*" -delete Beware this is not the same as dragging to the trash, it's permanent and non-recoverable. I'd suggest running this first to make sure it will delete the right things: find [source folder] -name "*[whatever word you want to find]*" -print

Tweaking spotlight

Spotlight generally causes disks to slow down and uses a lot of CPU cycles. The command-line utilities find, grep and locate are more efficient and run on-demand rather than randomly starting up and making your disk busy. If you find you can't eject a disk, it's generally because spotlight is using it. To permanently turn spotlight off for every disk: launchctl unload -w /System/Library/LaunchDaemons/com.apple.metadata.mds.plist Warning: if you do that, you won't be able to search using the magnifying glass tool in the menubar. To turn off spotlight for any attached (USB) disk: sudo defaults write /.Spotlight-V100/VolumeConfiguration Exclusions -array "/Volumes" To turn off spotlight for any specific disk: sudo mdutil -a -i off /Volume   rm / .metadata_never_index     mdutil -i   on   /

The most useful defaults-write options

The following are really useful options to set system-wide for Mac OS X. I'd suggest reading them to guess what they do. You should be able to safely run all of them except the first one which tells spotlight (the search function) to ignore any attached drives forever. If you find that drives take a long time to unmount (eject), then usually it is because spotlight is indexing them (reading them in their entirety) to see what they contain. Hence if you find a disk doesn't want to eject, turn off spotlight for that drive. sudo defaults write /.Spotlight-V100/VolumeConfiguration Exclusions -array "/Volumes" defaults write com.apple.dock springboard-columns -int X defaults write com.apple.dock springboard-columns -int 10 defaults write com.apple.dock springboard-rows -int 7 defaults write com.apple.dock springboard-rows -int 5 defaults write com.apple.dock springboard-rows -int 5 springboard-columns -int 10 defaults write com.apple.dock springboard-rows -int X defaults w...

Create Mac OS X boot disk image

Go into the downloaded installer app, e.g. cd /Applications/Install\ OS\ X\ Yosemite.app/ and go into the Resources/MacOS folder, in the Terminal. cd Resources/MacOS/ look for an executable called " increateinstallmedia ". If you can't find, it, try find ./ -name "createinstallmedia" -print Once you find it, run this: ./createinstallmedia --volume /path/to/target --applicationpath /Applications/Install\ OS\ X\ Yosemite.app/ --nointeraction

create a DMG file on the command line

Sometimes you might not want to fiddle with the Disk Utility. Instead, you can do it on the command line. 1. Open Terminal.app 2. Run this: hdiutil create -encryption -size 2m -fs HFS+ -volname DISKNAME ~john/DISKNAME.dmg This will create a 2 megabyte sized disk image, in HFS+ format, encrypted. It will ask for a password. 3. You can also convert formats, e.g. ISO to IMG hdiutil convert -format UDRW -o raspbian.iso raspbian.img

The purpose of this blog

 The purpose of this blog is to keep track of useful Mac-specific tips.