Move all but not the latest


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Move all but not the latest
# 1  
Old 08-16-2006
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

# 2  
Old 08-16-2006
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.
# 3  
Old 08-16-2006
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
# 4  
Old 08-16-2006
Try this :
Code:
ls -rt | grep -v '^X$' | sed '$d' | xargs -I{} mv {} X

Jean-Pierre.
# 5  
Old 08-16-2006
thanks

thanks it works
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to move latest zip file to another directory

Hi folks, In my application there is a job running which create a .dat file along with it zip file also at unix box location /opt/app/cvf/temp1 so in temp1 directory I have one .dat file and its zip file also. Now since this job runs every day so if a job runs today there will be two files... (5 Replies)
Discussion started by: punpun66
5 Replies

2. Shell Programming and Scripting

Shell script to get the latest file from the file list and move

Hi, Anybody help me to write a Shell Script Get the latest file from the file list based on created and then move to the target directory. Tried with the following script: got error. A=$(ls -1dt $(find "cveit/local_ftp/reflash-parts" -type f -daystart -mtime -$dateoffset) | head... (2 Replies)
Discussion started by: saravan_an
2 Replies

3. Solaris

Solaris latest

Am new to using Solaris. I wanna know about some of highly re usable and powerful scripts in tcsh. A kind of generic scripts. can anyone please help me out with any links or functions ?? Thanks in advance (8 Replies)
Discussion started by: Rahul619
8 Replies

4. Shell Programming and Scripting

create t a filelist with the latest file by YYYYMMDD and move to subfolder

Hi all, I am receiving files named like: ABC_20120730.csv ABC_20120801.csv ABC_20120812.csv They are residing in a folder named Incoming. I am supposed to write a script that will create a filelist which will contain only the name of the latest file beginning with ABC_, by YYYYMMDD... (3 Replies)
Discussion started by: kedrick
3 Replies

5. UNIX for Advanced & Expert Users

Getting Latest files

Hai I wolud like to know how to get the latest files. ex: file_ssss_00 file_ssss_01 i need to get file_ssss_01 files only. (in Unix script) Please give some idea ... (2 Replies)
Discussion started by: raju4u
2 Replies

6. Shell Programming and Scripting

Move the latest or older File from one directory to another Directory

I Need help for one requirement, I want to move the latest/Older file in the folder to another file. File have the datetimestamp in postfix. Example: Source Directory : \a destination Directory : \a\b File1 : xy_MMDDYYYYHHMM.txt (xy_032120101456.txt) File2: xy_MMDDYYYYHHMM.txt... (1 Reply)
Discussion started by: pp_ayyanar
1 Replies

7. Shell Programming and Scripting

To get the latest file

Hi Experts Team, I wish to store the latest file name of partcular pattern in the remote server in a variable. i tried this LATEST_FILE=`ssh ${USER_ID}@${REMOTE_HOSTNAME} 'ls -t ${SOURCE_DIRECTORY}/${SOURCE_FILEPATTERN}'` but its nt working..pls guide me.. Regards, Kanda (2 Replies)
Discussion started by: spkandy
2 Replies

8. Shell Programming and Scripting

get latest file

i have the following in my directory x: 3 files with the word "LIST" inside the files 2 files without the word "LIST" 1 folder (sudirectory) i want to get the filename of the latest file (timestamp) with the word "LIST". by the way the script and the list of files are in seperate... (4 Replies)
Discussion started by: inquirer
4 Replies

9. Shell Programming and Scripting

How do I get the name of latest file?

1) How do I get the name of latest file in a variable? 2) Is it safe to delete all files from a dir I am doing cd $dir_name if return_code > 0 rm * fi what are other alternates to delete all files from a dir in a shell script? :) (5 Replies)
Discussion started by: Hangman2
5 Replies

10. UNIX for Dummies Questions & Answers

latest Unix

hi all, someone tell me which is the latest object oriented, easy to use and handle, Unix version/release and from where to get it. have my thanks in advance if you ll also mention some good books regarding Unix's administration and use for a perfect newbie. (7 Replies)
Discussion started by: irfanets
7 Replies
Login or Register to Ask a Question