Skip to main content
added 492 characters in body
Source Link
Stéphane Chazelas
  • 586.9k
  • 96
  • 1.1k
  • 1.7k

Here, with several find implementations, you can just do:

find / -samefile init.vim

Which is also more correct as inode numbers are only unique per filesystem, so you also need to check the device number in addition to the inode number.

Here, where the whole output of a command substitutionminus the trailing newline characters it to be passed as one argument to a command, command substitution is the way to go, though leaving it unquoted doesn't make sense in this case:

find / -inum "$(ls -id init.vim | awk '{print $1; exit}')"

No need for -l, but we need -d to avoid problems if init.vim is of type directory. See also the ;exit so we only print the first field of the first line to handle cases where the file name contains newline characters.

Or better, check first that you're able to get the inode number

With

#! /bin/sh -
file=init.vim
inode=$(
  set -o pipefail # also care about ls exit status below
  ls -id -- "$file" | awk '{print $1; exit}'
) || exit
find / -inode "$inode"

(here, it's fine to leave it unquoted as we're not in a list context as that's a scalar variable assignment, so no split+glob happens).

The generic command to convert an input stream to a list of arguments is the xargs command (cross-arguments):

ls -id init.vim | awk '{print $1; exit}' |
  xargs -I @INODE@ find / -inum @INODE@

It's important to note that xargs will split the input (in its very own way where it does understand some form of quoting) to make up the arguments (and with -I, the splitting is done differently than without), see the man page on your system for details and the POSIX specification for what you can expect portably. In this case, the 12345<newline> output of awk will be split into one 12345 argument.

Here, with several find implementations, you can just do:

find / -samefile init.vim

Which is also more correct as inode numbers are only unique per filesystem, so you also need to check the device number in addition to the inode number.

Here, command substitution is the way to go, though leaving it unquoted doesn't make sense in this case:

find / -inum "$(ls -id init.vim | awk '{print $1; exit}')"

No need for -l, but we need -d to avoid problems if init.vim is of type directory. See also the ;exit so we only print the first field of the first line to handle cases where the file name contains newline characters.

Or better, check first that you're able to get the inode number

With

#! /bin/sh -
file=init.vim
inode=$(
  set -o pipefail # also care about ls exit status below
  ls -id -- "$file" | awk '{print $1; exit}'
) || exit
find / -inode "$inode"

(here, it's fine to leave it unquoted as we're not in a list context as that's a scalar variable assignment, so no split+glob happens).

The generic command to convert an input stream to a list of arguments is the xargs command (cross-arguments):

ls -id init.vim | awk '{print $1; exit}' |
  xargs -I @INODE@ find / -inum @INODE@

Here, with several find implementations, you can just do:

find / -samefile init.vim

Which is also more correct as inode numbers are only unique per filesystem, so you also need to check the device number in addition to the inode number.

Here, where the whole output of a command minus the trailing newline characters it to be passed as one argument to a command, command substitution is the way to go, though leaving it unquoted doesn't make sense in this case:

find / -inum "$(ls -id init.vim | awk '{print $1; exit}')"

No need for -l, but we need -d to avoid problems if init.vim is of type directory. See also the ;exit so we only print the first field of the first line to handle cases where the file name contains newline characters.

Or better, check first that you're able to get the inode number

With

#! /bin/sh -
file=init.vim
inode=$(
  set -o pipefail # also care about ls exit status below
  ls -id -- "$file" | awk '{print $1; exit}'
) || exit
find / -inode "$inode"

(here, it's fine to leave it unquoted as we're not in a list context as that's a scalar variable assignment, so no split+glob happens).

The generic command to convert an input stream to a list of arguments is the xargs command (cross-arguments):

ls -id init.vim | awk '{print $1; exit}' |
  xargs -I @INODE@ find / -inum @INODE@

It's important to note that xargs will split the input (in its very own way where it does understand some form of quoting) to make up the arguments (and with -I, the splitting is done differently than without), see the man page on your system for details and the POSIX specification for what you can expect portably. In this case, the 12345<newline> output of awk will be split into one 12345 argument.

Source Link
Stéphane Chazelas
  • 586.9k
  • 96
  • 1.1k
  • 1.7k

Here, with several find implementations, you can just do:

find / -samefile init.vim

Which is also more correct as inode numbers are only unique per filesystem, so you also need to check the device number in addition to the inode number.

Here, command substitution is the way to go, though leaving it unquoted doesn't make sense in this case:

find / -inum "$(ls -id init.vim | awk '{print $1; exit}')"

No need for -l, but we need -d to avoid problems if init.vim is of type directory. See also the ;exit so we only print the first field of the first line to handle cases where the file name contains newline characters.

Or better, check first that you're able to get the inode number

With

#! /bin/sh -
file=init.vim
inode=$(
  set -o pipefail # also care about ls exit status below
  ls -id -- "$file" | awk '{print $1; exit}'
) || exit
find / -inode "$inode"

(here, it's fine to leave it unquoted as we're not in a list context as that's a scalar variable assignment, so no split+glob happens).

The generic command to convert an input stream to a list of arguments is the xargs command (cross-arguments):

ls -id init.vim | awk '{print $1; exit}' |
  xargs -I @INODE@ find / -inum @INODE@