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 may well 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 the operating system he chooses.

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's 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 their 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.

Basics of Navigation

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 up 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 enumerating 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'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 in a text file all the characters you typed from the keyboard. 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 - list of all folders available on your system partition.

Copying

Copying files and folders is done using the cp command. Suppose you want to copy the note.txt file from the desktop to the Documents directory. In this case, you will need to dial 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 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 file note.txt 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 whole directory / Users (including all home folders inside), while keeping 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 the 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 whole directory under the name "Documents" and names a copy of the directory "Documents backup".

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 with 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) 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 prints out 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 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, select the required, say, process 2200, (in my case it turned out to be completely 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 must 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 shell commands are running. The program, which deserves a separate review, so here we will not dwell in detail 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 partition name 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, while 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.