LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-26-2009, 12:00 PM   #1
d3coy
LQ Newbie
 
Registered: May 2009
Location: Canada
Posts: 3

Rep: Reputation: 0
Script to move old files, create symbolic link


Hi, one of my file systems is reaching capacity, and I would like to clear up some room by finding old files (ex. >1 yr), moving them, and then create a link to its new location.

find /mnt/oldLocation/* -mtime +365 -type f -exec mv {} /mnt2/newLocation/ \;

The above command will find files (in my filesystem reaching capacity) over 365 days old and move them to a new location. Now, for every file that is moved, I need to create a symlink from its old location to its new location.

How can I go about doing this? Any help is greatly appreciated.
Thanks

Last edited by d3coy; 05-26-2009 at 12:43 PM.
 
Old 05-26-2009, 01:16 PM   #2
lucazorzi
Member
 
Registered: Jul 2006
Location: Italy
Distribution: Ubuntu 11.04, Debian, OS X
Posts: 71

Rep: Reputation: 16
You could create a simple script like this:
Code:
#!/bin/bash
mv $1 /mnt2/newLocation/
ln -s /mnt/new/newLocation/`basename $1` $1
put it in you path and call it through find:
Code:
find /mnt/oldLocation/* -mtime +365 -type f -exec my_script.sh {} \;
Note: i haven't tested the script, but it should work.
 
Old 05-26-2009, 03:35 PM   #3
d3coy
LQ Newbie
 
Registered: May 2009
Location: Canada
Posts: 3

Original Poster
Rep: Reputation: 0
Hi lucazorzi, thanks! That's exactly what i was looking for.
One more question:
Since I have subdirectories (and want to keep the directory tree in /mnt2), I modified the script a little.
Code:
#!/bin/bash
mv $1 /mnt2/$1
ln -s /mnt2/$1 $1
However the mv command does not make a directory if it does not exist. How do I check if the dir exists, and if not, mkdir?
Is there a better way to work around this?

Thanks again.
 
Old 05-26-2009, 04:45 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
You can modify the script like this:
Code:
#!/bin/bash
mkdir -p /mnt2$(dirname $1)
mv $1 /mnt2$1
ln -s /mnt2$1 $1
the dirname command gives the path of the file. Prepend it with /mnt2, then create the directory using the -p option (it has the double effect to create the directory and its parents directories if they don't exist and to not give an error message if the directory already exist).
 
Old 05-26-2009, 04:48 PM   #5
lucazorzi
Member
 
Registered: Jul 2006
Location: Italy
Distribution: Ubuntu 11.04, Debian, OS X
Posts: 71

Rep: Reputation: 16
Maybe you should edit the find command to return also directories, then you edit our previous script to check if the argument is a directory, in which case we recreate it, or a file, which we'll copy:
Code:
#!/bin/bash
if [[ -d $1 ]]
then
     mkdir /mnt2/$1
else
     mv $1 /mnt2/$1
     ln -s /mnt2/$1 $1
fi
EDIT: I think colucix solution is smarter, I didn't remember of the dirname command

Last edited by lucazorzi; 05-26-2009 at 05:03 PM.
 
Old 05-27-2009, 12:04 PM   #6
d3coy
LQ Newbie
 
Registered: May 2009
Location: Canada
Posts: 3

Original Poster
Rep: Reputation: 0
Awesome answers guys, I ended up using colucix's solution... thx
 
Old 11-06-2011, 03:33 PM   #7
clenux
LQ Newbie
 
Registered: Nov 2011
Posts: 2

Rep: Reputation: Disabled
Blanks in filenames

I am using the mentioned code for a similar task, however, it fails, when it comes to filenames that contain blanks.
Even though I worked more than an hour on that problem, I am apparently too much of a newbie to fix it myself.
Any suggestions?

Thx

Clemens

Quote:
Originally Posted by lucazorzi View Post
You could create a simple script like this:
Code:
#!/bin/bash
mv $1 /mnt2/newLocation/
ln -s /mnt/new/newLocation/`basename $1` $1
put it in you path and call it through find:
Code:
find /mnt/oldLocation/* -mtime +365 -type f -exec my_script.sh {} \;
Note: i haven't tested the script, but it should work.
 
Old 11-06-2011, 03:46 PM   #8
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Quote:
Originally Posted by clenux View Post
I am using the mentioned code for a similar task, however, it fails, when it comes to filenames that contain blanks.
Even though I worked more than an hour on that problem, I am apparently too much of a newbie to fix it myself.
Any suggestions?

Thx

Clemens
Put the variables inside quotations, ie:
Code:
mv "$1" /mnt2/newLocation/
 
Old 11-09-2011, 02:26 PM   #9
clenux
LQ Newbie
 
Registered: Nov 2011
Posts: 2

Rep: Reputation: Disabled
Thanks

Ouch! That easy. Thanks, it works.
 
  


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
How to create symbolic link to html file and avoid page link problem? haxpor Linux - General 2 01-29-2008 08:40 AM
KDE Random wallpaper or script to create symbolic links to random files cvelasquez Linux - Software 2 02-26-2007 07:48 PM
Need to create a script to move files from one directory to another mustang05 Solaris / OpenSolaris 4 06-16-2006 11:25 AM
How do I create a symbolic link? Arenba Linux - Newbie 4 09-29-2003 11:40 AM
how do I create a symbolic link? rdaves@earthlink.net Linux - Newbie 2 05-23-2001 02:28 PM


All times are GMT -5. The time now is 04:54 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration