code_junkie

how do you normalize a file path in bash?

Nov 12th, 2011
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. // realpath.c: display the absolute path to a file or directory.
  2. // Adam Liss, August, 2007
  3. // This program is provided "as-is" to the public domain, without express or
  4. // implied warranty, for any non-profit use, provided this notice is maintained.
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <libgen.h>
  10. #include <limits.h>
  11.  
  12. static char *s_pMyName;
  13.  
  14. void usage(void);
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18. char
  19. sPath[PATH_MAX];
  20.  
  21.  
  22. s_pMyName = strdup(basename(argv[0]));
  23.  
  24. if (argc < 2)
  25. usage();
  26.  
  27. printf("%sn", realpath(argv[1], sPath));
  28. return 0;
  29. }
  30.  
  31. void usage(void)
  32. {
  33. fprintf(stderr, "usage: %s PATHn", s_pMyName);
  34. exit(1);
  35. }
  36.  
  37. $ dirname /foo/bar/baz
  38. /foo/bar
  39. $ basename /foo/bar/baz
  40. baz
  41. $ dirname $( dirname /foo/bar/baz ))
  42. /foo
  43. $ realpath ../foo
  44. ../foo: No such file or directory
  45. $ realpath /tmp/../tmp/../tmp
  46. /tmp
  47.  
  48. readlink -f /path/here/..
  49.  
  50. readlink -m /path/there/../../
  51.  
  52. realpath -s /path/here/../../
  53.  
  54. normalDir=`cd "${dirToNormalize}";pwd`
  55. echo ${normalDir}
  56.  
  57. MY_PATH=$(readlink -f "$0")
  58.  
  59. get_abs_path() {
  60. local PARENT_DIR=$(dirname "$1")
  61. cd "$PARENT_DIR"
  62. local ABS_PATH="$(pwd)"/"$(basename $1)"
  63. cd - >/dev/null
  64. echo $ABS_PATH
  65. }
  66.  
  67. python -c "import os,sys; print os.path.realpath(sys.argv[1])" /foo/bar/..
  68.  
  69. test -x /usr/bin/readlink || readlink () {
  70. echo $(/bin/ls -l $1 | /bin/cut -d'>' -f 2)
  71. }
  72.  
  73.  
  74. test -x /usr/bin/realpath || realpath () {
  75. local PATH=/bin:/usr/bin
  76. local inputpath=$1
  77. local changemade=1
  78. while [ $changemade -ne 0 ]
  79. do
  80. changemade=0
  81. local realpath=""
  82. local token=
  83. for token in ${inputpath//// }
  84. do
  85. case $token in
  86. ""|".") # noop
  87. ;;
  88. "..") # up one directory
  89. changemade=1
  90. realpath=$(dirname $realpath)
  91. ;;
  92. *)
  93. if [ -h $realpath/$token ]
  94. then
  95. changemade=1
  96. target=`readlink $realpath/$token`
  97. if [ "${target:0:1}" = '/' ]
  98. then
  99. realpath=$target
  100. else
  101. realpath="$realpath/$target"
  102. fi
  103. else
  104. realpath="$realpath/$token"
  105. fi
  106. ;;
  107. esac
  108. done
  109. inputpath=$realpath
  110. done
  111. echo $realpath
  112. }
  113.  
  114. mkdir -p /tmp/bar
  115. (cd /tmp ; ln -s /tmp/bar foo; ln -s ../.././usr /tmp/bar/link2usr)
  116. echo `realpath /tmp/foo`
  117.  
  118. pushd foo/bar/..
  119. dir=`pwd`
  120. popd
Add Comment
Please, Sign In to add comment