Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a command line tool in linux that will 'normalize' a filename? i.e. remove all "xx/./xx" parts, or "myfolder/../myotherfolder" parts?

share|improve this question
I don't get what you are asking myself.. but could just be me :-) – Kyle Brandt Sep 15 '09 at 14:44
up vote 1 down vote accepted

Actually, it is realpath:

There is also a command-line version:

But on my CentOS 5 it is not available by default.

Also there is a "cheating way":

$ bash -c "cd /foo/../bar/ ; pwd"
share|improve this answer
or just (cd /foo/../bar && pwd) – reinierpost Sep 16 '09 at 11:09

readlink -f seems to do it (despite being intended for de-referencing symlinks):

[root@noldevvg19 ~]# readlink -f /etc/../usr/../etc/./sysconfig/../redhat-release 

I believe this is called 'canonicalizing'.

share|improve this answer
-f: all but the last component must exist, -e: all components must exist, -m: no requirement for existence – Dennis Williamson Sep 15 '09 at 16:26
readlink is not guaranteed to be on a given system, that might be an issue – Thorbjørn Ravn Andersen Sep 15 '09 at 22:52

Are ou asking for the basename command ?

$ basename ../../.bashrc

And there is also the other part : dirname

$ dirname ../../.bashrc
share|improve this answer
They are related, but they are not the answer. One reason they aren't is that they just take one step; another is that they just rewrite the strings without looking at the file system, which may go wrong in the presence of symlinks. – reinierpost Sep 16 '09 at 11:07

With python line command:

python -c 'import os; print os.path.basename("/home/mezgani/nat")'
python -c 'import os; print os.path.direname("/home/mezgani/nat")'

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.