This is a command line tutorial for Mac OS

Mac OS

This is the operating system that runs on Macintosh computers. Mac OS is UNIX based with a Darwin Kernel and so the terminal lets you basically enter the commands directly into that UNIX environment.

 If you have used  Linux box before you might actually have experienced the exact same sort of commands on them as on Mac OS  

 Terminal command line in macOS

The Terminal is a gateway into the backend of Mac OS , it allows us to control mac using command prompt . 

In a UNIX based we have to work on command line interface. one of the main advantage of terminal is that - its is very fast , in order to use the terminal you should be aware of the basic commands and functions ,

How to Open Terminal

To open the terminal go to the applications folder and then go to utilities , there you can find Terminal , double click it.  or just click press Command - spacebar to launch Spotlight and type "Terminal" then hit enter.

there are a few things that you should keep in mind while typing the commands, every character matters, including spaces. So when you’re copying a command , make sure you include the spaces and that characters are in the correct case.

UNIX Directory Commands


1. pwd

In the console, you are always working in a directory, or folder, on your computer. We call this your working directory. You can see where you are using this command

2. ls

This command will show you all the files in that directory. and you can also view all the hidden files by this command "ls -a"  

3. cd

This command will take you to a directory ,
suppose right now we are in the home folder and you want to go to Desktop,  so just type  
" cd Desktop/ "    

here we use "\  " for space , suppose you want to open a file named "Mac book" from Desktop we should type                     "cd Desktop/Mac\ book/"

now you are in the folder named Mac book , but you want to come back to desktop , here we use " cd .. " , This command will bring you back one directory  and typing " cd  " will take you back to Desktop directly  

4. mkdir

This command is used for making a directory . to make a directory you should type the directory name also for example "mkdir Mac",  this will create a new directory with name Mac

5. rmdir

This command is used to remover an empty directory . to remove a directory name Mac we use this command "rmdir Mac"

Working With Files

1. rm

"rm" command is used to remove the directory and "rm -r" is used to remove the directory with files. If you want to confirm the file to be deleted, use " -i " as in                             " rm -i  scr.png  " (works only with files )

2. chmod

Changes permissions for access to a file. now lets see how to modify permissions with chmod. For total control over permissions, you can use two Unix commands "- ls" and "chmod -"to display permissions and modify them. Assume you want to find a folder’s current permissions and then change them to 755. This would give you as the owner read, write and execute permissions, and everyone else read and execute permissions.

Type ls –l, and then press enter. The symbolic permissions of the files and folders in your home directory are displayed 

Type "chmod 755 foldername", and then press enter. This changes the permissions of the folder to rwxr-xr-x .

3. diff

Compares two files line by line. It might not be the most elegant tool, but it's definitely a solid one that does the job of comparing & merging text. the command is like            "diff  [firstfile]  [secondfile]"

4. more

is a command to view  the contents of a text file one screen.Press the spacebar to see the next page and  press Q to quit. The command is like "more [filename]"

5. mv 

This command can move one file into a directory by the command "mv [filename]  [foldername]"and it can also move the contents in a text file to another text file by the command "mv [1st filename] [2nd filename]"

Here the text file has been moved into the folder named hari

Here the content in the first file has been moved to the second file and the content in the second file will be removed

Miscellaneous Commands


1. ctrl+C & ctrl+z

ctrl+c is used to terminate most of the operations and ctrl+z is for force stop , we use this command mostly when we run  python , java or other programs.

2. date

It displays the date and time

3. echo

Its a command used to output the text to a file. suppose you want to type " hello world " and save it in a text file, just type this command " echo hello world >> name.txt "

4. help

Displays a partial list of bash commands. for this simply type "help" in the terminal

5. history

Displays the last commands you typed. You can redo a command by typing an exclamation point (!) followed immediately (no space) by the number of that command in the history list. To repeat the last command, type " !! ". To repeat the last filename, type " !* "

6. pico

A simple UNIX text editor. simply type the command "pico" and hit enter, the text editor will appear 

8. zip and unzip

These commands are used to compress to zip archive and to extract from zip archive

9. sudo

To execute a sudo command , you must be logged in with an administrator account that has a password.

you can use the sudo command in the Terminal to execute commands as a different user, such as the root user. After you enter the command, Terminal asks you to enter your account password. If you forgot your password or your account doesn't have a password, add or change your password in Users & Groups preferences. You can then execute sudo commands in Terminal.

10. ping

Using this command yo can check the connection to a service , for example "ping maker.pro "

3. hostname

This command will display your hostname. you can also change  your hostname by this command "sudo scutil --set HostName [your host name]"

Hari Krishnan