difference between unlink and rm

Posted on 2004-08-20
Medium Priority
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 150 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 150 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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 find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses

762 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