LinuxQuestions.org
Review your favorite Linux distribution.
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 10-08-2010, 04:12 PM   #1
WhisperiN
Member
 
Registered: Jun 2009
Location: Middle East
Distribution: Slackware 13.1, CentOS 5.5
Posts: 137

Rep: Reputation: 17
Move a file to some path and create that path when it doesn't exist


Hello Fellows,

Well, I guess the subject clear it all out.
But, let me add some more details..

I use this command:
Code:
find ./ -atime +360
to figure out the files that haven't been accessed since 360 days.

The command above will return results like this:

Code:
/uploads/2010/02/some-file-name.ext
/uploads/2009/08/another-file-name.ext
... etc
I'm taking here about tins of directories, thousands of files.

I'm looking to find a command that makes me able to move the results above to another path, and to create that path once it doesn't exist like below:
Code:
mv /uploads/2010/02/some-file-name.ext /old-files/uploads/2010/02/some-file-name.ext
But I want the executed command to create this path
Code:
/old-files/uploads/2010/02/
If it doesn't exist..

Any helpful thoughts.. will be more than appreciated..!!

Thanks..
- Hasan
 
Old 10-08-2010, 04:34 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Code:
for name in $(your find command here, with absolute search path); do

    newdir="/old-files/$(dirname "$name")"
    mkdir -p "$newdir"
    mv -t "$newdir" "$name"

done
I've not tested this, so for testing, maybe wise to insert an `echo` in the front of the mkdir and mv statements so you can see if the desired results are really going to occur. If it looks good, remove the echo statements.
 
1 members found this post helpful.
Old 10-08-2010, 05:17 PM   #3
WhisperiN
Member
 
Registered: Jun 2009
Location: Middle East
Distribution: Slackware 13.1, CentOS 5.5
Posts: 137

Original Poster
Rep: Reputation: 17
Oh, great..

The
Code:
dirname
Solved my whole issue..

I didn't know about that piece of command, GREAT JOB Fruit Girl

Now, simply my code will be like this:

Code:
....
for filename in `find uploads/ -atime +360`; do

    # Check if destination path doesn't exist, create it..
    if [[ ! -d old-files/`dirname $filename` ]]; then
          mkdir -p old-files/`dirname $filename`
    fi

    mv $filename old-files/$filename
done
....
Again, thanks a lot..!!!

- Hasan
 
Old 10-08-2010, 06:09 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Hi,

glad that helped. However, why are you doing that IF-THEN check to see if the directory exists? Just a waste of code there, because the -p option for `mkdir` will make the parent directories if they aren't there already, or if they already exist, it simply won't make them.

 
1 members found this post helpful.
Old 10-09-2010, 01:52 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Also, as you skipped the previous advice on using absolute paths, this script will react differently depending on where it is run.
 
Old 10-09-2010, 02:34 AM   #6
WhisperiN
Member
 
Registered: Jun 2009
Location: Middle East
Distribution: Slackware 13.1, CentOS 5.5
Posts: 137

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by GrapefruiTgirl View Post
Hi,

glad that helped. However, why are you doing that IF-THEN check to see if the directory exists? Just a waste of code there, because the -p option for `mkdir` will make the parent directories if they aren't there already, or if they already exist, it simply won't make them.

That's right, I totally agree
In fact, few more commands will be executed when the directory is not there other than the 'mkdir -p', So that's why I need the IF-THEN.
Thanks for the hint any way, and again for coming back..!!

You made Great here

Quote:
Originally Posted by grail View Post
Also, as you skipped the previous advice on using absolute paths, this script will react differently depending on where it is run.
Hello grail,
What you said, is a Sure thing..
I'm taking that in consideration, by placing a
Code:
cd path-to-the-meant-directory
Right before the 'find'.

Thanks for reminding, though.
 
Old 10-09-2010, 12:47 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Quote:
In fact, few more commands will be executed when the directory is not there other than the 'mkdir -p', So that's why I need the IF-THEN.
Well not to want to state the obvious, but if you run the 'mkdir -p' prior to all other commands then it would be impossible (almost) to not be there
 
  


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
bash script path issue - how to pass a path as a string to a variable PiNPOiNT Programming 5 04-17-2009 06:48 PM
Find a file path and directory path ak.lokesh Linux - Newbie 3 02-19-2009 01:37 PM
Help me...after used mv command all file missing only folder move to new path scoreone1 Linux - General 9 10-21-2008 12:22 AM
script to check $PATH directories exist Frustin Linux - Software 3 09-21-2004 01:20 PM
Cant move a folder to another path? dwarf007 Linux - Newbie 2 09-19-2004 03:28 AM


All times are GMT -5. The time now is 03:13 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration