Terminal Commands Everyone Should Know
Terminal Commands Everyone Should Know
Anonim
Terminal Commands Everyone Should Know
Terminal Commands Everyone Should Know
terminal-icon
terminal-icon

Despite the ubiquity of graphical interfaces and all sorts of convenient manipulators like Magic Mouse and Magic Trackpad, the Terminal.app application is still quite a useful and slightly "geeky" tool for performing some operations, with which most of us are rather superficially familiar. In fact, there is nothing difficult in "taming" it, therefore, for all readers of MacRadar, I have prepared commands for the Terminal that may be useful.

For starters, it's worth saying that the Terminal.app application itself, like all useful utility utilities, is located in the Programs> Utilities directory. Immediately after launching it, you will see a line in which the name of the computer and the current directory is written, and after the dollar sign ($) - a welcoming blinking cursor that invites you to enter your first command …

terminal
terminal

List of files and folders - ls

By default, after starting the utility, the user's home folder is selected as the working directory. In order to get a list of files and folders in it, just run the two-letter command

ls

terminal-ls
terminal-ls

There are several variations of this command, which are called by different keys (in fact, there are many more):

  • ls -l

  • will display detailed information about each item, including permissions, file sizes, etc. In addition, the first character of each line will allow us to determine the data type: if it is the Latin letter "d", then we have a directory, and if a hyphen (-), then a regular file.
  • ls -a

  • will display a list of all files and directories in the current folder, including hidden items (names that start with a dot in Mac OS X).

Note to the hostess: the keys can be easily combined with each other, so if you want to view detailed data about all elements of the directory, including hidden files, then your command should look like this:

ls -la

term-ls-la
term-ls-la

Moving between directories - cd

The name of this command does not come from the old CDs that Apple successfully killed with its digital iTunes Store, but from the two English words "change directory" - which is what it does:

  • Write

    cd Music

  • - and the Terminal will go to the Music folder, if, of course, it exists in your current directory.
  • To exit to a folder one level higher, use the command

    cd..

  • (with two dots).
  • And by executing the cd command, you will instantly be transferred to the user's home directory.
term-cd
term-cd

Full path to working directory - pwd

The name of this command also comes from the first letters of the phrase "print working directory". Whichever directory you are in, this command will result in the full path to the current directory.

term-pwd
term-pwd

Create directory - mkdir

This command creates a directory with the specified name. For example,

mkdir Temp

will create a Temp directory in the current folder. If you have forgotten which directory you are currently in, use

pwd

:

term-mkdir
term-mkdir

Removing files and directories - rm, rmdir

During this action on files and directories through the Terminal, you need to take into account a small but important nuance: there is no Recycle Bin, so the data is permanently deleted.

But in general the team

rm test1.txt

will remove the file test1.txt from the current directory. And if you add, say, the key "i" (

rm -i test2.txt

) the user will be prompted to confirm the deletion.

term-rm-files
term-rm-files

Unfortunately, deleting directories is a more difficult task, because there are several types of commands for this action:

  • rmdir Test

  • will delete the Test directory only if it is empty and does not contain any subfolders or files inside it.
  • rm -r Test2

  • recursively deletes all files and folders inside Test2, and deletes it at the very end.
term-rmdir
term-rmdir

Moving and copying files - mv and cp

The two commands perform almost the same actions, therefore the list of parameters is the same for them. If I want to move a file from one directory to another, then I need to use the following command:

mv ~ / test1.txt ~ / Documents / test1.txt

The first parameter (~ / test1.txt) is the file that we need to move, and the second is the destination directory and the resulting file name.

And again to the mistress of the note. First, as you may have already guessed, it is not necessary to navigate to the correct directory to perform actions on files. It is enough to know its name, full path and use them as command parameters.

Secondly, to avoid writing the path to the home directory every time, it is enough to use the tilde (~). For example, the entry

~ / Documents

tantamount to

/ Users / username / Documents

To copy files, just replace

mv

on

cp

:

term-mv-cp
term-mv-cp

The rest of the teams

Naturally, it is not possible to describe all the available commands (as well as their keys), so I will focus on a few more interesting and useful "instances":

  • You can run any application on the system using the command

    open

    specifying the path to the application and its name separated by a space. For example,

    open /Applications/Airfoil.app

  • Using the command

    who

  • you can see a list of users who are logged into the system. Naturally, this is only relevant if the work on this computer involves the login of several users or active SSH.
  • Command

    cat test.txt

  • will allow you to display the contents of a file named test.txt in a Terminal window, but you cannot edit it (you will need to use a text editor like nano, vim, or emacs to do this), just view it.
  • You can always cancel the process of executing any command (be it ping or the work of a shell script) using the universal keyboard shortcut

    Control + C

  • .
  • It is quite easy to clear the Terminal window of a large amount of text by running the command

    clear

  • .
  • You may need administrator privileges to run some commands. The fastest way to do this is by adding the command at the beginning of the line

    sudo

  • … In this case, the Terminal will ask you to enter the password for your account.
  • You can view the list of running processes using the command

    top

  • , however, in this case the data will look less clear than in System Monitoring.
  • To display detailed help for a command, you can use

    man

  • , specifying the name of the command of interest separated by a space.
  • I almost forgot to mention a fantastic search utility

    grep

    with which you can find a text string in a file or, say, a specific process in a process list (for example,

    ps ax | grep smbd

  • ).

Perhaps we will stop at this, and if I missed something or indicated it incorrectly - you are welcome in the comments.

Recommended: