Home > bash, python > Show the absolute path of a file

Show the absolute path of a file

When working in a terminal, a common task is to get the absolute path of a file. For instance, you want to open the given file with another (already running) application.

Old method:

Well, to get the absolute path of a file, I was doing the following:

C:\usr\local\lib\python2.6\dist-packages> ls -al
total 32
drwxrwsr-x  4 root staff 4096 2011-03-16 14:41 .
drwxrwsr-x  4 root staff 4096 2010-10-25 21:22 ..
drwx--S--- 10 root staff 4096 2010-09-29 09:30 pyglet
-rw-------  1 root staff 1039 2010-09-29 09:30 pyglet-1.1.4.egg-info
drwx--S---  2 root staff 4096 2011-03-16 14:41 termcolor-1.1.0.egg-info
-rw-r--r--  1 root staff 5044 2011-03-16 14:41 termcolor.py
-rw-------  1 root staff 3672 2011-03-16 14:41 termcolor.pyc
C:\usr\local\lib\python2.6\dist-packages> pwd
/usr/local/lib/python2.6/dist-packages
C:\usr\local\lib\python2.6\dist-packages>

First, find the file (termcolor.py in this example). Second, get the current working directory with the pwd command. Third, copy and paste the current working directory. Finally, copy and paste the filename. At each copy/paste, I had to switch between the terminal and the other application where I needed the absolute path. Too much work…

New method:

C:\usr\local\lib\python2.6\dist-packages> ls -al
total 32
drwxrwsr-x  4 root staff 4096 2011-03-16 14:41 .
drwxrwsr-x  4 root staff 4096 2010-10-25 21:22 ..
drwx--S--- 10 root staff 4096 2010-09-29 09:30 pyglet
-rw-------  1 root staff 1039 2010-09-29 09:30 pyglet-1.1.4.egg-info
drwx--S---  2 root staff 4096 2011-03-16 14:41 termcolor-1.1.0.egg-info
-rw-r--r--  1 root staff 5044 2011-03-16 14:41 termcolor.py
-rw-------  1 root staff 3672 2011-03-16 14:41 termcolor.pyc
C:\usr\local\lib\python2.6\dist-packages> sp termcolor.py
/usr/local/lib/python2.6/dist-packages/termcolor.py
C:\usr\local\lib\python2.6\dist-packages> sp termcolor.py | tocb
C:\usr\local\lib\python2.6\dist-packages>

The script “sp” (see below) takes one argument (a file name) and it prints the concatenation of the current directory and the file name, i.e. the absolute path of the file. Of course, you need to use the script in the directory where the given file is located. If no argument is given, it will print the current working directory.

If your goal is to paste the absolute path to somewhere else, you can copy the output of sp to the clipboard with tocb.

The script

The name of the script stands for “show path”.

#!/usr/bin/env python

# sp.py ("show path")

import os
import os.path
import sys

if len(sys.argv) == 1:
    print os.getcwd()
else:
    print os.path.join( os.getcwd(), sys.argv[1] )

Update (20110402)

The script is pushed to GitHub: https://github.com/jabbalaci/Bash-Utils.

About these ads
Categories: bash, python Tags: , , , ,
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.
Follow

Get every new post delivered to your Inbox.

Join 61 other followers

%d bloggers like this: