This page has been robot translated, sorry for typos if any. Original content here.

Terminal and its use for Mac OS X

On this topic:


Every year the number of switches is growing. Moreover, Macs are switching not only from Windows, there are very frequent cases of switching from Unix.

And while for most Unix users who are used to console commands, working with a terminal in Mac OS X does not cause any discomfort, for migrants with Windows, the situation looks somewhat different.

Most of these users rarely use the capabilities of the Unix kernel, without seeing any need for it.

Naturally, such a user may well do without a terminal, but knowledge of at least the basic commands in some situations can greatly facilitate the life of any owner of a computer running Mac OS X, and someone will simply help expand their horizons and better understand their chosen operating system.

Given that this short review is aimed at people who have never before had even minimal experience working with Unix-like systems, we will start from the very basics, gradually moving from simple to complex.

To begin with, it is worth mentioning the main command for any person who does not feel very confident at the sight of the command line: man . This is a reference. By adding the name of the command you need to man, you will get quite complete information about it. For example, enter man man, and you will see more complete documentation for this command in the terminal.

To exit, use the Q key.

Yes, it is worth noting that most of the commands in Unux systems that do not require information to be displayed to the user do not show anything at all if they are successfully completed. In cases where the purpose of the command is not to display information, only errors and warnings about violation of the normal execution of the command are displayed on the screen.

Navigation basics

cd command One of the main commands that allows you to navigate through various directories. If, by opening the terminal, you immediately want to be in a certain directory, say, in Documents, just type cd Documents in the terminal.

cd ~ command with which you go to your directory. For example, if you decide to go to the directory where your documents are located, then the following command will look like this: cd ~ / Documents.

cd - allows you to return to the previous directory.

cd .. allows you to go up to the higher directory.

ls lists the file names in the specified directory

ls -f shows files with their extension.

ls -a lists all files, including hidden ones.

ls -lo, besides listing existing files, also shows their permissions and the date they were created. If you want to list only files starting with a certain letter, for example, with Z, type ls Z *. Note that the command is case-sensitive, and if some files have a capital Z at the beginning, and you typed in regular z, you simply will not see them.

mkdir creates a new directory. For example: by typing: mkdir new, you will create a new directory called new.

rm delete the file.

rmdir delete directory.

Examples:

rm new.txt : the command will delete the new.txt file

rmdir new - removes a directory named new.

If you want to delete a directory that is not empty, you must either delete all its contents at the beginning or use the rm -R command

rm -R new : removes the new directory with all its contents.

clear - clears the terminal screen. In principle, sometimes it’s enough just to make a “right click” on the terminal window and select Clear Scrollback from the context menu.

script - allows you to save all the characters you entered from the keyboard into a text file. In practice - an extremely convenient option that allows you to get the most complete protocol of your entire session. The file will also indicate its exact date and time.

Script done on Sat Feb 10 18:21:32 2007

the file will be saved with the name typescript in your home directory.

bc - a command that displays the simplest console calculator on the terminal screen.

df is a simple command that will show you the volumes of your disk space, including network drives.

du - a list of all folders available on your system partition.

Copy

Copy files and folders using the cp command. Suppose you want to copy the note.txt file from your desktop to the Documents directory. In this case, you need to type cp note.txt ~ / Documents in the terminal

Please note that if a file with the same name exists in a new location, it will be deleted and replaced with the copied file without warning or the possibility of recovery.

If at the same time you want to not only copy the file, but copy and save it with a new name (for example, take new.txt as the new name), then the required command will look like this - cp note.txt ~ / Documents / new. txt . And in your folder, the new.txt file will appear, which is a copy of the note.txt file from the desktop.

In order to copy the desired file to the source directory, but with a new name, just cp note.txt new.txt

cp -R Documents "Documents backup" is a command through which you back up your entire Documents directory. Quotation marks are required because there is a space in the directory name.

sudo cp -Rp / Users "/ Users backup" - the command allows you to copy the entire directory / Users (including all home folders located inside), while preserving as much file information as possible (ownership, rights, etc., but not a branch resource) called Users backup. You must execute this command from under an administrator account, so in this example, sudo is used, which allows you to get temporary access with root privileges. If you execute a command from under a user account, you will be asked to enter the administrator password.

