difference between unlink and rm

Posted on 2004-08-20
Last Modified: 2013-12-27
     I have a normal file which is NOT a soft/hard link. When I say unlink <file name> the file gets removed. Can somebody explain this behaviour? Since the file gets removed is it equalent to rm <file name>.
     I'm writing a bourne shell script where I have check for only the softlinks and remove it. How do I do it?
Thanx in advance,
- Jacob
Question by:jacobselvin
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 38

Accepted Solution

yuzh earned 50 total points
ID: 11850059
    link  and  unlink  directly invoke the link(2) and unlink(2)
     system  calls,  performing  exactly what they are told to do
     and abandoning all error checking.  This  differs  from  the
     ln(1) command. See ln(1).

     While linked files and  directories  can  be  removed  using
     unlink  , it is safer to use rm(1) and rmdir(1) instead. See
     rm(1) and  rmdir(1).

man unlink
to learn more details.

    To find all the symbolic link under the current dir, you can do:
    find . -type l -print
    PS: it is type L,a  lower case L

    To check if the file is a symbolic link, you do:

    if [ -L filename ] ; then
      echo "filename is a symbolic link"

LVL 11

Expert Comment

ID: 11852049
if you unlink a file both the pointer and destination still exist except you can't see them

for example if /opt/softwareinstall is linked to /extradiskspace/app1

when you cd to  /opt/softwareinstall you will see everything in  /extradiskspace/app1  

if you remove this link then you will have to go to  /extradiskspace/app1  to see the same files and /opt/softwareinstall will be empty

Be very careful when using unlink if there are dependencies and you unlink you could run into major problems
LVL 14

Expert Comment

ID: 11854698
Think of unlink as the "raw" version of rm - rm gives you more control over things like properly removing directories, recurive directory removal, error-checking, etc.
LVL 10

Assisted Solution

Nukfror earned 50 total points
ID: 11856874
As already mentioned, rm and unlink do basically the same thing but rm gives you more control.

At the file system level, both unlink and rm do basically the same thing.  Look at the below:

soneill@linux:~> ls -la
total 204691
drwxr-xr-x  123 soneill  users        9000 2004-08-20 17:38 .
drwxr-xr-x    5 root     root          120 2004-07-02 15:25 ..
-rw-r--r--    1 soneill  users      298773 2004-05-27 17:37 9960_What _works_with_what.pdf
drwxr-xr-x    2 soneill  users          72 2004-01-17 14:57 .acrobat
drwx------    2 soneill  users         184 2004-06-26 11:00 .adobe

Specifically look at the second column.  Notice for the 9960_What.....pdf file the value is 1.  That is the link count.  The link count shows you how many times a specific inode has been referenced by an directory entry.   If you create a hard link to a file like this:

soneill@linux:~> ln 9960_What\ _works_with_what.pdf sean.pdf
soneill@linux:~> ls -li 9960_What\ _works_with_what.pdf sean.pdf
 193343 -rw-r--r--    2 soneill  users      298773 2004-05-27 17:37 9960_What _works_with_what.pdf
 193343 -rw-r--r--    2 soneill  users      298773 2004-05-27 17:37 sean.pdf

Notice I added the -i switch to the ls command which prints the inode number as the first column.  The link count column is now in the third position.  Notice the inode numbers are the same and link count is now 2.  If I remove sean.pdf:

soneill@linux:~> rm sean.pdf    # "unlink sean.pdf" would do the exact same thing
soneill@linux:~> ls -li 9960_What\ _works_with_what.pdf sean.pdf
/bin/ls: sean.pdf: No such file or directory
 193343 -rw-r--r--    1 soneill  users      298773 2004-05-27 17:37 9960_What _works_with_what.pdf

Notice the link count for the 9960.....pdf file went back to 1.  If I were to do a remove on 9960....pdf, the inode link count would goto 0 which "removes" file.  Only when the link count drops to 0 is a file deleted.

Actually to get really specific about it, the filenames you see in a ls listing are nothing more then references that POINT to a specific inode.  The inode structure actually holds a small amount of the file's content.  The rest of the file is traversed using a rather crazy reference scheme - but it works.  So think of the inode as "the file".  But an inode is simply a number in an "ls" listing.  The pretty-eye-catching name you see in a directory listing is nothing more then a way for us stupid humans to know what a specific inode is for because we assign meaning to that name.

Then there are ways to play with inodes:

- Hard links - this simply pointing a new directory reference to an already existing inode.  Basically what I did above in my example.  Hard links must point to inodes that exist in the same file system

- Soft Links - This is a directory entry that point to another directory entry (be it in same file system or another file system).  So basically:

directory entry -> directory entry -> ..... (directory entry) -> inode

Remember, a directory entry is nothing more then a name and it contains no data of any file.  So its pretty easy to make a stale or broken symbolic link cuz the system doesn't check to see if the directory entry you are pointing at really exists.  For hard links, it always checks so you can't create a broken hard link.

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question