Some common command-line tricks in Mac OS X

Source: Internet
Author: User
Tags administrator password

First, network settings related

1. Dynamic reset of the physical address of the network card

For some requirements, such as the IP address on the network or the network account and the physical address of the NIC to bind, so that multiple devices cannot switch the Internet, you can try to temporarily change the physical address. However, system preferences cannot modify the physical address of the NIC, which is where the command line comes in. Specifically, if you have a physical address in mind that you want to modify, such as 00:1f:3c:93:b5:99, the executable

$ sudo ifconfig en1 ether 00:1f:3c:93:b5:99

To temporarily modify the physical address of the network card, if there is no physical address, just want to temporarily enable the attempt, you can use OpenSSL to generate a random physical address:

$ OpenSSL Rand-hex 6 | Sed ' s/\ (.. \)/\1:/g; s/.$//'

Of course, the physical address generated with OpenSSL may conflict with the network card address, before enabling the randomly generated network card address, it is better to first confirm whether the IP address has been bound to the physical address:

$ arp-a

2, hostname, computer name reset

The default hostname is very silent, especially when there are some special characters associated with the locale. Typically, these are not visible, but when you use the terminal frequently, you get sick of these inexplicable hostnames. Therefore, modifying the hostname is a top priority, but the method of modification is simple. Click on the top left corner of the Apple icon, System Preferences, share, and then modify the host computer name and LAN host name. Again, the Unix command line is powerful, so modifications to the hostname and computer name can also be modified from the command line.

$ sudo scutil--set ComputerName newnameformacosx
$ sudo scutil--set HostName newhostnameformacosx

OK, check to see if the changes were successful:

$ scutil--getcomputername
$ scutil--get HostName

Put the hostname into the/etc/hosts file

$ echo newhostnameformacosx >>/etc/hosts

Restart the network service to make these changes take effect immediately:

$ sudo dscacheutil-flushcache
$ sudo killall-hup mdnsresponder

3. Use of command-line download tools

As is known to all, wget is mostly a standard command-line download tool in the *nix system. However, Mac OS X does not bring its own wget, instead it is the command line download tool curl. Here are some basic uses of curl, such as direct download

$ Curl-o Http://

If you want to rename the download file

$ Curl-o ' Http:// '

If you want to show progress during the download process, you can perform

$ Curl-#-O

If the downloaded URL link needs to be redirected, try the following

$ curl-l-o ' ' Http:// '

If you want to save bandwidth, perform

$ curl-l-o--compressed ' '

If you need to enter a user name and password for the download link, you can try

$ Curl Ftp://james:[email protected]: 21/path/to/backup.tar.gz
$ Curl Http://james:[email protected]/file/path/data.tar.gz

If you downloaded an encrypted link, such as a download link provided by ssl/https/sftp, try

$ Curl--ftp-ssl-u james:123456ftp://
$ curl-u James Sftp://

4. System update and command line tool installation

Typically, software updates on Mac OS X are done through the APP store, but for those geek, you can try the following commands:

$ sudo softwareupdate--list

All possible updates are listed and then the required updates are selected for installation

$ sudo softwareupdate--installsomeapp

If you do not want to choose to update all possible updates directly, the executable

$ sudo softwareupdate-i-a

Xcode is Apple's integrated development environment, with Apple's improved GCC version of LLVM-GCC. It's easy to install, just go to the App Store and download But for Unixgeek, this is not satisfactory. Because does not own a command-line tool, it needs to be installed separately. Before Xcode 5.0, the command-line tool was very easy to install, just open, then click on the components, download, preferences, and select the Xcode command-line tool to install. However, since Xcode 5.0, the command-line tool to install Xcode must execute the following command at the terminal:

$ xcode-select--install

Second, the file system related operations

1. HFS File System Extended Properties

On the hfs+ file system under Mac OS X, files are often appended with Mac-specific extended attributes. Like what

$ ls-l
Total 0
[Email protected] 1 Daniel staff 0 Jul 19:28 1.txt
-rw-r--r--1 Daniel staff 0 Jul 19:28 2.txt

There will be an @ flag. This property is appended when the user makes any action on the file in the Finder. For example, right-click on Get Info, then write a few words in spotlight commands and then delete it, this file will be accompanied by the @ attribute. If you want to remove the file's @ property manually, you can use the command xattr. For example

$ xattr-l 1.txt
$ xattr-d 1.txt
$ xattr-l 1.txt
$ ls-al 1.txt

There is no special attribute at this time.

2. Copy, package and unpack files or folders

Due to the extended attributes in Mac OS x file System, it is a strange mistake to release files from MacOS x system directly to the Linux system using packaging. Here we need to deal with the extended properties of the file during the copy and package process. First, the CP command for MAC OS X has an option-X, which allows you to ignore the extended properties of these files during the copy process. Therefore, before packaging, you can copy a package without the extended attributes, for example

$ CP-XR/PATH/TO/SRC-| Tar czvf src.tar.gz-

Of course, except for extended attributes, typically, files are packaged directly into the Mac OS X system-specific hidden files, which flow into _macosx,. _filename, and. Ds_store and so on. When you hit a zip package, you can use the-X option to exclude these files, such as

$ zip-r-X folder_to_compress

However, there is no such thing as the-X option for the *nix system to have a packaged command tar. Fortunately, Mac OS X also provides a special method, for example, when you hit the gzip package, you can use the following command

