8 Terminal Commands That Every Mac User Should Know - WindowsTips.net - Windows Tips and Tricks with Geek

Sunday, February 27, 2022

8 Terminal Commands That Every Mac User Should Know

 A backlight MacBook keyboard.

Use Flags to Modify Commands

Most commands can be appended with a flag in the form of a hyphen and a letter to access different functions. For example, the -R flag applies a command recursively so that it applies to a directory, all files and folders within that particular directory, all files and folders inside those folders, and so on.

The flag always appears after the command. For example: rm -i <location>. In this example, rm is the delete command, the -i flag instructs the process to ask for user confirmation, and <location> would be replaced with the file or folder’s location on the drive. Flags are case-sensitive.

Change Directory: cd

Example usage: cd /folder/

Use the cd command to change directories. For example: cd /Volumes/Elements/ to access an external drive called “Elements.”

cd macOS Terminal Command

You can use shortcuts to quickly skip to certain directories. For example, running cd ~ will take you to the Home directory for the current user. You can also use cd/ to get to the root of the drive, cd.. to move up one directory, or cd../.. to move up two directories.

List Files & Folders: ls

Example usage: ls /folder/

Also useful in navigating your drive, ls can be used to list the contents of the current directory simply by executing the command. Append it with a location on the drive to specifically target that directory.

You can add flags to the ls command to get different results. For example, use -C to get a multi-column output, -S to sort by size, -lt to sort by date modified, -la for detailed contents including hidden files, or -lh to create a list with readable file sizes.

ls macOS Terminal Command

Remember, you can also use the same location shortcuts that you’d use with the cd command (e.g. ls ~ ) to quickly jump around.

Copy: cp

Example usage: cp file.txt /destination/

Use cp to initiate the copy command, add a flag where required, and then enter the target file or folder, followed by a space, and then add the destination folder.

cp macOS Terminal Command

If you’re copying a single file, you can use the cp command without a flag as per the example shown above. If you want to copy a directory, you’ll need to use the -R flag to indicate that all files and folders in the directory are to be included. For example: cp -R /folder/ /destination/ .

You can even include multiple files in a single copy command. For example: cp file1.txt file2.txt file3.txt /destination/.

Move and Rename: mv

Example usage:mv file.txt /destination/

Move works almost identically to copy as shown above, except that there’s no need to add a recursive flag when moving directories. You can add an -i flag to the command to require confirmation before moving since the mv command will overwrite any files in the destination by default.

mv macOS Terminal Command

You can use mv to rename files too by “moving” a file to the same directory. For example: mv oldfilename.txt newfilename.txt.

Make a New Directory: mkdir

Example usage:mkdir <name>

If you want to create a new directory, use the mkdir command, followed by the name of the directory that you want to create. You can create multiple directories by separating the names with spaces. For example: mkdir folder1 folder2 folder3.

mkdir macOS Terminal Command

If you want to create a folder with a space in the name, make sure that you put the folder name in quotations. For example, mkdir "my folder".

Delete Files & Folders: rm

Example usage: rm <file>

The rm command deletes files or folders immediately without asking you for confirmation first. You can add the -i flag to require user confirmation whenever using it, which should help prevent mishaps.

rm macOS Terminal Command

You can delete multiple files at once by appending more filenames to the end of the command. For example: rm file1.txt file2.txt file3.txt.

Display Disk Usage & Free Space: du & df

Example usage: du /destination/

Use the du command to calculate disk usage in the location specified thereafter. For a far more useful readout, run du -sh /destination/ instead to provide a human-readable total of disk usage for a specified location.

du macOS Terminal Command

Similarly, you can use df -h to calculate disk space, or use the -H flag to display total disk space in “metric” storage units (e.g. 1000MB per GB as opposed to 1024MB per GB).

No comments:

Post a Comment