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 switchers is growing. And on the Mac do not just go with Windows, very often the transition from Unix.

And if most Unix users, accustomed to console commands, work with the terminal in Mac OS X does not cause any discomfort, then 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 can do without a terminal, but knowing 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 his horizons and better understand his chosen operating system.

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

First of all, it's worth mentioning the main team for any person who does not feel very confident when seeing the command line: man . This is a reference. Adding the name of the command you need to man, you will get enough information about it. For example, type man man, and you will see in the terminal more complete documentation for this command.

Use the Q key to exit.

Yes, it is worth noting that most of the commands in Unux systems that do not require the output of information to the user, in the case of its successful completion, do not show anything at all. In cases where the purpose of the command is not to output information - only errors and warnings about violation of the normal execution of the command are displayed on the screen.

Navigation Basics

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

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

cd - allows you to return to the previous directory.

cd .. allows you to go to the parent directory.

ls displays a list of file names in the specified directory

ls -f shows files with their extension.

ls -a lists all files, including hidden files.

ls -lo except for enumeration of existing files also shows their access rights and the date of their creation. If you want only files beginning with a certain letter, for example, with Z, to be listed, type ls Z *. Keep in mind that the command is case sensitive, and if some files have an uppercase Z in the beginning, and you typed a regular z, you simply will not see them.

mkdir creates a new directory. For example: typing the command: mkdir new, you will create a new directory with the name new.

rm delete the file.

rmdir remove the directory.

Examples:

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

rmdir new - removes the directory named new.

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

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

clear clears the terminal screen. In principle, sometimes it is quite easy to make a "right click" on the terminal window and select Clear Scrollback from the context menu.

script - allows you to save all typed characters into a text file. In practice, an extremely convenient option that allows you to get the 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 amount of your disk space, including network drives.

du is a list of all the folders on your system partition.

Copying

Copying files and folders is done using the cp command. Let's say you want to copy a note.txt file from the desktop to the Documents directory. In this case, you will need to type in the terminal cp note.txt ~ / Documents

It should be noted that if a file with the same name exists in a new location, it will be deleted and replaced with a copied file without any warnings and recovery options.

If you want to not only copy the file, but copy and save it with a new name (for example, take the new name as new.txt), then the required command will look like this: cp note.txt ~ / Documents / new. txt . And in your folder there will be a file new.txt, 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 type cp note.txt new.txt

cp -R Documents "Documents backup" - the command by which you save a backup copy of your entire Documents directory. Quotes are needed, since there is a space in the directory name.

sudo cp -Rp / Users "/ Users backup" - the command allows to copy the entire directory / Users (including all home folders inside), while preserving as much file information as possible (belonging, rights, etc., but not a branch resource) with the name Users backup. You must run this command from under the administrator account, so, in this example, sudo is used, which allows you to get temporary access as root. If you are running a command from a user account, you are prompted for an administrator password.

ditto allows you to copy a directory, but with the preservation of the attributes of hidden files.

ditto Documents "Documents backup" copies the entire directory as "Documents" and names a copy of the "Documents backup" directory.

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

Search

find - search for files that match your criteria.

Examples:

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

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

find. -name '* filename *' : (with saving the specified syntax) - the search will be performed in the current directory and all its subdirectories.

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

Processes

ps -x command that outputs the list of processes of the current user in the terminal.

ps -ax shows a list of all processes

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

top provides a list of the main processes involved and constantly updated information about them. Load on the processor, the size of the memory involved, 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 hung process or sends a signal to it.

sync - a command that forces the contents of the disk cache to be written to the hard disk.

Example: type in the terminal command top, in the list of processes that appears, we select the necessary, say, process 2200, (in my case it turned out to be absolutely unnecessary to me AppleSpell), 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 that shows a list of open files on your system and files currently accessed by application programs, for example, if you have a torrent client running, the command will show even the files that you are downloading at the moment, or distribute. Naturally, to see the full list, you need to have administrator rights.

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

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

Work with text

pico and nano are the simplest text editors for the terminal. Quite bounded in the functional. 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 under the X Window and losing some of its functionality when running in the terminal. But despite this, allows you to see several files at the same time, move the text between files and edit while running shell commands. The program, which deserves a separate review, so here we will not dwell on the nuances of its configuration and use.

HDD

diskutil displays 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, fixing all familiar "permishons" or format. Put simply, before you all the options (in fact, even more) are already familiar to you Disk Utility with the difference that all the actions you perform through the command line interface. Here is an example of just a few commands offered to you by diskutil

diskutil resizeVolume allows you to resize the selected partition

diskutil partitionDisk allows you to perform operations on the disk and its individual sections. Formatting, selecting a file system, etc.

diskutil eraseDisk erases the information from your hard drive.

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

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

sudo diskutil repairPermissions / another command to "fix the perm" on the boot disk from your MacOS X. When executing it, you will be prompted to enter the administrator password.

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

reboot - restart computer

shutdown -h now - a command that turns off your computer without time delay and unnecessary questions.

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

Of course, few users use the terminal's capabilities in everyday life. For some it seems an ancient and outdated anachronism, someone is repelled by the need to memorize a fairly 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 opportunities that it provides to you.