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 grows. Moreover, Macs are transferred not only from Windows, there are very frequent cases of transition from Unix.

And if the majority of Unix users who are accustomed to console commands working with a terminal on Mac OS X do not cause any discomfort, then for migrants with Windows, the situation looks a little different.

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

Naturally, such a user can easily do without a terminal, but knowing at least the basic commands in some situations can make life easier for any computer owner running Mac OS X, and just help someone to expand their horizons and better understand their chosen operating system.

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

For a start, it is worth mentioning the main command for any person who feels not very confident at the sight of the command line: man . This is a reference. Adding to the man the name of the command you need, you will get quite complete information about it. For example, enter man man, and you will see more complete documentation on 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 the output of information to the user, in case of their successful completion, do not show anything at all. In cases where the purpose of the command is not to display information - only errors and warnings about the violation of the normal execution of the command are displayed on the screen.

Basics of navigation

cd command One of the main commands for navigating various directories. If, opening a terminal, you immediately want to be in a certain directory, say, in Documents, you just need to type in the terminal cd Documents.

cd ~ command with which you navigate 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 extensions.

ls -a lists all files, including hidden ones.

ls -lo, in addition to listing existing files, also shows their access rights and the date they were created. If you want to list only files starting with a specific letter, for example, with Z, type ls Z *. Remember, the command is case-sensitive, and if some files have a capital Z at the beginning, and you typed the usual z, you simply won't see them.

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

rm delete file.

rmdir remove directory.

Examples:

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

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 : remove the new directory with all its contents.

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

script - allows you to save in the text file all the characters you entered from the keyboard. In practice, it is an extremely convenient option that allows you to get the most complete protocol for the 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 is a command that will display 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 - a list of all folders available on your system partition.

Copying

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

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

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

In order to copy the desired file to the original directory, but with a new name, you just need to type cp note.txt new.txt

cp -R Documents "Documents backup" is the command through which you save a backup of your entire Documents directory. Quotation marks are necessary, as 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 that are inside), while keeping as much file information as possible (membership, rights, etc., but not a branch resource) with the name Users backup. You must execute this command from under the administrator account, for example, sudo is used in this example, which allows you to get temporary access with root privileges. If you are executing a command under a user account, you will be prompted to enter an administrator password.

ditto allows you to copy the directory, but with 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 - rename files. For example, by typing mv new old in the terminal, you rename the file or directory called new to 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 (file name without brackets): the command will search for the file you need across the entire file structure, including all mounted drives (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 '* filename *' : (with the specified syntax preserved) - the search will be performed in the current directory and all its subdirectories.

find. -mtime - (here you need the number to enter without parentheses) : 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 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.

Processes

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

ps -ax shows a list of all processes

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

top lists the main processes involved and constantly updated information about them. The load on the processor, the size of the memory involved, the time, etc.

top -us5 is the same, but with the sorting of processes, according to the volume of their load on the processor.

kill is a command that kills a hung process or sends a signal to it.

sync is a command that forcibly writes the contents of the disk cache to the hard disk.

Example: we type the top command in the terminal, in the appeared list of processes we select the necessary, say, process 2200, (in my case, AppleSpell turned out to be completely unnecessary to me) press Q to exit the menu - and after kill 2200. Type the top again - and make sure that the 2200th process, mentioned more, 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 that are currently being accessed by application programs, for example, if you have a torrent client running, the command will show even the files you are currently downloading, 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 open network connections on your system.

Work with text

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

HDD

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

diskutil resizeVolume allows you to change the size of 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 disk.

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

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

sudo diskutil repairPermissions / another command to “repair permish” on the boot disk with your MacOS X. When it is executed, you will be asked to enter an administrator password.

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

reboot - restart computer

shutdown -h now is a command that shuts down your computer with no time lag and unnecessary questions.

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

Of course, few users use the capabilities of the terminal in everyday life. To some, this seems like 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 normal mouse. But, once you start working with the terminal, you yourself quite quickly appreciate the opportunities that it provides you.