blog posts

Terminal Commands

16 Essential Terminal Commands That Mac Users Should Be Aware Of

If You Are Afraid Of The Mac Command Line, Learning A Few Simple Commands Can Help Alleviate That Fear. Familiarity With The Command-Line, You May Find That You Enjoy Working In This Environment!

In the utility section of the Mac Application page, you can find the terminal application and execute commands using the command line. To get acquainted with the terminal environment, go to Applications> Utilities and run it; You can also find it by searching Terminal in Spotlight.

Many of the shortcut keys you use in other apps also work in the terminal environment. For example, you can open multiple tabs with the Command + T shortcut keys. Use the Command + N shortcut keys to open a new terminal window. Cut, copy and paste commands can also be done with the same shortcut buttons as usual. You can also drag any file or folder into the terminal window to be redirected to that directory immediately.

In this article, we cover some of the most basic Mac terminal commands. If you have used the Linux command line, these commands will be familiar to you. After reading this article, you can lock or disable your Mac with a simple command on the command line.

Use Flag‌ to change commands.

Most commands can be appended with a flag as a space and a letter to access various functions. For example, the R- flag executes a command in reverse to execute that command in a directory and all the files and folders in that particular directory and all the files and folders in those folders, and so on.

The flag always appears after the command; For example, look at this command:

 <rm -i <location

In this command:

  •  rm is a remove command.
  • Flag i- instructs the process to request user authentication.
  •  In the <location> section, the file or folder on the drive is located.

Therefore, the selected folder will delete after the user confirms; But if there were no flags, the folder would be deleted automatically. Note that flags are case-sensitive.

Change directory: cd

 / cd / folder

You must use the cd command  (Change Directory) to change the directory. For example, the / cd / Volumes / Elements  command is used to access the external Elements drive   .

You can use shortcuts to navigate to specific directories quickly. For example, executing the  ~ cd command will take you to the current user’s home directory. You can use the / cd command to go to the root directory. To reverse the current directory to the directory before it, the order ..cd and return to the two levels of command  ../..cd use. 

List of files and folders: ls

 / ls / folder

The ls command, which is also used to navigate drives, can list the contents of the current directory. You can add flags to the ls command to get different results. For example, using the C- flag,  you can see the output in several columns. The  S -command displays the output by size and the lt- output command by the creation date. Use the la-  command to display the full contents (including hidden files)  and the LH- command to create a list of readable file sizes.

Remember that you can use the same location shortcuts with the cd command (e.g., s ls).

Copy: cp

 / cp file.txt / destination

 

Use cp to execute the copy command. If necessary, add a flag and then enter the desired file or folder, space and finally add the destination folder.

If you want to copy only one file, you can use the flagless cp command as the example above. And If you want to copy a directory, you must use the R- flag   to indicate that you must copy all files and folders in the directory:

 /cp -R / folder / / destination

You can put multiple files in one copy:

 / cp file1.txt file2.txt file3.txt / destination

Move and rename: mv

 / mv file.txt / destination

The Move command is almost identical to the copy command; The difference is that there is no need to add an R-return flag when transferring directories. You can add an i- flag to the command to get user approval before moving the directory; Because the mv command overwrites all the files in the destination by default.

You can rename a file by moving it within the same directory using the mv command   :

 mv oldfilename.txt newfilename.txt

Creating a new directory: mkdir

 <mkdir <name

If you want to create a new directory, use the mkdir command, followed by the name of the directory you want to create. You can create multiple directories by separating directory names with Space:

 mkdir folder1 folder2 folder3

 

If you want to have a space in your folder name, you must put the folder name inside ( ); For example, “mkdir” my folder

Delete files and folders: rm

 <rm <file

 

The rm command immediately deletes files and folders without your confirmation. You can use the i- flag to prevent an accident and ask the user for confirmation before deleting the file.

You can delete multiple files at once by adding filenames to the end of the command:

 rm file1.txt file2.txt file3.txt

Disk Usage and free space display: du and df

 / you / destination

You can use the du command to calculate the amount of disk usage in the location you specify. For a more useful read, run the / du -sh / destination command so that the output results are readable to you.

You can use the df -h command to calculate disk space or the H- flag to display the entire disk space with metric storage units.

Find the file: find

 <find / location / -name <file

The find command can help you find files on disk. After the find command, enter the location of the directory you want to search, the name- and then the file name.

You can always use the * sign to search for files of the same type; For example, ‘find / location / -name’ * .png  All files with a PNG extension. Are searching in the specified location.

Open file: open

 <open <file

You can use the open command to open files or directories. You can open multiple directories or files by naming them at the end of the command; For example,  open file1.txt file2.txt file3.txt  will open three text files simultaneously.

You can open files in specific applications. This requires the flag a-  use, followed by the program name or file path APP. Write:

 open -a Preview file.pdf

File editing: nano

 <nano <file

nano is a simple, open-source text editor used to edit files in the macOS terminal. You can edit text files, including system files, using the nano command that follows the file name.

When entering the nano, pay attention to the commands at the bottom of the screen used with the control key. Press Control + O  or use the Control + X keys to leave the nano environment without saving the file to save the file.

Super User:  sudo

 <sudo <command

The sudo prefix is ​​used to execute the command as superuser or root, or admin. After entering a command with the prefix sudo, you must enter your password to execute it.

Some commands require root access to run. For example, if you want to edit a file system, you may need to use the <sudo nano <file command to save the changes. Use the sudo shutdown -h now command to turn off the Mac immediately. For example, if you want the Mac to shut down 30 seconds after executing the command, enter the command as sudo shutdown -h +30.

Show current directory: PWD.

You can use the PWD (print working directory) command to display the current directory. This command is used to print the path that you need to copy and paste later.

Show running processes:  top.

Use the top command to see a list of running processes and the CPU and memory. This command shows the processes based on CPU usage by default. There is a PID or process id next to each entry.

When you are done, press the Q button to return to the command line.

Terminate a process: kill

<kill <PID

To terminate a process, you must first run the top command to obtain the process PID. You can then terminate the process with the kill command followed by writing the process PID; For example, the kill 1569 command will terminate the process whose ID is 1569.

More information about the command: man

  <man <command 

Each instruction mentioned in this list has explanations for further guidance on using it and the function of the various flags next to it, along with a few examples.

For example, the command top has many flags that you can find out with the top command. If you want to become proficient in using the command line and its commands, it is important to read the command line.

***

Knowing these commands is very useful in times of need. For example, suppose you cannot run Finder to copy a file from a Mac drive in recovery mode. Still, if you are familiar with simple terminal commands, you can copy files manually in the terminal environment.