1

I tried to run readlink -f on multiple files by using wildcard "*", but it evaluates other files as operand

Command

readlink -f *log

Output (desired)

/home/trail_1.log
/home/trail_2.log

Am I doing something wrong ? I am using tcsh and readlink is only command I am aware to read path of file.

2

You can also use realpath:

% realpath *
/data/code/mdweb/Gemfile
/data/code/mdweb/Gemfile.lock
/data/code/mdweb/README.markdown

realpath is not POSIX. It is available by default on FreeBSD systems, and on some (but not all) Linux systems (but AFAIK can be installed as a package on most, if not all, Linux systems).
realpath's behaviour might be different across implementations, so the above behaviour is not guaranteed to work. I've tested it on FreeBSD 10 & Arch Linux.

  • I already checked. I have no command like realpath and I have no prev-ledges to install it. – JigarGandhi Jan 12 '15 at 11:49
  • 2
    @JigarGandhi You can get a binary (or compile one) and drop it in ~/bin, and add that to your $PATH. You don't need to install it system-wide. – Martin Tournoij Jan 12 '15 at 12:04
  • 1
    GNU coreutils includes a realpath command, and Linux distributions are standardizing on that, but it's taking time. For example Debian's coreutils package only includes realpath since jessie (currently testing); in wheezy (stable) realpath is an optional package containing an older implementation. – Gilles Jan 13 '15 at 23:02
3

readlink only works with one file, you need a loop to do that:

for f in *; do
  readlink -f -- "$f"
done
  • is there any alternative to for loop? @cuongim – JigarGandhi Jan 12 '15 at 10:51
  • 1
    This is probably the cleanest and most readable solution, looping over files is a widely used pattern in shell scripting. If you want to work recursively or with complex predicates, you can always use find . -exec readlink -f -- '{}' ';' – orion Jan 12 '15 at 12:54
  • Note that readlink isn't POSIX, for example on OSX readlink -f won't work (I happened to find this out the other day, I always thought that readlink was POSIX, for some reason). – Martin Tournoij Jan 27 '15 at 9:31
  • @Carpetsmoker: Yes, of course. readlink on OSX is not GNU readlink - developer.apple.com/library/mac/documentation/Darwin/Reference/…. On OSX, you can use greadlink from coreutils. – cuonglm Jan 27 '15 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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