Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Linux Ask!

Jul 232012
 

How to get absolute path in Python?

Answer:

To return the absolute path when you provide shortcut path such as ".", e.g.

import os
print os.path.abspath(".") # print the full path of ., depending where you execute the program
Jul 172012
 

Create hard link in Linux

Answer:

To create a hard link in Linux, use the ln command, e.g.

# echo "test" > test.txt
# ln test.txt hard_link.txt

To show they are pointing to the same thing, we can check the inode, e.g.

# ls -i
8232976 hard_link.txt  8232976 test.txt
Jul 152012
 

How to recover a deleted file (but still being opened) in Linux?

Answer:

If you have a file which is deleted but is still opening in another program, it is possible to recover it.

Let do a quick experiment:

# echo "hello" > /tmp/test.txt
# less /tmp/test.txt # Open this file

Now, in another terminal, we delete it

# rm /tmp/test.txt

To recover it, we need to find the pid of the program (less in our example) which is opening this file

# ps ax | grep less
31314 pts/0    S+     0:00 less test

Then we list the file descriptor being used by this program

# ls -l /proc/31314/fd

lr-x------ 1 joe joe 64 2012-07-05 08:56 4 -> /tmp/test (deleted)

Finally we can easily recover the content by

# cp /proc/31314/fd/4 /tmp/test.txt