nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

script finding file and sorting

This is a discussion on script finding file and sorting within the Shell scripting forums, part of the Development/Scripting category; Hi, I would need some help I`m pretty new to script writing...I`ll appreciete any help.. I need to find any ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 07-14-2006, 05:42 PM
Junior Member
User
 
Join Date: Jul 2006
Posts: 10
Rep Power: 0
dementeddude
Default script finding file and sorting

Hi,

I would need some help I`m pretty new to script writing...I`ll appreciete any help.. I need to find any files which has a big letter H on the end of the filename and put it in a new directory with todays date... I really dont know where to begin....

Thank you
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-14-2006, 08:55 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Ubuntu
Posts: 1,036
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Here is the script:

Code:
#!/bin/bash
SEARCHDIR="$1"
DESTDIR="$2"
list=$(find $SEARCHDIR -name "*[H$]" -print)
TODAY=$(date +"%d-%m-%y")
for f in $list
do
  d=$(dirname $f)
  ff=$(echo $f | awk -F"$d" '{ print $2}')
  [ ! -d $DESTDIR ] && mkdir -p $DESTDIR || :
  echo "Coping file: $f  => ${DESTDIR}${ff}-$TODAY"
  cp $f ${DESTDIR}${ff}-$TODAY
done
How to run?
Save above script as script.sh. Run following command:

Code:
chmod +x script.sh
./script.sh /tmp /data
Now script will look for any files which has a big letter H on the end of the filename in /tmp directory and copy all those files to /data directory with todays date.

Let me know..
__________________
Vivek | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
  #3 (permalink)  
Old 07-15-2006, 12:03 AM
Junior Member
User
 
Join Date: Jul 2006
Posts: 10
Rep Power: 0
dementeddude
Default

wow.. thank you I`ll check it out right away... What should I need to change to for example I would always create a files named 0714filenameH.doc, 0715filenameH.doc etc. and I would like to find these files and make a folder from the first four char and put the mathcing files in that folder?
THank you once again, it was a great help, I just started to learn shell scripting and I`m a bit confused yet but I think it is a very powerfull stuff This forum is great.
Reply With Quote
  #4 (permalink)  
Old 07-15-2006, 12:57 AM
tom tom is offline
Contributors
User
 
Join Date: Jun 2005
Location: London, UK
Posts: 213
Rep Power: 4
tom is on a distinguished road
Default

You need to modify script as per your requirements, as it does not make directories from first four characters.

It seems that variable ff stores actual filename, you need to pull first character from variable ff using cut command. For example following will print out first four character
Code:
echo 0714filenameH.doc | cut -b1-4
You can cut it from $ff variable and store to myDir variable:
Code:
mydir=$(echo $ff | cut -b1-4)
And then create a directory:
Code:
mkdir -p $mydir
Finally cp command would be:
Code:
cp $f ${mydir}${ff}-$TODAY
Hope you can assembled rest of script

Quote:
This forum is great.
Welcome to our community!
Reply With Quote
  #5 (permalink)  
Old 07-15-2006, 02:06 AM
Junior Member
User
 
Join Date: Jul 2006
Posts: 10
Rep Power: 0
dementeddude
Default

Thank you guys.. I hope I can do the rest. I hope someday I can help others like you helped me.
Reply With Quote
  #6 (permalink)  
Old 07-20-2006, 09:38 PM
Junior Member
User
 
Join Date: Jul 2006
Posts: 10
Rep Power: 0
dementeddude
Default

I have managed the script to work like this:

#!/bin/bash
SEARCHDIR="$1"
DESTDIR="$2"
list=$(find $SEARCHDIR -name "*[HH]*.doc" -print)

for f in $list
do
d=$(dirname $f)
ff=$(echo $f | awk -F"$d" '{ print $2}')
mydir=$(echo $ff | cut -b1-5)
mkdir $DESTDIR/$mydir
echo "Coping file: $f => ${mydir}${ff}"
cp $f $DESTDIR/${mydir}${ff}
done

there is only one problem when there is a space in a folder name than it doesnt work like: " Some Directory Name"
Can it be fixed or the directory names must be one word?
Thank you
Reply With Quote
  #7 (permalink)  
Old 07-21-2006, 03:11 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Ubuntu
Posts: 1,036
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

You need to read files on fly and copy them using some tricks:

Code:
#!/bin/sh
SEARCHDIR="$1"
DESTDIR="$2"

find $SEARCHDIR-name "*[HH]*.doc" -exec echo '{}'  \; | while read LINE
do
f="$LINE"
d=$(dirname "$f")
ff=$(echo "$f" | awk -F"$d" '{ print $2}')
mydir=$(echo $ff | cut -b1-5)
mkdir $DESTDIR/$mydir
Coping file: $f => $DESTDIR/${mydir}${ff}
cp "$f" "$DESTDIR/${mydir}${ff}"
done
hope this helps. Read above code carefully till u understand it very well...
__________________
Vivek | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
  #8 (permalink)  
Old 07-21-2006, 03:54 AM
Junior Member
User
 
Join Date: Jul 2006
Posts: 10
Rep Power: 0
dementeddude
Default

Quote:
Originally Posted by nixcraft
You need to read files on fly and copy them using some tricks:

Code:
#!/bin/sh
SEARCHDIR="$1"
DESTDIR="$2"

find $DESTDIR -name "*[HH]*.doc" -exec echo '{}'  \; | while read LINE
do
f="$LINE"
d=$(dirname "$f")
ff=$(echo "$f" | awk -F"$d" '{ print $2}')
mydir=$(echo $ff | cut -b1-5)
mkdir $DESTDIR/$mydir
Coping file: $f => $DESTDIR/${mydir}${ff}
cp "$f" "$DESTDIR/${mydir}${ff}"
done
hope this helps. Read above code carefully till u understand it very well...
I`ll study it from line to line..
I think it should be "find $SEARCHDIR -name "*[HH]*.doc" -exec echo '{}' \; | while read LINE"
Thank you for a great help... BTW do you know some online tutorials or books about shell scripting?
Thanks again

Regards, Norbert
Reply With Quote
  #9 (permalink)  
Old 07-21-2006, 05:14 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Ubuntu
Posts: 1,036
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Quote:
I think it should be "find $SEARCHDIR -name "*[HH]*.doc" -exec echo '{}' \; | while read LINE"
Err it should be $SEARCHDIR.. to be frank i just typed above script i.e. not tested on system... but i guess you got the logic

Try out following url for more on shell scripting:
http://www.cyberciti.biz/nixcraft/li...features/lsst/
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/abs/html/

Read man page of bash and other command

Books:
Book : UNIX Shells by Example 4th Edition
Author : Ellie Quigley
Published by Prentice Hall PTR.

Mastering UNIX Shell Scripting
Author : Randal K. Michael
Published by Wiley Press

hope this helps
__________________
Vivek | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
  #10 (permalink)  
Old 07-21-2006, 12:07 PM
Junior Member
User
 
Join Date: Jul 2006
Posts: 10
Rep Power: 0
dementeddude
Default

Thank you.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
shell script for ftp the file vishal_titre Shell scripting 3 12-10-2007 09:40 AM
Finding machines S/N and Model Type mlperry Linux software 3 10-05-2006 11:15 PM
finding files in linux chiku Linux software 2 08-03-2006 10:23 AM
Sorting individual records in a file based on some fields cucu81 Shell scripting 1 07-25-2006 04:08 AM
shell script for finding users not logged on for last 10 day ganes Shell scripting 4 07-06-2005 12:01 PM


All times are GMT +5.5. The time now is 06:30 PM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36