Copy the latest (last file) in given folder


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Copy the latest (last file) in given folder
# 1  
Old 07-07-2011
Copy the latest (last file) in given folder

Code:
#!/bin/bash

for i in {1..1536..1}
do
    #find  /home/test/Desktop/up111/workplace/Malware/$i/logs

    for a in  /home/test/Desktop/up111/workplace/Malware/$i/logs/*
    do
        #max=a
        for b in  /home/test/Desktop/up111/workplace/Malware/$i/logs/* 
        do  
            echo "$a and $b are getting compared"
             # var1 contains size of file a
            var1=$(stat -c%s $a)
            # var2 contains size of file b
            var2=$(stat -c%s $b)
            if [[ "$var1" -lt "$var2" ]]
            then
                $a = $b
            
            fi
            
        done
    done

    cp /home/test/Desktop/up111/workplace/Malware/$i/logs/$max -R  /home/test/Desktop/up111/workplace/pre11
done

---------- Post updated at 05:57 AM ---------- Previous update was at 05:55 AM ----------

this is code for copying largest file inside given folder.

Last edited by vbe; 07-07-2011 at 08:44 AM.. Reason: Please use code tags!
# 2  
Old 07-07-2011
Im maybe tired..., but when is max ( in your cp line...) given a value?
Then I dont get the point between the script I see and your request...If you want the last modified file in a directory, why not just
Code:
 ls -t <dir>| head -1

?
# 3  
Old 07-07-2011
Code:
 
ls -l | grep -v "^d" | sort -r -k5 | awk '{if(NR==1)print $NF}'

# 4  
Old 07-08-2011
Quote:
Originally Posted by upvan111
Code:
for i in {1..1536..1}

This syntax is not working in my bash (GNU bash, version 3.2.39). However, {1..1536} works, prints no from 1 to 1536.
# 5  
Old 07-11-2011
- it simply (i=1;i<=1536;i++);
{1..1536..n}denotes (i=1;i<=1536;i+=n);
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Retrieve the Latest file in a folder using SFTP

HI Guys, Can anyone help me to retrieve the latest file from below example using SFTP I have below list of files in a folder v403t.lstprgms.sortin1 val027.d099.fwest.oct2711 xcelrptd.d1400sqp.dec1713.t040459.@02del xcelrptd.d1400sqp.dec1713.t073308.@02del... (3 Replies)
Discussion started by: heye18
3 Replies

2. Shell Programming and Scripting

Copy specific file (different but same name) as folder name

I have to copy a particular file present in a main folder having part of the file-name present in many sub-folders to a new destination preserving the name of the source "part of the main folder" and previous file-name of the output file: Example: From /005_0/1000/005.xxx ->... (7 Replies)
Discussion started by: wappor
7 Replies

3. UNIX and Linux Applications

Need to copy the latest file from Unix server to Shared folder

Hi All, One job in unix server will generate .csv files daily. I need to copy the latest of these .csv file from the unix server to the shared drive/folder in windows through unix script. My shared folder will look something like W:\some folder(for example). Could any one of you please help... (3 Replies)
Discussion started by: jaya@123
3 Replies

4. Shell Programming and Scripting

Copy latest generated file

Hi, There is csv file generated at /usr/data on server1 on monthly basis. It is in the format reportYYYYDD(e.g 201105). I needed a script which would copy the latest generated file from the location to another server at /usr/loc Please can you help? (2 Replies)
Discussion started by: Alok Ranjan
2 Replies

5. Shell Programming and Scripting

Need to copy latest file using SFTP

Hi All, In my unix server, I have the following files: h1.txt h2.txt h3.txt and through SFTP i need to copy only the latest file to another unix server. Can you please let me know what command i need to use. Thanks in Advance, (2 Replies)
Discussion started by: HemaV
2 Replies

6. Shell Programming and Scripting

copy the latest file in the remote server's directory

Hi Expert Team, I performed the below piece of code to copy the latest file in the remote server's directory to the same server's other directory. But it is not working properly. How can i handle this? Can you please help me..? ssh ${REMOTE_USERID}@${REMOTE_HOSTNAME} "cp -p `ssh... (3 Replies)
Discussion started by: spkandy
3 Replies

7. Shell Programming and Scripting

Copy the latest file to a directory

Hi Team, I wish to copy the latest file of pattern "MyFile*" to some other location. I need to do all the operation in a single command separated by |. ls -rt <MyFile*> | tail -1 | <copy command>. How can I do? Please help me. Thanks, Kanda (2 Replies)
Discussion started by: spkandy
2 Replies

8. Shell Programming and Scripting

Copy the latest file from one directory to another

Hi All, I am in the directory a/b/processed the files in this directories are -rw-r--r-- 1 owb users 330 Aug 8 chandantest.txt_08082008 -rw-r--r-- 1 owb users 220 Aug 7 chandantest.txt_07082008 -rw-r--r-- 1 owb users 330 Aug 6... (3 Replies)
Discussion started by: chandancsc
3 Replies

9. Shell Programming and Scripting

Copying latest file into a folder

Hello all, this is my first post while i am trying to understand unix. I would basically like to know if i can do this: Lets say i have a folderA and folderB And i save something in folderA Can i make a script that checks folderA latest file, then compares it with the date of latest file in... (16 Replies)
Discussion started by: takissd
16 Replies

10. UNIX for Dummies Questions & Answers

Copy the latest file from a folder

Hi, I have a problem. I have some text files in a folder. The names can be like: emp_20080307053015.dat emp_20080306053015.dat emp_20080305053015.dat emp_20080304053015.dat The date format appended is like yyyymmdd and timestamp. What i need is i have to copy the latest file every... (3 Replies)
Discussion started by: Aswarth
3 Replies
Login or Register to Ask a Question