$ copyfile_disable=1 TAR-ZCVF archive_name.tar.gz folder_to_compress

It's also easy to hit bzip2 bag:

$ copyfile_disable=1 TAR-JCVF archive_name.tar.bz2 folder_to_compress

For other formats, the TAR package shines this processing. The unpacking process is relatively straightforward, and here is an unpacking-related Bash configuration:

$ echo "# Ignore useless invisible files or extendedattributes on Mac OS X for CP and Tar
Export Copyfile_disable=true
# Extract:extract most know archives with one command
Extract () {
If [-F $]; Then
Case $1in
*.TAR.BZ2) tarxjf $;;
*.tar.gz) tar xzf $;;
*.BZ2) bunzip2 $;;
*.rar) Unrar e $;;
*.gz) gunzip $;;
*.tar) tar xf $;;
*.TBZ2) tarxjf $;;
*.TGZ) tar xzf $;;
*.zip) unzip $;;
*. Z) uncompress$1;;
*.7z) 7z x $;;
*) echo "' $ ' cannot be extracted via extract ()";
echo "' is not a valid file"
} ">> ~/.BASHRC

3, the production of DMG format files and ISO conversion Mutual transfer

The DMG format is a commonly used packaging format in Mac OS X, creating DMG-formatted files that can be used directly from the system's own disk Utils. But here's a little bit about the use of command-line Hdiutil. Create a file in DMG format

$ hdiutil create-size 100m-stdinpass-format udzo-srcfolder folder_to_compress archive_name.dmg

The available DMG formats are UDZO (compression format, default), Udro (read-only format), UDBZ (Better compressed image), UDRW (writable format) udto (DVD format). If you want to change the size of the DMG file, perform

$ hdiutil Resize 150m/path/to/the/diskimage

To modify the encryption password in the DMG format, perform

$ hdiutil Chpass/path/to/the/diskimage

Files mounted in DMG format are available

$ hdiutil Attach ARCHIVE_NAME.DMG

Its mount point is in the directory with the same name as the/volumes directory

$ ls-lah/volumes/archive_name/

To uninstall a DMG file:

$ hdiutil eject/volumes/archive_name/

Converting ISO-formatted files to DMG-formatted files

$ hdiutil Convert/path/imagefile.iso-format udrw-o/path/convertedimage.dmg

Convert DMG-formatted files to ISO-formatted files

$ hdiutil Convert/path/imagefile.dmg-format UDTO-O/PATH/CONVERTEDIMAGE.CDR
$ hdiutil Makehybrid/path/convertedimage.cdr-iso-joliet-o/path/convertedimage.iso

4. File System mount

In most cases, the File Manager Finder will automatically discover external devices such as USB, so that users do not have to mount the partitions themselves, but sometimes, for example, when a Mac OS X hard disk is re-partitioned, partitions of non-HFS file systems, such as MSDOS partitions or EXT4, are zoned out, if you want to read and write these partitions, You will need to mount these file systems manually. The first step in mounting a file system is to set up mount points, such as

$ sudo mkdir/volumes/foo
$ sudo mkdir/volumes/bar

Then see what devices you need to mount and mount them manually

$ ls/dev/disk*
$ sudo mount-t msdos/dev/disk1s1/volumes/foo
$ sudo mount-t hfs/dev/disk1s2/volumes/bar

Finally, uninstall these partitions that are not needed

$ diskutil EJECT/DEV/DISK1S1
$ diskutil eject/dev/disk1s2

4. Hidden properties of a file or folder

Typically, in order to protect files in Mac OS x systems from being arbitrarily modified by the user, the file Manager Finder does not display files with hidden properties and system files. However, there are times when you need to manipulate these files (clips), and it's easy to have the finder find them. In the Finder's menu bar, click Go-and Go tofolder, which corresponds to using the shortcut key shift+command+g. Of course, if you are not satisfied with the file manager that comes with Mac OS x, you can install a third-party file manager, such as Totalfinder. In fact, Mac OS X also provides other methods for hiding files alone, which are described below.

4.1 Modify Finder profile to show hidden files to Finder programs

$ defaults Write appleshowallextensions TRUE

Let Finder program not show hidden files

$ defaults Write Appleshowallextensionsfalse

The finder's configuration file affects the entire file system and is a more violent approach.

4.2. Turn on or off hidden properties for some files or folders use the following command to dismiss a folder's hidden properties:

$ chflags Nohidden ~/library

To recover hidden properties of a folder:

$ chflags Hidden ~/library

In fact, the hidden properties of files and directories in the hfs+ file system can also be modified by the Setfile command. For example, perform

$ mkdir-p ~/sites
$ setfile-a V ~/sites

You can turn on the hidden properties of the folder ~/sites, and execute

$ setfile-a v ~/sites

You can turn off its hidden properties.

Iii. Miscellaneous

1. Reset the Forgotten Administrator password

First, press Command+s to enter single-user mode when the system is not in the login screen. And then enter


Mount the file system in read-write mode; then reset the administrator James Password

$ passwd James

When you are finished, enter

$ reboot

Restart the boot.

2. Rebuilding the Spotlight Database

The simpler way is to use the mouse to click the System preference->spotlight and rebuild the database; For command-line enthusiasts, you can try

$ sudo mdutil-e/

3. Clean up the cache

$ sudo purge


Some common command-line tricks in Mac OS X

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.