1

I'm trying to execute a command for each file in a directory but while using their absolute path (such as /home/richi/mydir/myfile.py) instead of their relative path (such as myfile.py).

In other words, I want to execute a command on files in a directory based on their absolute path - similar to for file in *.py; do thecommand -a "$file"; done but not quite.

I'm asking this because I'm trying to implement a Travis CI script running in an Ubuntu 14.04 environment which will install and use pyminifier to recursively minify all the Python code files in a directory.

Please note that I'm asking may be similar to this post but it's not.

Richie Bendall
  • 7,738
  • 4
  • 38
  • 58
  • What do you know about these files? do you have a list by absolute path? Do you need to search through with `find /` through your whole filesystem? – jeremysprofile Jul 21 '18 at 02:43
  • I need to find the files in a directory - I've updated paragraph 2 in the question to answer that. – Richie Bendall Jul 21 '18 at 02:44
  • so... `for file in *.py; do thecommand -a "$PWD$file"; done`? – jeremysprofile Jul 21 '18 at 02:51
  • You've got Windows filenames but bash script syntax, so… does that mean you have a full MinGW setup, with a `realpath` command? If so, just call `$(realpath "$file")`. – abarnert Jul 21 '18 at 02:51
  • The windows filenames is a mere prop but I'll update my question to fix that. – Richie Bendall Jul 21 '18 at 02:54
  • If you don't have that, you do presumably have Python, so worst case you can `$(python -c 'import os,sys; print(os.path.realpath(sys.argv[1]))' "$file"`. – abarnert Jul 21 '18 at 02:56
  • OK, so if you're not on Windows, what platform _are_ you on, what shell are you using, and you do have a full standard userland? – abarnert Jul 21 '18 at 02:57
  • As I described in the question, I'm using Travis CI. Sorry for my lack in information – Richie Bendall Jul 21 '18 at 02:57
  • Travis CI is neither a platform nor a shell nor a userland. It runs jobs in whatever container or VM you want. If you run the job in, say, a stock Ubuntu Server 16.04 LTS container, that's completely different from running it in, say, the macOS that comes with a Travis "Xcode" container. – abarnert Jul 21 '18 at 03:00
  • At any rate, if you can't answer any of these questions, you can at least try it. Do you have a `realpath` command? Does it do what you want? If not, what do you want that it isn't doing? – abarnert Jul 21 '18 at 03:02
  • Forget about Travis CI, just take in these specs: Ubuntu 14.04 – Richie Bendall Jul 21 '18 at 03:02
  • I think it has `realpath` beacuse of [this](https://packages.ubuntu.com/trusty/utils/realpath) on Ubuntu packages. – Richie Bendall Jul 21 '18 at 03:08
  • Your `for` loop very definitely *doesn't* do anything with full paths. – tripleee Jul 21 '18 at 04:01
  • We've sorted out this question already - see the answer. – Richie Bendall Jul 21 '18 at 10:19

1 Answers1

0

Since you're on a standard Linux distro with a full userland, you can just use the realpath command:

Print the resolved absolute file name…

For example:

$ pwd
/home/abarnert/src/test
$ touch 1
$ realpath 1
/home/abarnert/src/test/1

That's it.

If you don't know how to use that from within bash, you can call a subcommand using $(…) syntax:

$ echo $(realpath 1)
/home/abarnert/src/test/1

Of course you want to pass it the value of the variable file, but that's just as easy:

$ file=1
$ echo $(realpath "$file")
/home/abarnert/src/test/1

I'm assuming you're using bash here. With a different sh-style shell, things will be different; with tcsh or zsh or fish or something, it may be even more different.


A really old userland, or a really stripped down one (e.g., for an embedded system) might not include realpath. In that case, you can use readlink, since the GNU version, as usually, adds everything including a couple kitchen sinks, and can be used as a realpath substitute.

Or, if worst comes to worst, Python has come with a realpath function since 2.2:

$(python -c 'import os,sys; print(os.path.realpath(sys.argv[1]))' "$file")
abarnert
  • 354,177
  • 51
  • 601
  • 671
  • Interesting... I'm going to see if Travis CI will run it. – Richie Bendall Jul 21 '18 at 03:12
  • I made the [changes](https://github.com/Richienb/ROS-Code/blob/ea29b195d5c170cf4402332cef9261d49c92b0da/.travis.yml#L69) and it [works](https://travis-ci.org/Richienb/ROS-Code/jobs/406502839)! Thank you for your help. – Richie Bendall Jul 21 '18 at 03:19