Can someone help create a symbolic link where I append my own string to the name of the link? Here's the code that I have so far:
find /home/folder1/*.txt -type f -exec ln -s {} \;
find /home/folder2/*.txt -type f -exec ln -s {} \;
So that command works to create my symbolic link but I would like to append my own text to the link name because I could have the same file name in each folder and in the destination I need to find a way to give unique name to the symbolic link. So I was thinking of creating the sym links to look like this:
folder1_doc1.txt
folder2_doc1.txt
folder1_doc2.txt
folder2_doc2.txt
I'm thinking the script would look something like this:
find /home/folder1/*.txt -type f -exec ln -s {} "folder1_" +\;
find /home/folder1/*.txt -type f -exec ln -s {} "folder2_" + \;
Allow me to point out something. Using zillions of symbolic links is fraught with problems. A simple one is that you use up files. Filesystems have data objects called inodes allocated to them. The number is fixed. So if you have 10000 inodes allocated, all of your directories, filename and link files use up one inode each.
To answer your question -
Note: You also seem to have a syntax error in your find statement.
consider NOT using a one liner, use a function instead:
# function make_links
# usage make_links pathname linkname linkhome [directory for the link to live in]
# so $1 is the serch path for find
# $2 is the name to prepend to the link name
# $3 is where the link will live
make_links()
{
find "$1" -type f -name '*.txt' |
while read fname
do
# make sure we do not overwrite an existing link
[ ! -L ${3}/${2}${fname} ] && ln -s $fname ${3}/${2}${fname}
done
}
# example usage
make_links /home/folder1 folder1_ $HOME
make_links /home/folder2 folder2_ $HOME