LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-07-2010, 03:10 PM   #1
xmrkite
Member
 
Registered: Oct 2006
Location: California, USA
Distribution: Mint 16, Lubuntu 14.04, Mythbuntu 14.04, Kubuntu 13.10, Xubuntu 10.04
Posts: 554

Rep: Reputation: 30
create symbolic links from recurisve folders?


Hello, I am somewhat familiar with the ln -sf command to create symbolic links.

However, can I run a command and create symbolic links for all files in a given folder and its subfolders and have all the links be in one folder?

I have a file structure such as:


\Folder\A
\Folder\B
\Folder\C

and I want to have symbolic links for all the files in the A, B, and C all in one new folder (Folder\ALL) for example.

Is this possible?

I have hundreds of folders that need to be done, so a simple 1 line command would be ideal if possible.

-Thanks
 
Old 06-07-2010, 04:12 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,803

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
Code:
[mherring@mystical newplay]$ for file in $(ls -R ../play/*); do ln -s $file; done
In this example, the command is run in the "newplay" directory**, which is at the same level as "play". It works fine, but the only issue is that it creates some bogus links based on the output format of "ls -R". This should be easy to fix.


**When making soft links, I always "link in" by running the ln command from inside the target folder. It avoids certain kinds of errors, and involves less typing.
 
Old 06-07-2010, 04:34 PM   #3
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
Why do you want symlinks for all files in the folders ?
Surely just linking the folder is sufficient. Any actions on the symlink will be actions on the original anyway.
If you were prepared to have your ALL folder one level higher, all you'd have to do is ln -s Folder and it would contain all the folders and all the files.

Unless you want a completely flat structure with no subdirectories visible, but you will have to be careful of duplicate names.

Last edited by smoker; 06-07-2010 at 04:40 PM.
 
Old 06-07-2010, 04:50 PM   #4
xmrkite
Member
 
Registered: Oct 2006
Location: California, USA
Distribution: Mint 16, Lubuntu 14.04, Mythbuntu 14.04, Kubuntu 13.10, Xubuntu 10.04
Posts: 554

Original Poster
Rep: Reputation: 30
"Unless you want a completely flat structure with no subdirectories visible, but you will have to be careful of duplicate names."


Yes, this is what I want. There will be no duplicate names. It's important to have the files accessible via both a single flat directory and their original directory structure.

-Thanks
 
Old 06-07-2010, 05:02 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
What about this?
Code:
find /path/to/top/level/directory -type f -exec ln -t /path/to/link/directory -s {} \;
this will descend into folder /path/to/top/level/directory and it will create links into folder /path/to/link/directory. It is mandatory that you specify the full path of the top level directory containing all the files you want to link, otherwise the links themselves will result broken.
 
Old 06-07-2010, 05:34 PM   #6
xmrkite
Member
 
Registered: Oct 2006
Location: California, USA
Distribution: Mint 16, Lubuntu 14.04, Mythbuntu 14.04, Kubuntu 13.10, Xubuntu 10.04
Posts: 554

Original Poster
Rep: Reputation: 30
find /path/to/top/level/directory -type f -exec ln -t /path/to/link/directory -s {} \;


returns:

find: missing argument to `-exec'


Any ideas?
 
Old 06-07-2010, 05:41 PM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
That's strange. The error stats there's nothing after -exec. Please, can you copy/paste the actual command line from your terminal, including the error message?
 
Old 06-07-2010, 06:54 PM   #8
xmrkite
Member
 
Registered: Oct 2006
Location: California, USA
Distribution: Mint 16, Lubuntu 14.04, Mythbuntu 14.04, Kubuntu 13.10, Xubuntu 10.04
Posts: 554

Original Poster
Rep: Reputation: 30
Here you go,

find "/media/structure/db" -type f -exec ln -t /home/usr/docs -s {} \ ;

done exactly so, all on one line, right in konsole using kubuntu 10.04
 
Old 06-07-2010, 07:41 PM   #9
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
I'm seeing the same thing here. In fact, examples provided in man find aren't working, giving the exact same error message.

Wonder if there is a bug in find? Sure looks like it.
 
Old 06-08-2010, 04:23 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Even more strange. I tried it on three different systems and it works like a charm. Here is an example on Ubuntu 8.04, GNU find version 4.2.32:
Code:
$ tree -F
.
|-- ALL/
`-- folder/
    |-- A/
    |   `-- file1
    |-- B/
    |   `-- file2
    `-- C/
        `-- file3
$
$ find /home/colucix/test -type f
/home/colucix/test/folder/C/file3
/home/colucix/test/folder/A/file1
/home/colucix/test/folder/B/file2
$ find /home/colucix/test -type f -exec ln -t /home/colucix/test/ALL -s {} \;
$ ls -l ALL
total 0
lrwxrwxrwx 1 colucix users 35 2010-06-08 10:18 file1 -> /home/colucix/test/folder/A/file1
lrwxrwxrwx 1 colucix users 35 2010-06-08 10:18 file2 -> /home/colucix/test/folder/B/file2
lrwxrwxrwx 1 colucix users 35 2010-06-08 10:18 file3 -> /home/colucix/test/folder/C/file3
$
I wonder if the same error pops up if you use a different command after -exec. Anyway, which version of find do you have? If it's really a bug, maybe we have to submit it...
 
Old 06-08-2010, 04:55 AM   #11
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Worth a shot: try replacing the \; with a + (which should make the command faster anyway):
Code:
find /path/to/top/level/directory -type f -exec ln -t /path/to/link/directory -s {} +
colucix's way works on my Slackware 13.1 installation just fine (findutils v4.4.2).
 
1 members found this post helpful.
Old 06-08-2010, 12:22 PM   #12
xmrkite
Member
 
Registered: Oct 2006
Location: California, USA
Distribution: Mint 16, Lubuntu 14.04, Mythbuntu 14.04, Kubuntu 13.10, Xubuntu 10.04
Posts: 554

Original Poster
Rep: Reputation: 30
find /path/to/top/level/directory -type f -exec ln -t /path/to/link/directory -s {} +



Now that worked

Thanks a ton
Strange that it did not work with the slash in there.
 
Old 06-08-2010, 12:35 PM   #13
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Quote:
Originally Posted by pwc101 View Post
Worth a shot: try replacing the \; with a +
Hi pwc101! I barely could explain the reason why this syntax works, anyway... good catch!
 
Old 06-08-2010, 01:11 PM   #14
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by xmrkite View Post
find /path/to/top/level/directory -type f -exec ln -t /path/to/link/directory -s {} +



Now that worked

Thanks a ton
Strange that it did not work with the slash in there.
Weird. I wasn't really expecting it to work.

The + at the end is actually faster than the \; anyway, so I tend to favour it when using find with exec: it makes find behave more like find and xargs does. Instead of a single invocation of the program being exec'ed for each file found, it collects a number of files and passes them all as arguments to a single instance of the exec'ed command, thus dramatically reducing the number of called to the exec'ed command, thereby increasing the speed of execution.

Anyway, glad it worked.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Learn Linux, 101: Create and change hard and symbolic links LXer Syndicated Linux News 1 06-08-2010 09:45 AM
can I create symbolic links for multiple files simutaneously cliffyao Linux - Newbie 3 02-18-2010 04:11 PM
how to create jar files with symbolic links alps963 Linux - General 1 05-03-2008 07:30 AM
How to create incrementing symbolic links from the output of a listing of devices no_treble Programming 8 02-07-2008 02:03 PM
how do I create links to folders? liontamer13 Linux - Newbie 2 12-18-2004 04:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration