Link Files in linux
2 types of link files are present in linux:
Soft link and Hard link
Soft link | Hard link |
Shortcut file | Backup file |
size of link file is equal to the no of chars in original filename | size of both files are same |
If original file is deleted, link is broken and data is lost | if original file is deleted then also link will contain all data |
command: ln -s <source_file> <dest_file> | command: ln <source_file> <dest_file> |
Soft link is similar to shortcut in windows
To execute command/shellscript from anywhere, create a soft link of the file in /usr/local/bin directory
ex:
touch hello.sh
cat > hello.sh
echo "hello world"
^C
execute this file in same directory:
./hello.sh
output: hello world
cd testdir/
execute hello.sh in another directory:
./hello.sh
output: -bash: ./hello.sh: No such file or directory
create a link of this shell script file to directory /usr/local/bin
ln -s /opt/training/hello.sh /usr/local/bin/hello
Now execute hello from any directory:
hello
output: hello world