Find the answer to your Linux question:
Results 1 to 8 of 8
Need: A one line or script that does the following. Find all *.txt files in directory and subdirectories and create links to them in a /temp folder. I do know ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    LRC
    LRC is offline
    Just Joined! LRC's Avatar
    Join Date
    Apr 2009
    Posts
    13

    Copying files with spaces.


    Need: A one line or script that does the following. Find all *.txt files in directory and subdirectories and create links to them in a /temp folder.
    I do know that using a combination of find and copy -l can do this.
    Big Problem: All Directories and file names have spaces in them.
    Renaming them is an issue as other programs are depended upon those names. Could possibly using a command line that would replace all those files with a character (underscore is out as it is already being used, could a double character like !! work?), setting up links then replacing the character with space work? Or would that break the link?
    Do understand that linux does not like spaces in names for this reason, but one must work with what ones has.

  2. #2
    Just Joined!
    Join Date
    May 2011
    Location
    Central FL
    Posts
    91
    No need to worry about the spaces for this problem:
    Code:
    find . -type f -name "*.txt" -exec ln -s $PWD/{} /temp/ \;
    example/proof:
    Code:
    cd /tdayid@clugg:~$ cd /tmp
    dayid@clugg:/tmp$ mkdir 5
    dayid@clugg:/tmp$ mkdir 7
    dayid@clugg:/tmp$ cd 5
    dayid@clugg:/tmp/5$ touch "this is a file.txt"
    dayid@clugg:/tmp/5$ touch "this is also a file.txt"
    dayid@clugg:/tmp/5$ ls
    this is a file.txt  this is also a file.txt
    dayid@clugg:/tmp/5$ find . -type f -name "*.txt" -exec ln -s $PWD/{} ../7/ \;
    dayid@clugg:/tmp/5$ cd ../7
    dayid@clugg:/tmp/7$ ls -l
    total 0
    lrwxrwxrwx 1 dayid dayid 27 Jun  8 22:54 this is a file.txt -> /tmp/5/./this is a file.txt
    lrwxrwxrwx 1 dayid dayid 32 Jun  8 22:54 this is also a file.txt -> /tmp/5/./this is also a file.txt

  3. #3
    LRC
    LRC is offline
    Just Joined! LRC's Avatar
    Join Date
    Apr 2009
    Posts
    13
    Many thanks worked like a charm. Mistake I made was the "".

  4. $spacer_open
    $spacer_close
  5. #4
    LRC
    LRC is offline
    Just Joined! LRC's Avatar
    Join Date
    Apr 2009
    Posts
    13
    Ran into another problem. Link created, but they go to nowhere be cause it links to directory /./ instead of /home/garfus/Calibre Library/. Tried
    Code:
    /home/garfus/Calibre\ Library/ -type f -name "*.epub" -exec ln -s /{} /media/RESERVE1E/tempepub/ \;
    but that came with the same result.
    I did get a lot of
    ln: failed to create symbolic link `/media/RESERVE1E/tempepub/Calibre': File exists
    results.
    Is this because I have duplicate files of the same name? If so is there a way to modify the link name by adding incremental numbers after each one?

  6. #5
    Just Joined!
    Join Date
    May 2011
    Location
    Central FL
    Posts
    91
    Quote Originally Posted by LRC View Post
    Code:
    /home/garfus/Calibre\ Library/ -type f -name "*.epub" -exec ln -s /{} /media/RESERVE1E/tempepub/ \;
    !=
    Code:
    find . -type f -name "*.txt" -exec ln -s $PWD/{} /temp/ \;
    Why do you have it doing
    Code:
    ln -s /{}
    instead of
    Code:
    ln -s $PWD/{}
    ?

  7. #6
    LRC
    LRC is offline
    Just Joined! LRC's Avatar
    Join Date
    Apr 2009
    Posts
    13
    Sorry. They always say never do things when your are more then half asleep. First setup up a link renamed it to get rid of the space problem.
    Did it 2 ways:
    1)
    Code:
    find /media/RESERVE1E/Library/ -type f -name "*.epub" -exec ln -s $PWD/{} /media/RESERVE1E/tempepub/ \;
    That gave me links to /Library//media/RESERVE1E/Library/
    2)
    Code:
     cd /media/RESERVE1E/Library/
    Code:
    find . -type f -name "*.epub" -exec ln -s $PWD/{} /media/RESERVE1E/tempepub/ \;
    That gave me links to /media/RESERVE1E/Library/./
    3) moved link to ~/
    Code:
    find ~/Library/ -type f -name "*.epub" -exec ln -s $PWD/{} /media/RESERVE1E/tempepub/ \;
    That gave me links to /home/garfus//home/garfus/Library/.
    So I thought maybe the directory I am starting my commands are the problem. Still a no go. Is this a bug? or am I missing something?
    If I have no way around this, then is there a script I could use that could modify those links like getting rid of those added directories that do not exist.

  8. #7
    Just Joined!
    Join Date
    May 2011
    Location
    Central FL
    Posts
    91
    Quote Originally Posted by LRC View Post
    2)
    Code:
     cd /media/RESERVE1E/Library/
    Code:
    find . -type f -name "*.epub" -exec ln -s $PWD/{} /media/RESERVE1E/tempepub/ \;
    That gave me links to /media/RESERVE1E/Library/./
    If you type in (manually):
    Code:
    ln -s /media/RESERVE1E/Library/thistitle.epub /media/RESERVE1E/tempepub/
    That will create:
    Code:
    ls -l
    /media/RESERVE1E/tempepub/thistitle.epub -> /media/RESERVE1E/Library/./thistitle.epub
    This is precisely the same as:
    Code:
    /media/RESERVE1E/Library/thistitle.epub
    If it doesn't work, you have something else that is broken.

    What shell are you using, etc... Please also give a real "ls -l" for the created files that are supposedly wrong.

    Naturally, since I'm using "$PWD", you need to use the "find ." style, not name the directory, since $PWD relies on the ... present... working... directory. If you're exec'ing it from somewhere else, then your PWD is not going to match...

  9. #8
    LRC
    LRC is offline
    Just Joined! LRC's Avatar
    Join Date
    Apr 2009
    Posts
    13
    Thank you very much. Now that I understand what folder I had to be in to use PWD, everything went as it should. Found out it was tacking on the extra phantom directories based on starting directory point. Needed to be inside the directory I wanted to search.
    note: Would like to put "SOLVED" up but can not see where I can and maybe either because I am blind or a new comer. Many thanks for the help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •