The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Getting latest file from ftp arunavlp UNIX for Dummies Questions & Answers 3 02-08-2008 06:19 AM
latest Unix irfanets UNIX for Dummies Questions & Answers 7 07-28-2007 10:24 PM
latest unix irfanets UNIX for Advanced & Expert Users 1 07-27-2007 04:26 AM
latest version of bsd chilaka BSD 1 12-18-2006 04:24 AM
get latest file inquirer Shell Programming and Scripting 3 07-06-2003 09:44 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-15-2006
Registered User
 

Join Date: Jul 2006
Posts: 66
Stumble this Post!
Move all but not the latest

what is way to not move a latest file in a a particular folder X.

There are number of files in the folder, I donot want to move the latest file into another folder.

Can anyone figure out where is wrong in the script.

Code:
lastfile=$(ls -rt | tail -1)

 for allfile in $(ls | grep -v |$lastfile)
 do
     echo > $allfile
    mv <all existing file> X
done
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 08-16-2006
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,211
Stumble this Post!
Remove the last pipe character in your for command :
Code:
lastfile=$(ls -rt | tail -1)
lastfile=$(ls -rt | tail -1)
for file in $(ls | grep -v $lastfile)
do
    echo $lfile
    mv $file X
done
Another way to do the work :
Code:
for file in $(ls | sed '$d')
do
    echo $file 
    mv $file X
done
You can also do the work without for loop :
Code:
ls -rt | sed '$d' | xargs -I{} mv {} X
If the X directory is in the current directory you will get an error because you will try to move X to itself.

Jean-Pierre.
Reply With Quote
  #3 (permalink)  
Old 08-16-2006
Registered User
 

Join Date: Jul 2006
Posts: 66
Stumble this Post!
ls -rt | sed '$d' | xargs -I{} mv {}

Thats true

I am inside the folder Y inwhich folder X is there.
all files are there inside the Y and want to move into X
pwd return Y
I am executing in this folder only.
Code:
ls -rt | sed '$d' | xargs -I{} mv {} X
return
mv: cannot rename archive to X/X: Invalid argument
Then what should be best way to move these into Y
can you suggest ..do I need to call this from top folder ie above X and Y
Reply With Quote
  #4 (permalink)  
Old 08-16-2006
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,211
Stumble this Post!
Try this :
Code:
ls -rt | grep -v '^X$' | sed '$d' | xargs -I{} mv {} X
Jean-Pierre.
Reply With Quote
  #5 (permalink)  
Old 08-16-2006
Registered User
 

Join Date: Jul 2006
Posts: 66
Stumble this Post!
thanks

thanks it works
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:18 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0