Delete files and folders.

     rm [options] file ...

     unlink file

   -d   Attempt to remove folders.

   -f   Attempt to remove the files without prompting for confirmation,
        regardless of the file’s permissions.  If the file does not exist,
        do not display a diagnostic message or modify the exit status
        to reflect an error.

   -i   Request confirmation before attempting to remove each file.

   -P   Overwrite regular files before deleting them.  Files are
        overwritten three times, first with the byte pattern 0xff,
        then 0x00, and then 0xff again, before they are deleted.

   -R   Remove the entire file hierarchy rooted in each file argument.
        (implies the -d  Delete folders).
        If the -i option is specified, the user is prompted for confirmation
        before each directory’s contents are processed (as well as before
        the attempt is made to remove the directory).  If the user does not
        respond affirmatively, the file hierarchy rooted in that directory
        is skipped.

   -r   Equivalent to -R.

   -v   Be verbose when deleting files, showing them as they are removed.

   -W   Attempt to undelete the named files.  Currently, this option
        can only be used to recover files covered by whiteouts.

   unlink   Unlink a file, only one argument, which must not be a directory
        can be supplied. Calls the unlink() function which removes the link
        named by path from its directory and decrements the link count of
        the file which was referenced by the link.

The rm utility removes symbolic links, not the files referenced by the links.

Expanding variables

If you are expanding a variable always put quotes around the filename in case it contains spaces:

rm "$filename"

Also consider the case where the $variable has not been set: rm -rf /$variable is not a good idea.

Undeletable files

To delete a file with non-printable characters in the name: 'bad file name' Use the shell wildcard "?" for each character

rm bad?file?name

If an error occurs, rm exits with a value >0.


Delete all .jpg files in the current folder:

$ rm *.jpg

Delete the folder named "temp", and all its contents:

$ rm -R temp

Delete an old iPhone update file, normally these are automatically deleted:

$ rm "~/Library/iTunes/iPhone Software Updates/iPhone3_17.2.1_21C66_Restore.ipsw"

Delete a protected folder:

you will be prompted for your password. hit return.

Move the file Hunter.txt to the Trash using mv. The tilde ~ indicates that the .Trash folder is located in the users home:

$ mv Hunter.txt ~/.Trash

“You can’t delete racism. It’s like a cigarette. You can’t stop smoking if you don’t want to, and you can’t stop racism if people don’t want to. But I’ll do everything I can to help” ~ Mario Balotelli

Related macOS commands

cp - Copy files.
find - Find and optionally Delete files.
ls - List information about files.
mv - Move files and/or folders.
rmdir - Remove empty folder.
srm - Securely remove files or directories.
trimforce - Enable TRIM commands on third-party drives.
HT1526 - You can’t empty the Trash or move a file to the Trash.
HT201583- You can’t empty the Trash or move a file to the Trash.

Copyright © 1999-2024 SS64.com
Some rights reserved