Terminal Commands for OS X – Basic

This is the first part of the Terminal Commands topic. This article covers the basic commands which a user can use on the terminal.
To launch the terminal application simply navigate to /Applications/Utilities/Terminal. Alternatively, you can even search for the same using spotlight.

cd
This command is used to change the current directory we are. So if we want to navigate to a new folder we simply run the cd command.
Syntax:  cd <folder path>
Note that the folder path has to be the absolute path of the folder. Beginning from root. If you simply place the name for the folder, the OS is going to search for that folder within the current directory. The command won’t work if it doesn’t find the specified folder.

ls
This command is used to list the contents of the current directory.
Syntax: ls or ls <folder name>
There are a lot of switch based options available to get more detailed results.
-a shows hidden files
-l shows in a list format with more information
-r oldest entries first
-t most recently modified entries first
View the manual for more switches.

rm
This is the remove command. This command is used to remove one or more than one files &/or folders from the specified directory.
Syntax: rm <folder name>
To remove folders we need to use the -r switch.
NOTE: The rm command should be run with caution. Running the above command does not move the contents to trash. There is no way to undo the rm command. Also care must be given to the folder from which the command is run, make sure you are in the folder you wish to be in before running this command.

pwd
Present Working Directory. This command tells us the directory we are currently in.
Syntax: pwd

cp
This command is used to copy the specified file or folder to a specified location.
Syntax: cp <source file> <destination folder>
In order to copy folders we need to use the -r switch.

mv
This command is used to move files or folders to a specified location.
Syntax: mv <source file/folder> <destination folder>
This command can also be used to rename files & folders by giving a different name but keeping the destination the same. The destination folder is replaced with new name.

less
This command is used to display the contents of a file.
Syntax: less <name of file>
This command displays the contents of a file in simple text. It displays the content in a page by page format. To stop viewing the file simply type ‘q’.

chmod
This command is used to modify the ACLs of a specified file or folder.
Syntax: chmod <rwx-rwx-rwx> <file or folder name>
The rwx indicate read-write-execute permissions for the owner-group-everyone else. The permissions are given by either indicating 1 or 0 in place of each rwx. 1 indicating true, 0 indicating false. The resulting binary numbers should be replaced by their octal equivalents. So to give only the owner rwx permission on a file the command would need 111-000-000 which is 700, so the command would look like chmod 700 <file/folder name>. If we wanted to give rw permission to owner & r permission for group & everyone else then the command would look like 110-100-100 or 644, chmod 644 <file/folder name>.

mkdir
This command is used to create a directory in the current working directory.
Syntax: mkdir <directory/folder name>
This will create a directory in the present working directory. Do keep track of the current directory before creating a new one, to make sure you are creating it in the correct directory.

sudo
Super User Do.
Syntax: sudo <command to be executed by super user>
This command is used as a prefix before other commands when we want those commands to be executed as a super user (aka root user). You are required to authenticate as an administrator for the command to work. NOTE: Do exercise caution while using the sudo command. Root users don’t have the built is permission checks & security checks. Improper usage of sudo may leave your machine in an unusable state.

man
Terminal command manual.
Syntax: man <terminal command>
This command is used to open the manual for the different commands that are available within the system. It is a good starting point to understand the full functionality of the different commands along with examples of usage.

cat
The cat command is used to perform 3 different tasks: Display text files, copy text files, combine text files, create new text files.
Display Text Files
Syntax: cat <file name>
This will display the text files on the screen.

Copy Text Files
Syntax: cat <first file name> > <second file name>
This will copy the contents of the first file to the second file using the redirect ‘>’ operator.

Combine Text Files
Syntax: cat <first file name> <second file name> > <third file name>
This will combine the contents of the 2 files & save it in the third file.

Create New Files
Syntax: cat > <filename>
This will create a new file.

echo
Display given sentence on the screen.
Syntax: echo <sentence>

grep
Used to find a particular string within a file
Syntax: grep <string pattern> <file name>
This will look for the given string pattern in the specified file. You can use different switch options to modify your result. View the man pages for the same.

find
The find command is used to find files within the system.
Syntax: find <search path> <search criteria> <name of the file> <operation to perform>
Search path – specifies the folder where we want to search
Search Criteria – specifies what to search eg: name
Name of File – name of file to search
Operation to perform – how to show the results

Advertisement

11 thoughts on “Terminal Commands for OS X – Basic

  1. Pingback: Terminal Commands for OS X – Part 2 | Arun Patwardhan's Blog

  2. Pingback: Terminal Commands – Part 3 | Arun Patwardhan's Blog

  3. Pingback: Configuring an SVN Server on OS X on your Local Network | Arun Patwardhan's Blog

  4. Pingback: Configuring/Troubleshooting OS X Using Command Line | Arun Patwardhan's Blog

  5. Pingback: Automation on the Mac | Arun Patwardhan's Blog

  6. Pingback: UPDATE: Swift on Linux | Arun Patwardhan's Blog

  7. Pingback: Automation on the Mac | Arun Patwardhan's Blog

  8. Pingback: Useful scripts for macOS | Arun Patwardhan's Blog

  9. Pingback: List of macOS Terminal commands | Arun Patwardhan's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s