Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create symbolic link from find

Tags:

find

sed

I'm trying to create a symbolic link (soft link) from the results of a find command. I'm using sed to remove the ./ that precedes the file name. I'm doing this so I can paste the file name to the end of the path where the link will be saved. I'm working on this with Ubuntu Server 8.04.

I learned from this post, which is kind of the solution to my problem but not quite-

How do I selectively create symbolic links to specific files in another directory in LINUX?

The resulting file name didn't work, though, so I started trying to learn awk and then decided on sed.

I'm using a one-line loop to accomplish this. The problem is that the structure of the loop is separating the filename, creating a link for each word in the filename. There are quite a few files and I would like to automate the process with each link taking the filename of the file it's linked to.

I'm comfortable with basic bash commands but I'm far from being a command line expert. I started this with ls and awk and moved to find and sed. My sed syntax could probably be better but I've learned this in two days and I'm kind of stuck now.

for t in find -type f -name "*txt*" | sed -e 's/.//' -e 's$/$$'; do echo ln -s $t ../folder2/$t; done

Any help or tips would be greatly appreciated. Thanks.

like image 385
mark Avatar asked Nov 27 '12 05:11

mark


People also ask

How do I create a symbolic link?

Ln Command to Create Symbolic Links By default, the ln command creates a hard link. Use the -s option to create a soft (symbolic) link. The -f option will force the command to overwrite a file that already exists. Source is the file or directory being linked to.

Can directories be symbolic links?

A symbolic link, also known as a soft link or symlink, is a special file pointing to another file or directory using an absolute or relative path. Symbolic links are similar to shortcuts in Windows and are useful when you need quick access to files or folders with long paths.

How do I create a symbolic link in Linux?

Create Symbolic Link in Linux for Files There is nothing hard in creating Symbolic links in Linux – you just need to follow one simple step. The ln command in Linux creates links between source files and directories. -s – the command for Symbolic Links. [Symbolic filename] – name of the symbolic link.


2 Answers

Easier:

Go to the folder where you want to have the files in and do:

find /path/with/files -type f -name "*txt*" -exec ln -s {} . ';'
like image 97
Florian Schicker Avatar answered Sep 22 '22 23:09

Florian Schicker


Execute your for loop like this:

(IFS=$'\n'; for t in `find -type f -name "*txt*" | sed 's|.*/||'`; do ln -s $t ../folder2/$t; done)

By setting the IFS to only a newline, you should be able to read the entire filename without getting splitted at space.

The brackets are to make sure the loop is executed in a sub-shell and the IFS of the current shell does not get changed.

like image 45
Guru Avatar answered Sep 25 '22 23:09

Guru