help: find and modified files script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help: find and modified files script
# 1  
Old 10-08-2008
help: find and modified files script

hello all im a newbie in the linux world ..i have just started creating basic scripts in linux ..i am using rhel 5 ..the thing is i wanted to create a find script where i could find the last modified file and directory in the directory given as input by the user and storing the output in a file so i tried

echo"enter the file you want to search in";
read " " >> a;
/usr/bin/find echo $a -mtime +1 -exec basename {} >> modifies_files.txt \

please help me on this asap ..i can understand there are certain flaws in the script and would greatly appreciate the help
thank you
kumar
# 2  
Old 10-08-2008
try using this..
Code:
 
read a?"enter the file you want to search in"
find . -name $a -mtime +1 -exec basename {} \;>>modifiedfiles

# 3  
Old 10-08-2008
I don't know if this *has* to use find... I hope a different approach is OK. The ls command has the ability to sort by modification time. For example, the command ls -alt will sort by mod time, showing long format for all files.

I hope tcsh is acceptable... that's the shell that happened to pop to mind...

Code:
#!/bin/tcsh

echo -n "What directory do you want to check? "
set dir = $<

ls -alt $dir | tail +2 | head -n 1

That will output to the shell. It looked like you already know how to modift to send the output to a file instead, so I won't worry about that. Also, you might need to massage the tail value a little to get it working with your OS. If you post the exact output of ls -alt from your command line, we should be able to whip it into shape.
# 4  
Old 10-08-2008
Code:
printf "%s: " "Enter the directory you want to search in"
IFS= read -r dir
cd "$dir" || exit 1
last_modified=$(
    ls -t | {
        IFS= read -r file
        printf "%s\n" "$file"
    } )

# 5  
Old 10-10-2008
thanks vidyasagar , treesloth and cfajohnson ..u guys are awesome and truly rockstars ..well treesloth i tried running ur script it gave me some errors here was one of the outputs frm ur script

"What directory do you want to check? /usr/bin/tail: cannot open `+2' for reading: No such file or directory"

the script looked like this
#!/bin/bash /(Changed from /bin/tcsh to /bin/bash)/
echo -n "What directory do you want to check? ";
set dir = $;

/bin/ls -alt $dir | /usr/bin/tail +2 | /usr/bin/head -n 1 ;

i edited the file and put the whole path of the ls tail and head commands .
and the output of command ls -lrt is

total 128
-rw-r--r-- 1 root root 4478 Sep 17 08:28 install.log.syslog
-rw-r--r-- 1 root root 32645 Sep 17 08:28 install.log
-rw------- 1 root root 1232 Sep 17 08:28 anaconda-ks.cfg
-rw-r--r-- 1 root root 480 Sep 24 06:38 gvustudentsbatch1.txt
-rw-r--r-- 1 root root 78 Sep 26 07:32 a.txt
-rw-r--r-- 1 root root 66 Sep 26 07:37 b.txt
drwxr-xr-x 9 root root 4096 Oct 8 16:04 Desktop
-rw-r--r-- 1 root root 0 Oct 8 16:25 tarun
-rw-r--r-- 1 root root 0 Oct 8 16:26 a
-rwxrwxrwx 1 root root 81 Oct 8 18:44 latest.sh
drwxr-xr-x 2 root root 4096 Oct 8 18:46 untitled folder
-rwxrwxrwx 1 root root 122 Oct 10 16:12 mod.sh
-rwxrwxrwx 1 root root 191 Oct 10 16:15 mod1.sh



cfajohnson i tried running ur script too ..it ran perfectly fine it solved first part of desired result which i wanted ...but the thing is .i ran your script , it was asking for a input from the user ...after that the script exited and returned nothing..as in the script ran it asked me to enter the name of the directory i wanted to find the modified files in ..then the output was blank
here is the output
[root@server1 ~]# ./mod1.sh /(i renamed the script as mod1.sh)/
Enter the directory you want to search in: /root /(i gave /root directory)/
[root@server1 ~]#
# 6  
Old 10-10-2008
help

