|
Home > Archive > Unix Programming > December 2007 > Very small Shell Script Help...
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Very small Shell Script Help...
|
|
| kirankumar.techy@gmail.com 2007-12-10, 1:21 pm |
| The following Script takes each extension and determine what category
it belongs and then moves it into a directory based on the extension.
(for eg. 1.sh, 5.sh, 9.sh together; 4.csh, 120.csh, 6.csh together and
7.ksh, 2.ksh, 59.ksh together) and moves them to their respective
directories viz. sh, csh and ksh...
#!/usr/bin/ksh
for ext in ` ls -d *.* | awk -F. '{print $NF}' | sort -u `
do
mkdir -p $ext
mv *."$ext" "$ext"/.
done
The above Script sees for all similar extension, creates the
directories and moves Files to their corresponding directories.
I am trying to do it per extension only and not all at a time, like
take all files with ksh only and try to move them to ksh directory
while dont do anything to files with Extension sh and csh. That is,
the Shell parameter should be taken.
Can anyone give some inputs.
Thanks a lot in advance.
| |
| Rainer Weikusat 2007-12-10, 1:21 pm |
| kirankumar.techy@gmail.com writes:
> The following Script takes each extension and determine what category
> it belongs and then moves it into a directory based on the extension.
> (for eg. 1.sh, 5.sh, 9.sh together; 4.csh, 120.csh, 6.csh together and
> 7.ksh, 2.ksh, 59.ksh together) and moves them to their respective
> directories viz. sh, csh and ksh...
>
> #!/usr/bin/ksh
> for ext in ` ls -d *.* | awk -F. '{print $NF}' | sort -u `
> do
> mkdir -p $ext
> mv *."$ext" "$ext"/.
> done
>
> The above Script sees for all similar extension, creates the
> directories and moves Files to their corresponding directories.
>
> I am trying to do it per extension only and not all at a time, like
> take all files with ksh only and try to move them to ksh directory
> while dont do anything to files with Extension sh and csh. That is,
> the Shell parameter should be taken.
>
> Can anyone give some inputs.
Replace the *.* in the ls-call with *.$ext and pass $ext as argument?
| |
| Chris F.A. Johnson 2007-12-11, 1:37 am |
| On 2007-12-10, kirankumar.techy@gmail.com wrote:
>
> The following Script takes each extension and determine what category
> it belongs and then moves it into a directory based on the extension.
> (for eg. 1.sh, 5.sh, 9.sh together; 4.csh, 120.csh, 6.csh together and
> 7.ksh, 2.ksh, 59.ksh together) and moves them to their respective
> directories viz. sh, csh and ksh...
>
> #!/usr/bin/ksh
> for ext in ` ls -d *.* | awk -F. '{print $NF}' | sort -u `
> do
> mkdir -p $ext
> mv *."$ext" "$ext"/.
> done
>
> The above Script sees for all similar extension, creates the
> directories and moves Files to their corresponding directories.
>
> I am trying to do it per extension only and not all at a time, like
> take all files with ksh only and try to move them to ksh directory
> while dont do anything to files with Extension sh and csh. That is,
> the Shell parameter should be taken.
ext=$1 ## extension given as first argument
[ -d "$ext" ] || mkdir "$ext"
mv *."$ext" "$ext"
To move them all:
is_file()
{
for f in "$@"
do
[ -f "$f" ] && return
done
return 1
}
while is_file *.*
do
ext=${f##*.}
[ -d "$ext" ] || mkdir "$ext"
mv *."$ext" "$ext"
done
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
| |
| John Gordon 2007-12-11, 1:24 pm |
| In <493652f7-19a4-49b0-91b9-0e48795027c3@q3g2000hsg.googlegroups.com> kirankumar.techy@gmail.com writes:
> The following Script takes each extension and determine what category
> it belongs and then moves it into a directory based on the extension.
> (for eg. 1.sh, 5.sh, 9.sh together; 4.csh, 120.csh, 6.csh together and
> 7.ksh, 2.ksh, 59.ksh together) and moves them to their respective
> directories viz. sh, csh and ksh...
> #!/usr/bin/ksh
> for ext in ` ls -d *.* | awk -F. '{print $NF}' | sort -u `
> do
> mkdir -p $ext
> mv *."$ext" "$ext"/.
> done
> The above Script sees for all similar extension, creates the
> directories and moves Files to their corresponding directories.
> I am trying to do it per extension only and not all at a time, like
> take all files with ksh only and try to move them to ksh directory
> while dont do anything to files with Extension sh and csh. That is,
> the Shell parameter should be taken.
The whole purpose of the "for ext in ..." line is to discover all of the
file extensions in the current directory and operate on each one. If you
only want to operate on one at a time, then you only need the mkdir and
mv commands:
#!/usr/bin/ksh
mkdir -p "$1"
mv *."$1" "$1"/.
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
|
|
|
|
|