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 ...
|
|||||||
| Register | FAQ | Members List | Calendar | Forgotten your password? | Mark Forums Read |
|
|||
|
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 |
| Sponsored Links | ||
|
|
|
|||
|
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 |
|
|||
|
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 Code:
mydir=$(echo $ff | cut -b1-4) Code:
mkdir -p $mydir Code:
cp $f ${mydir}${ff}-$TODAY
Quote:
|
|
|||
|
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 |
|
||||
|
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
|
|
|||
|
Quote:
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 |
|
||||
|
Quote:
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 |
![]() |
| Bookmarks |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| 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 |