Intro To 'ln' Command In Linux
2023-05-06 - By Robert Elder
I use the 'ln' command to create links between files or directories:
ln -s target link_name
These links can act like shortcuts:
Here, I have a single file containing a list of to-do items in a file called 'todo.txt':
Todo items for the day:
- Check Hacker News 758943272 times
- Drink coffee
- Write software
- Get money
I can use this 'ln' command to create a symbolic link named 'stuff.txt' that links to the original 'todo.txt' file.
ln -s todo.txt stuff.txt
and now the current directory contains an entry for both 'todo.txt' and 'stuff.txt':
ls
stuff.txt todo.txt
Now, if I add some new text to my to-do list, I can see the changes through both the file itself and the symbolic link to the file:
echo "- Watch motivational YouTube videos for 3 hours" >> todo.txt
checking 'todo.txt', I get the following:
cat todo.txt
Todo items for the day:
- Check Hacker News 758943272 times
- Drink coffee
- Write software
- Get money
- Watch motivational YouTube videos for 3 hours
and in 'stuff.txt', I get the exact same thing:
cat stuff.txt
Todo items for the day:
- Check Hacker News 758943272 times
- Drink coffee
- Write software
- Get money
- Watch motivational YouTube videos for 3 hours
If I run
ls -l
I can see clearly that the symbolic link 'stuff.txt' points to 'todo.txt':
total 4
lrwxrwxrwx 1 robert robert 8 May 6 22:27 stuff.txt -> todo.txt
-rw-rw-r-- 1 robert robert 152 May 6 22:27 todo.txt
I can even create a symbolic link to the 'todo.txt' file located from another directory:
mkdir ../stuff
cd ../stuff
ln -s ../docs/todo.txt todo_shortcut.txt
Using the 'ls -l' command, I can view where the 'todo_shortcut.txt' symlink points to:
ls -l
lrwxrwxrwx 1 robert robert 16 May 6 22:34 todo_shortcut.txt -> ../docs/todo.txt
and as expected, the 'todo_shortcut.txt' can be used to access the same file contents:
cat todo_shortcut.txt
Todo items for the day:
- Check Hacker News 758943272 times
- Drink coffee
- Write software
- Get money
- Watch motivational YouTube videos for 3 hours
And that's why the 'ln' command is my favourite Linux command.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|