Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
add comment

4 Answers

up vote 1 down vote accepted

Actually, it is realpath: http://linux.about.com/library/cmd/blcmdl3%5Frealpath.htm

There is also a command-line version: cyberciti.biz/faq/unix-linux-bsd-find-real-physical-path/

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

Also there is a "cheating way":

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

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

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

I believe this is called 'canonicalizing'.

share|improve this answer
1  
-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
add comment

Are ou asking for the basename command ?

$ basename ../../.bashrc
.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
add comment

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
add comment

Your Answer

 
discard

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.