ditto allows you to copy the directory, but preserving the attributes of hidden files.

ditto Documents "Documents backup" copies the entire directory under the name "Documents" and names the copy of the directory "Documents backup".

mv - rename files. For example, typing in the terminal mv new old, you will rename the file or directory with the name new to old. And with the following command: mv old ~ / Documents - you move it from the current directory to your Documents directory.

Search

find - search for files matching your criteria.

Examples:

find / -name (file name without brackets): the command will search for the file you need across the entire file structure, including all mounted disks (including network drives) and display the full path to it.

find -x / -name (file name without brackets): the same, but the search will be performed exclusively on the boot disk of your system.

find. -name '* file name *' : (with the specified syntax preserved) - the search will be carried out in the current directory and all its subdirectories.

find. -mtime - (enter the number you need without brackets) : this command, like the previous one, searches the current directory and all its subdirectories, but its difference is that by specifying, for example, the number 3 (find. -mtime -3) the team will give you all the files that have been changed in the last three days. By specifying 0, the command will find only those files whose date has been changed.

The processes

ps -x command issuing in the terminal a list of processes of the current user.

ps -ax shows a list of all processes

ps - aux shows a list of all processes currently running in the system, as well as additional information about them, including CPU usage, time, etc.

top provides a list of the main processes involved and constantly updated information about them. CPU load, memory used, time, etc.

top -us5 is the same, but with the sorting of processes, by the amount of their load on the processor.

kill - a command that kills a frozen process or sends a signal to it.

sync - a command that force writes the contents of the disk cache to the hard disk.

Example: we type top command in the terminal, in the list of processes that appear, select the desired, say, process 2200, (in my case it turned out to be AppleSpell completely unnecessary to me) press Q to exit the menu - and after kill 2200. Once again type top - and make sure that the more mentioned 2200th process no longer appears in the list of running processes.

lsof is a command showing a list of open files on your system and files that are currently being accessed by application programs, for example, if you have a torrent client running, the command will even show the files that you are currently downloading or distributing. Naturally, to see the full list, you must have administrator rights.

sudo lsof - a list of open files, including the entire system. Administrator password required.

Finally, the sudo lsof -i command is a command that allows you to see all open network connections on your system.

Work with text

pico and nano are the simplest text editors for the terminal. Pretty limited in functionality. To exit pico or nano, press CTRL + X

more or less - displays the contents of a text file one page at a time. To go to the next, press the spacebar. It does not work with Word, PDF, RTF documents and any files other than txt format.

emacs - GNU Emacs is a fairly advanced editor developed for the X Window and loses some of its functionality when run in the terminal. But despite this, it allows you to see several files at the same time, move text between files and edit while shell commands work. A program that deserves a separate review, so here we will not dwell on the nuances of its configuration and use.

HDD

diskutil provides a list of all possible commands for operations with your hard disk. From simply viewing the diskutil list command to operations such as mounting partitions, repairing familiar “permichons” or a format. Simply put, you have all the options (in fact, there are even more) of the already familiar Disk Utility with the difference that you perform all the actions through the command line interface. Here is an example of just a few diskutil commands.

diskutil resizeVolume allows you to resize the selected partition

diskutil partitionDisk allows you to perform operations on the disk and its individual partitions. Formatting, file system selection, etc.

diskutil eraseDisk erases information from your hard drive.

diskutil info / displays information about the boot partition of your disk. Its name, file system type, total size, total and free space, etc.

diskutil info / Volumes / here the partition name is the same, but for any other mounted but non-boot partition of your hard drive.

sudo diskutil repairPermissions / is another command to fix permissions on a boot disk from your MacOS X. When you run it, you will be required to enter an administrator password.

sudo passwd root is a command that you may need only in one case - if you forgot the root password, the administrator password.

reboot - reboot the computer

shutdown -h now - a command to shut down your computer without time delay and unnecessary questions.

(The last two commands are executed exclusively from the administrator account.)

Of course, few users use the capabilities of the terminal in everyday life. This seems to some an ancient and obsolete anachronism, someone is repelled by the need to memorize a rather large list of necessary commands, at a time when most of the required tasks can be performed using a conventional mouse. But, once you start working with the terminal, you yourself will quickly appreciate the possibilities that it provides you.