How to create a hard link to an inode (ext4)?
If I know the index node (inode) of a file, but I don't know its path (or any of its paths), is it possible to create a hard link to that inode directly?
I could find the file using sudo find / -inum 123546
and then create a hardlink, but that would be way too slow for my application.
N.B. I'm using an ext4 filesystem.
inode hard-link
New contributor
add a comment |
If I know the index node (inode) of a file, but I don't know its path (or any of its paths), is it possible to create a hard link to that inode directly?
I could find the file using sudo find / -inum 123546
and then create a hardlink, but that would be way too slow for my application.
N.B. I'm using an ext4 filesystem.
inode hard-link
New contributor
Closely related: removing or renaming a file via its inode (both of which are equally impossible to do directly).
– Gilles
13 hours ago
add a comment |
If I know the index node (inode) of a file, but I don't know its path (or any of its paths), is it possible to create a hard link to that inode directly?
I could find the file using sudo find / -inum 123546
and then create a hardlink, but that would be way too slow for my application.
N.B. I'm using an ext4 filesystem.
inode hard-link
New contributor
If I know the index node (inode) of a file, but I don't know its path (or any of its paths), is it possible to create a hard link to that inode directly?
I could find the file using sudo find / -inum 123546
and then create a hardlink, but that would be way too slow for my application.
N.B. I'm using an ext4 filesystem.
inode hard-link
inode hard-link
New contributor
New contributor
edited 11 hours ago
terdon♦
132k32261441
132k32261441
New contributor
asked 14 hours ago
AstoneAstone
563
563
New contributor
New contributor
Closely related: removing or renaming a file via its inode (both of which are equally impossible to do directly).
– Gilles
13 hours ago
add a comment |
Closely related: removing or renaming a file via its inode (both of which are equally impossible to do directly).
– Gilles
13 hours ago
Closely related: removing or renaming a file via its inode (both of which are equally impossible to do directly).
– Gilles
13 hours ago
Closely related: removing or renaming a file via its inode (both of which are equally impossible to do directly).
– Gilles
13 hours ago
add a comment |
2 Answers
2
active
oldest
votes
AFAIK, not with the kernel API. If such an interface existed, it would have to be limited to the super-user as otherwise that would let anyone access files in directories they don't have search access to.
But you could use debugfs
on the file system (once it's unmounted) to do it (assuming you have write access to the block device).
debugfs -w /dev/block/device
(replace /dev/block/device
with the actual block device the file system resides in).
Then, at the prompt of debugfs
, enter
stat <123>(with the angle brackets, replacing 123 with the actual inode number) to check that the file exists (inode has a link count greater than 0) and is not a directory.
If all good, enter:
ln <123> path/to/newfileto create the hardlink (note that the path is relative to the root of the file system). Followed by:
mi <123>to increment the link count (press Enter for all the fields except the link count where you'll want to add 1 to the current value).
4
Such an interface would also have to check that the file has a nonzero link count, otherwise it would be possible to resurrect a deleted-but-still-open file, which IIRC was rejected because it violates kernel invariants.
– Gilles
13 hours ago
1
@Gilles related: unix.stackexchange.com/a/499760/308316
– mosvy
13 hours ago
1
@PhilipCouling, the execute permission bit on a directory translates to search permission. I already said in directories then don't have search access to.
– Stéphane Chazelas
12 hours ago
@StéphaneChazelas Yes I realise that. Though "search access" isn't self explanatory and the nomenclature is a touch misleading. For the sake of future readers, I think it's worth noting that that [search access / execute bit] infers restricted access to the children. This would help less experienced readers. It gives the logic in a single pass rather than forcing further google searches.
– Philip Couling
12 hours ago
2
@OrangeDog, search permission is the terminology used by the POSIX standard.
– Stéphane Chazelas
9 hours ago
|
show 2 more comments
Depending on your use case, another approach could be to first collect all file candidates in one directory by hard linking it and then hard linking the files you are particularly interested in.
Such as
mkdir -pm 0700 by-inode/{0..999}
find <path> ! -type d -printf "%i/%p" |
while IFS=/ read -rd '' i n; do
ln "$n" "by-inode/$((i/1000))/$i"
done
(assuming your inode numbers are all less than 1,000,000, create more directories if need be).
Afterwards, your inodes are grouped 1000-wise and collected in the by-inode/
tree. From there, you can link them as needed.
Note though that it means that deleting files under <path>
will not reclaim the space because of that extra hard link.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Astone is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f505815%2fhow-to-create-a-hard-link-to-an-inode-ext4%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
AFAIK, not with the kernel API. If such an interface existed, it would have to be limited to the super-user as otherwise that would let anyone access files in directories they don't have search access to.
But you could use debugfs
on the file system (once it's unmounted) to do it (assuming you have write access to the block device).
debugfs -w /dev/block/device
(replace /dev/block/device
with the actual block device the file system resides in).
Then, at the prompt of debugfs
, enter
stat <123>(with the angle brackets, replacing 123 with the actual inode number) to check that the file exists (inode has a link count greater than 0) and is not a directory.
If all good, enter:
ln <123> path/to/newfileto create the hardlink (note that the path is relative to the root of the file system). Followed by:
mi <123>to increment the link count (press Enter for all the fields except the link count where you'll want to add 1 to the current value).
4
Such an interface would also have to check that the file has a nonzero link count, otherwise it would be possible to resurrect a deleted-but-still-open file, which IIRC was rejected because it violates kernel invariants.
– Gilles
13 hours ago
1
@Gilles related: unix.stackexchange.com/a/499760/308316
– mosvy
13 hours ago
1
@PhilipCouling, the execute permission bit on a directory translates to search permission. I already said in directories then don't have search access to.
– Stéphane Chazelas
12 hours ago
@StéphaneChazelas Yes I realise that. Though "search access" isn't self explanatory and the nomenclature is a touch misleading. For the sake of future readers, I think it's worth noting that that [search access / execute bit] infers restricted access to the children. This would help less experienced readers. It gives the logic in a single pass rather than forcing further google searches.
– Philip Couling
12 hours ago
2
@OrangeDog, search permission is the terminology used by the POSIX standard.
– Stéphane Chazelas
9 hours ago
|
show 2 more comments
AFAIK, not with the kernel API. If such an interface existed, it would have to be limited to the super-user as otherwise that would let anyone access files in directories they don't have search access to.
But you could use debugfs
on the file system (once it's unmounted) to do it (assuming you have write access to the block device).
debugfs -w /dev/block/device
(replace /dev/block/device
with the actual block device the file system resides in).
Then, at the prompt of debugfs
, enter
stat <123>(with the angle brackets, replacing 123 with the actual inode number) to check that the file exists (inode has a link count greater than 0) and is not a directory.
If all good, enter:
ln <123> path/to/newfileto create the hardlink (note that the path is relative to the root of the file system). Followed by:
mi <123>to increment the link count (press Enter for all the fields except the link count where you'll want to add 1 to the current value).
4
Such an interface would also have to check that the file has a nonzero link count, otherwise it would be possible to resurrect a deleted-but-still-open file, which IIRC was rejected because it violates kernel invariants.
– Gilles
13 hours ago
1
@Gilles related: unix.stackexchange.com/a/499760/308316
– mosvy
13 hours ago
1
@PhilipCouling, the execute permission bit on a directory translates to search permission. I already said in directories then don't have search access to.
– Stéphane Chazelas
12 hours ago
@StéphaneChazelas Yes I realise that. Though "search access" isn't self explanatory and the nomenclature is a touch misleading. For the sake of future readers, I think it's worth noting that that [search access / execute bit] infers restricted access to the children. This would help less experienced readers. It gives the logic in a single pass rather than forcing further google searches.
– Philip Couling
12 hours ago
2
@OrangeDog, search permission is the terminology used by the POSIX standard.
– Stéphane Chazelas
9 hours ago
|
show 2 more comments
AFAIK, not with the kernel API. If such an interface existed, it would have to be limited to the super-user as otherwise that would let anyone access files in directories they don't have search access to.
But you could use debugfs
on the file system (once it's unmounted) to do it (assuming you have write access to the block device).
debugfs -w /dev/block/device
(replace /dev/block/device
with the actual block device the file system resides in).
Then, at the prompt of debugfs
, enter
stat <123>(with the angle brackets, replacing 123 with the actual inode number) to check that the file exists (inode has a link count greater than 0) and is not a directory.
If all good, enter:
ln <123> path/to/newfileto create the hardlink (note that the path is relative to the root of the file system). Followed by:
mi <123>to increment the link count (press Enter for all the fields except the link count where you'll want to add 1 to the current value).
AFAIK, not with the kernel API. If such an interface existed, it would have to be limited to the super-user as otherwise that would let anyone access files in directories they don't have search access to.
But you could use debugfs
on the file system (once it's unmounted) to do it (assuming you have write access to the block device).
debugfs -w /dev/block/device
(replace /dev/block/device
with the actual block device the file system resides in).
Then, at the prompt of debugfs
, enter
stat <123>(with the angle brackets, replacing 123 with the actual inode number) to check that the file exists (inode has a link count greater than 0) and is not a directory.
If all good, enter:
ln <123> path/to/newfileto create the hardlink (note that the path is relative to the root of the file system). Followed by:
mi <123>to increment the link count (press Enter for all the fields except the link count where you'll want to add 1 to the current value).
edited 11 hours ago
answered 13 hours ago
Stéphane ChazelasStéphane Chazelas
309k57582945
309k57582945
4
Such an interface would also have to check that the file has a nonzero link count, otherwise it would be possible to resurrect a deleted-but-still-open file, which IIRC was rejected because it violates kernel invariants.
– Gilles
13 hours ago
1
@Gilles related: unix.stackexchange.com/a/499760/308316
– mosvy
13 hours ago
1
@PhilipCouling, the execute permission bit on a directory translates to search permission. I already said in directories then don't have search access to.
– Stéphane Chazelas
12 hours ago
@StéphaneChazelas Yes I realise that. Though "search access" isn't self explanatory and the nomenclature is a touch misleading. For the sake of future readers, I think it's worth noting that that [search access / execute bit] infers restricted access to the children. This would help less experienced readers. It gives the logic in a single pass rather than forcing further google searches.
– Philip Couling
12 hours ago
2
@OrangeDog, search permission is the terminology used by the POSIX standard.
– Stéphane Chazelas
9 hours ago
|
show 2 more comments
4
Such an interface would also have to check that the file has a nonzero link count, otherwise it would be possible to resurrect a deleted-but-still-open file, which IIRC was rejected because it violates kernel invariants.
– Gilles
13 hours ago
1
@Gilles related: unix.stackexchange.com/a/499760/308316
– mosvy
13 hours ago
1
@PhilipCouling, the execute permission bit on a directory translates to search permission. I already said in directories then don't have search access to.
– Stéphane Chazelas
12 hours ago
@StéphaneChazelas Yes I realise that. Though "search access" isn't self explanatory and the nomenclature is a touch misleading. For the sake of future readers, I think it's worth noting that that [search access / execute bit] infers restricted access to the children. This would help less experienced readers. It gives the logic in a single pass rather than forcing further google searches.
– Philip Couling
12 hours ago
2
@OrangeDog, search permission is the terminology used by the POSIX standard.
– Stéphane Chazelas
9 hours ago
4
4
Such an interface would also have to check that the file has a nonzero link count, otherwise it would be possible to resurrect a deleted-but-still-open file, which IIRC was rejected because it violates kernel invariants.
– Gilles
13 hours ago
Such an interface would also have to check that the file has a nonzero link count, otherwise it would be possible to resurrect a deleted-but-still-open file, which IIRC was rejected because it violates kernel invariants.
– Gilles
13 hours ago
1
1
@Gilles related: unix.stackexchange.com/a/499760/308316
– mosvy
13 hours ago
@Gilles related: unix.stackexchange.com/a/499760/308316
– mosvy
13 hours ago
1
1
@PhilipCouling, the execute permission bit on a directory translates to search permission. I already said in directories then don't have search access to.
– Stéphane Chazelas
12 hours ago
@PhilipCouling, the execute permission bit on a directory translates to search permission. I already said in directories then don't have search access to.
– Stéphane Chazelas
12 hours ago
@StéphaneChazelas Yes I realise that. Though "search access" isn't self explanatory and the nomenclature is a touch misleading. For the sake of future readers, I think it's worth noting that that [search access / execute bit] infers restricted access to the children. This would help less experienced readers. It gives the logic in a single pass rather than forcing further google searches.
– Philip Couling
12 hours ago
@StéphaneChazelas Yes I realise that. Though "search access" isn't self explanatory and the nomenclature is a touch misleading. For the sake of future readers, I think it's worth noting that that [search access / execute bit] infers restricted access to the children. This would help less experienced readers. It gives the logic in a single pass rather than forcing further google searches.
– Philip Couling
12 hours ago
2
2
@OrangeDog, search permission is the terminology used by the POSIX standard.
– Stéphane Chazelas
9 hours ago
@OrangeDog, search permission is the terminology used by the POSIX standard.
– Stéphane Chazelas
9 hours ago
|
show 2 more comments
Depending on your use case, another approach could be to first collect all file candidates in one directory by hard linking it and then hard linking the files you are particularly interested in.
Such as
mkdir -pm 0700 by-inode/{0..999}
find <path> ! -type d -printf "%i/%p" |
while IFS=/ read -rd '' i n; do
ln "$n" "by-inode/$((i/1000))/$i"
done
(assuming your inode numbers are all less than 1,000,000, create more directories if need be).
Afterwards, your inodes are grouped 1000-wise and collected in the by-inode/
tree. From there, you can link them as needed.
Note though that it means that deleting files under <path>
will not reclaim the space because of that extra hard link.
add a comment |
Depending on your use case, another approach could be to first collect all file candidates in one directory by hard linking it and then hard linking the files you are particularly interested in.
Such as
mkdir -pm 0700 by-inode/{0..999}
find <path> ! -type d -printf "%i/%p" |
while IFS=/ read -rd '' i n; do
ln "$n" "by-inode/$((i/1000))/$i"
done
(assuming your inode numbers are all less than 1,000,000, create more directories if need be).
Afterwards, your inodes are grouped 1000-wise and collected in the by-inode/
tree. From there, you can link them as needed.
Note though that it means that deleting files under <path>
will not reclaim the space because of that extra hard link.
add a comment |
Depending on your use case, another approach could be to first collect all file candidates in one directory by hard linking it and then hard linking the files you are particularly interested in.
Such as
mkdir -pm 0700 by-inode/{0..999}
find <path> ! -type d -printf "%i/%p" |
while IFS=/ read -rd '' i n; do
ln "$n" "by-inode/$((i/1000))/$i"
done
(assuming your inode numbers are all less than 1,000,000, create more directories if need be).
Afterwards, your inodes are grouped 1000-wise and collected in the by-inode/
tree. From there, you can link them as needed.
Note though that it means that deleting files under <path>
will not reclaim the space because of that extra hard link.
Depending on your use case, another approach could be to first collect all file candidates in one directory by hard linking it and then hard linking the files you are particularly interested in.
Such as
mkdir -pm 0700 by-inode/{0..999}
find <path> ! -type d -printf "%i/%p" |
while IFS=/ read -rd '' i n; do
ln "$n" "by-inode/$((i/1000))/$i"
done
(assuming your inode numbers are all less than 1,000,000, create more directories if need be).
Afterwards, your inodes are grouped 1000-wise and collected in the by-inode/
tree. From there, you can link them as needed.
Note though that it means that deleting files under <path>
will not reclaim the space because of that extra hard link.
edited 6 hours ago
Toby Speight
5,34811032
5,34811032
answered 8 hours ago
glglglglglgl
1,164812
1,164812
add a comment |
add a comment |
Astone is a new contributor. Be nice, and check out our Code of Conduct.
Astone is a new contributor. Be nice, and check out our Code of Conduct.
Astone is a new contributor. Be nice, and check out our Code of Conduct.
Astone is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f505815%2fhow-to-create-a-hard-link-to-an-inode-ext4%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Closely related: removing or renaming a file via its inode (both of which are equally impossible to do directly).
– Gilles
13 hours ago