2014-11-28 03:20 阅读 124


I have seen a comment about the unlink() function here

It says: The unlink() is not about removing file, it's about removing a file name. The manpage says: "unlink - delete a name and possibly the file it refers to."

I don't get it, will unlink() delete the files or will they be "invisible" and still waste space and performance????

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dongyanggan3025 dongyanggan3025 2014-11-28 03:48


    It basically means the space reserved for the said file is freed up for use within the system. All links to that file are removed, making the count 0. And 0 means no file, effectively freeing up the space.

    unlink() will remove the link (the name of the file you specify) to the data which is coherently stored as (and referred to as) an inode.

    When there are no more "links" to the data, the system automatically frees the associated space with that file. The number of links referenced to an inode are tracked right into the inode.

    You can see how many links reference to a file by running the command ls -l

    drwxr-xr-x    7 user      Administ     4096 Nov 25 07:46 app
    drwxr-xr-x    5 user      Administ        0 Nov 24 14:25 inc

    Where 7 and 5 are the respective links to the said file. Now when you run unlink() and remove the record from the directory (making the link count 0) allows the system to free up the related space.

    Why you ask? Well when the links are 0 it means there are no links to the said data, effectively meaning there is no more link to the data, so it can be freed. Allowing you to use that freed up space.

    This is exactly how hard linking and snapshots work.

    点赞 评论 复制链接分享
  • douliang9057 douliang9057 2014-11-28 03:25

    please refer: http://www.w3schools.com/php/func_filesystem_unlink.asp

    All content has been taken from that page.

    The unlink() function deletes a file.


    $file = "test.txt";
    if (!unlink($file)) {
        echo ("Error deleting $file");
    } else {
        echo ("Deleted $file");
    点赞 评论 复制链接分享