0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

unlinkコマンドで指定する削除対象の末尾に「/」付けてエラーになった話

Last updated at Posted at 2024-02-23

unlinkコマンドが通らない

誤って作成したシンボリックリンクを削除するためにunlinkコマンドを実行したらエラーが返ってきてちょっと困ったときのお話。

私「シンボリック作るだけの作業?こんなん瞬殺でしょ」
私「ほい出来た」
手順書くん「リンク元とリンク先、逆でしたわ笑」
私「は??????」

こんな感じのディレクトリ構成の環境

 ls -lR
.:
total 0
drwxrwxr-x 2 ec2-user ec2-user  6 Feb 23 14:44 link_DST
drwxrwxr-x 4 ec2-user ec2-user 28 Feb 23 14:44 link_SR

./link_DST:
total 0

./link_SR:
total 0
drwxrwxr-x 2 ec2-user ec2-user 22 Feb 23 14:46 SR1
drwxrwxr-x 2 ec2-user ec2-user 22 Feb 23 14:45 SR2

lnコマンドで./link_SR/SR1/と./link_SR/SR2/のリンクを作る

# シンボリックリンクを作る
ln -s ./link_SR/SR1 ./link_DST/
ln -s ./link_SR/SR2 ./link_DST/

# 作成結果
ls -lR
.:
total 0
drwxrwxr-x 2 ec2-user ec2-user 28 Feb 23 14:51 link_DST
drwxrwxr-x 4 ec2-user ec2-user 28 Feb 23 14:44 link_SR

./link_DST:
total 0
lrwxrwxrwx 1 ec2-user ec2-user 13 Feb 23 14:51 SR1 -> ./link_SR/SR1
lrwxrwxrwx 1 ec2-user ec2-user 13 Feb 23 14:51 SR2 -> ./link_SR/SR2

./link_SR:
total 0
drwxrwxr-x 2 ec2-user ec2-user 6 Feb 23 14:49 SR1
drwxrwxr-x 2 ec2-user ec2-user 6 Feb 23 14:49 SR2

./link_SR/SR1:
total 0

./link_SR/SR2:
total 0

私「リンク消すのってどうやるんだっけ(ググり)→unlinkコマンド?そんなんあったっけか

で、unlinkコマンドで削除しようとする

unlink ./link_DST/SR1/
unlink: cannot unlink ‘./link_DST/SR1/’: Not a directory

私「消せない……」
私「『ディレクトリではありません』って言ってるけど、シンボリックリンクはそりゃディレクトリではないだろうよい」
私「……あ!」

unlink ./link_DST/SR1

# 消せた!
ls -lR
./link_DST:
total 0
lrwxrwxrwx 1 ec2-user ec2-user 13 Feb 23 14:51 SR2 -> ./link_SR/SR2

気が付いた理由(理由か?)

私「なんで動かんねん、通れや(同じコマンド実行カチャカチャッターン) 1
私「あれ!?削除出来た!?」

要するに

私「よく見たら同じコマンド実行してないじゃん」
私「tabでパス補完して入力したら末尾に『/』ついてないわ」
私「リンクの元先間違ってた手順書に書いてあるコマンドメモを信じてコピペしたのがいけなかったのね」

学んだこと

  1. コマンドを叩くときはちゃんと調べる
    1. そもそも手順書をちゃんとレビューする
  2. 補完機能は偉大である
  3. unlinkコマンドの存在

【参考資料】

  1. https://twitter.com/simonharrer/status/1006843860679643137
    「Software and cathedrals are much the same – first we build them, then we pray. — Sam Redwine」
    なんか一時期知り合いの中で流行ってたので有名なのかと思ってたけどそういうわけでもなさそうだった

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?