Search code examples
linuxfindmvln

Move files in bulk and create links in their place in the directory in linux


I am trying to move hundreds of files from one directory to another but create a softlink in the old directory while doing that. Is there a single line command that can do that?

/dir1 file1.txt file2.txt . . . file100.txt

move to dir2 and create soft link to them in dir1.

I am currently doing that seperately but was hoping to find a single line command if possible.

cd dir1 mv *.txt /dir2 ln -s /dir2/*.txt .

I tried using find but that didn't work either.


Solution

  • There's no single line command. It's quite trivial to do with shell scripting. For example, in tcsh:

    % cd dir1
    % foreach FILETOMOVE ( file*.txt )
         echo mv -iv $FILETOMOVE /dir2
         echo ln -s /dir2/$FILETOMOVE .
         end
    

    (Remove the echo's once you're sure you've got it right.)

    Bash is similar, with slightly different syntax.

    This is slightly more complicated if the filenames or paths include spaces, but still quite simple. (:q in tcsh, using "", etc.)