Have you ever wanted to see what's in a Zip archive without unzipping it? Well, it turns out that there is a Terminal command you can use to do so: zipinfo. Just run zipinfo [filename] to get a list of files in an archive.
There are a number of interesting options to this command. The default behavior is to list the files in a short "ls -l" format, but you can also use the -m or -l options to have medium or long lists. You can use the -1 option to only show file names. And there are other useful options in the man zipinfo page.
One other useful item is the amount of space the archive takes up, and the amount of space saved. For example:
Find which users take up the most disk space. This can of course be used to indentify any number of users so I picked 10 as an arbitrary number.
If you are already root (unlikely, but possible) you can remove the sudo -s part. Here's the command:
sudo -s du -sm /Users/* | sort -nr | head -n 10
Generally you would have to make sure that you use sudo -s or it will give a few Permission denied errors before finally spitting out the results, and they may be incorrect. The directory structure should start with the /Users Directory and then it will recursively perform the operation. This may miss any folders outside of the normal user space, but there shouldn't normally be any user data there.
[crarko adds: I tested this, and it works as described. There are third party utilities that will give the same results with a GUI, but this is pretty handy to have for use on remote machines over an ssh connection. It can take a while to complete, so be patient. Also I noticed in Activity Monitor the du process was using a lot of cpu, so it's best to do this while not running other cpu (or I suppose disk) intensive programs.]
There may be times when you want to consolidate all the files in a directory and its sub-directories (or a folder and its sub-folders) into a single directory or folder. For example, you may have a folder with sub-folders for years, and other sub-folders in each year folder for months, and you may want to move files in the month folders all to the top level.
Doing this manually is a complex and time-consuming process. While you might be able to do this by using a search - for example, if all the files are, say, Excel files, you can search for Excel files in the top folder, then just copy them all to a new folder - if there are lots of different types of files, this wouldn't make things easier.
Fortunately, there's a way to do this from a command line. On the BedroomLAN blog, Alexios presents two commands that will do this:
find -type f -print0 | xargs -0 -I%%% cp %%% $FLAT_DIRECTORY
Replace $ROOT_DIRECTORY with the top level directory containing all the sub-directories and files, and replace $FLAT_DIRECTORY with the directory you wish to contain all the files. Note that this command will overwrite any files with the same name, so if you don't have uniquely named files, it's not ideal.
You can also use the ln command instead of the cp command, and this will not overwrite files, but will give error messages if there are duplicate file names. See the blog post for more details on this.
H/t to robg for pointing this out.
I have a recurring need to extract full names and email addresses from a plaintext archive of email messages. The archive is created by selecting a bunch of emails in Mail, copying them, pasting into TextEdit, and converting to plain text.
For each message in the file, the first line contains the information I wanted:
From: Joe Example <email@example.com>
I wanted one email address per line, suitable for pasting into another location. I am far from an expert with the bash shell, but here's what I came up with—I imagine there are many more efficient ways to do this, as I'm sure experienced perl, sed, awk, etc. users may point out. Note that this is highly dependent on the format created by Apple's Mail app in OS X 10.8.
The grep bit pulls out the entire From: line, then the first cut command grabs the email address and the trailing close-bracket, by setting the delimiter to an open bracket. The second cut eliminates the closing bracket, by setting that as the delimiter. The output will be one email address per line, sitting on your clipboard ready for pasting. (To debug, just remove the | pbcopy bit to see the output.)
I also wanted to extract the names, and came up with a variant to do just that:
grep 'From:' ~/Desktop/testfile.txt | sed -e 's/: /:^/g' | sed -e 's/ \</^\</g' | cut -f2 -d^ | pbcopy
This one is messier, as names can contain one or more spaces. After getting the From: line, sed is used (twice) to add a carat delimiter immediately after From:, and immediately before the opening bracket of the email address. I then used cut, with the delimiter changed to the carat, to extract the full name (field two) from the found lines. Again, the results are copied to the clipboard; leave this bit off for debugging.
With the names and addresses extracted, it's fairly easy to do other stuff with them. In my case, I'm reading them into a couple of array variables in a bash script, so I can then output a name and email address pair to consecutive locations on my multi-pasteboard. If you want to use the names in an array in a bash script, you'll want to change the array delimiter from a space to a newline:
Without this, your array will get split anywhere there's a space in the name values ... or so I've heard, not that it's ever happened to me!
There are several ways to open a Terminal window to the current directory in the Finder. But wouldn't it be useful if you could do the reverse and open a Finder window to the current Terminal directory? Well, you can, and you can completely control the Finder from the terminal.
I have put the code on github with full instructions on how to set it up. It works by using bash_completion, .bash_profile with some applescript to control the Finder, .inputrc and .bash_aliases.
Here are some of the features:
Changing a directory in the Terminal opens the same directory in the Finder.
You can change the Finder window view from the Terminal (column, list, icon views).
It is case insensitve, you can press Tab for menu completion, and Shift-Tab to expand bash aliases.
Open a Terminal directory to the current Finder window.
This code will work with both the Terminal.app and iTerm2 and should work with older macs as well
[kirkmc adds: I haven't tested this, but it sounds very useful.]
If you use top in Terminal, you may occasionally see apps with hugeVSIZE values. I know this because Witch, one of our apps, is an example of such—it's VSIZE can exceed 11GB.
In trying to figure out why this was so (short answer seems to be: we can't control it, but it's not a problem), I ran across an interesting command, vmmap. This command will spew out a ton of detail about virtual memory usage. Stringing a couple Unix commands together, though, you can extract just the summary portion of the report.
[kirkmc adds: The problem with this hint is that each line of code works for different types of volumes. If you have both local and network volumes, you need to use both; you could, of course, stick them together with a semi-colon separating them so they are, in effect, a single command.. In my tests, it's not really faster than the osascript code above, but I'm posting it because some may find it useful. If you have different results in speed or efficacy, please post in the comments.]
Imagine that you run a script automatically on your Mac, and that you want to check the result of that script. There are many ways you could do this, such as remotely connecting to the Mac, or sending the results by e-mail. But with iCloud, you can also save the output to a file and put it on iCloud, where you can access it with your favorite iCloud-compatible text editor on another Mac, or on an iOS device.
To do so, simply send the output of the script to a file like this:
So, to save a list of a directory's contents, you'd use this:
ls -al > ~/Library/Mobile\ Documents/com\~apple\~TextEdit/Documents/list.txt
That saves a file called list.txt in TextEdit's Documents folder. Look inside the ~/Library/Mobile\ Documents folder for the paths to other apps you have that can use iCloud. Each folder in the Mobile Documents folder has a Documents sub-folder. Depending on the app, you may be able to access the files on another Mac or on iOS.