guys it would be awesome ..if some one can guide me ...what should i do....
# 7  
Old 10-10-2008
Quote:
Originally Posted by tarunicon
cfajohnson i tried running ur script too ..it ran perfectly fine it solved first part of desired result which i wanted ...but the thing is .i ran your script , it was asking for a input from the user ...after that the script exited and returned nothing..as in the script ran it asked me to enter the name of the directory i wanted to find the modified files in ..then the output was blank

The script I posted stores the result in a variable so that you can work with it.

If you want to display the result, add a printf statement to do so.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Find files modified in previous minute only

Hi, How can I get files which are modified only in last minute ? it should not display 2 minutes back filels -la -rw-rw-r-- 1 stuser st 51 Dec 3 09:22 a.csv -rw-rw-r-- 1 stiser st 50 Dec 3 09:25 b.csv -rw-rw-r-- 1 stuser st 53 Dec 3 09:33 c.csv When I run command at 9:34am then I... (7 Replies)
Discussion started by: sbjv
7 Replies

2. Shell Programming and Scripting

Find list of files modified for a given day ?

find list of files modified for a given day ? if i have 10 files in my directory, i have modified only 5 ... how to display only modified files ? (1 Reply)
Discussion started by: only4satish
1 Replies

3. UNIX for Advanced & Expert Users

find files modified by hours instead of minutes

Is there an easy way to find files modified by hours? If you wanted to find something modified by like 28 hours then I know you could do this: find . -mmin -1440It is pain to break out a calculator and calculate in minutes. Could you do something similar to this? I know I don't have the right... (1 Reply)
Discussion started by: cokedude
1 Replies

4. UNIX for Dummies Questions & Answers

Find last modified date for many files

Hello all - I've looked and have not been able to find a "find" command that will list the last modified date of files within a specific directory and its subdirectories. If anyone knows of such a command it would be very much appreciated! If possible, I would like to sort this output and have... (5 Replies)
Discussion started by: MichaelH3947
5 Replies

5. UNIX for Dummies Questions & Answers

how to find the modified files before 60 mins?

hi, I need to find all the modified files before 60 minutes in a folder. Is that possible to find using mtime in minutes? Suggestions please. Thanks for looking into it... Geetha (8 Replies)
Discussion started by: iamgeethuj
8 Replies

6. Shell Programming and Scripting

find files modified more than a day

Hi All, I am using the below command to check the files modified within last 24hours find /home/karthik -mtime -1 -type f -exec ls -l {} \; What parameter do i need to add in the above command to check the files modified in last 2 or 3 days Kindly let me know if any other alternative... (2 Replies)
Discussion started by: karthikn7974
2 Replies

7. Shell Programming and Scripting

Shell script to find out 2 last modified files in a folder..PLZ HELP!!!!!!!!!

hi all, I need to find out the last 2 modified files in a folder.There is some way by which,we can check the timestamp and find out..??please help this is urgent. Thanks in Advance Anju (3 Replies)
Discussion started by: anju
3 Replies

8. UNIX for Advanced & Expert Users

find files modified in a specific month

hello i need a way to list files modified in a specific month and move them to a specific directry , i mean somthing like : find . -modifiedtime "May" -print -exec /usr/bin/mv newdirectory thank u (1 Reply)
Discussion started by: omer_ome
1 Replies

9. Solaris

find files modified in a specific month

hello i need a way to list files modified in a specific month and move them to a specific directry , i mean somthing like : find . -modifiedtime "May" -print -exec /usr/bin/mv newdirectory thank u (1 Reply)
Discussion started by: omer_ome
1 Replies

10. HP-UX

find files modified in a specific month

hello i need a way to list files modified in a specific month and move them to a specific directry , i mean somthing like : find . -modifiedtime "May" -print -exec /usr/bin/mv newdirectory thank u (1 Reply)
Discussion started by: omer_ome
1 Replies
Login or Register to Ask a Question