pdir=`pwd`
if [ $# -lt 1 ]; then
echo current directory $pdir
ls -altr
echo
fi
for f in $*
do
# directory
if [ -d $f ]; then
echo current directory $f
cd $f
ls -latr
echo
fi
# but you can test file/dir
# regular file only
if [ -f $f ]; then
echo current directory $pdir
ls -altr $f
echo
fi
done
i have a basic code on shell script that accepts parameter for directory name with or without a filename or extension file name.
if i enter this below on the command line
sh dir_file_list.sh /u02/app/eatv/dev/out/*.txt
it give me this output:
current directory /export/home/ccalftdv/apps
-rw-r--r-- 1 ccalftdv ccalogrp 7 Jun 30 09:30 /u02/app/eatv/dev/out/
sample.txt
$
how do i capture the string *.txt or the filename on the input parameter? thanks.
If i have got your question right, to capture the filename, use the 'basename' command.
#basename /u02/app/eatv/dev/out/sample.txt
sample.txt
#
#!/bin/sh
pdir=`pwd`
if [ $# -lt 1 ]; then
echo current directory $pdir
ls -altr
echo
fi
for f in $*
do
# directory
if [ -d $f ]; then
echo current directory $f
cd $f
ls -latr
echo
fi
# but you can test file/dir
# regular file only
if [ -f $f ]; then
pfile=`basename $f`
echo current directory $pdir
ls -altr $pfile
echo
fi
done
thanks. i added the basename command and i was getting this error:
$ sh dir_file_list.sh /u02/app/eatv/dev/out/*.txt
current directory /export/home/ccalftdv/apps
ls: 0653-341 The file sample.txt does not exist.
$
Before you have the line "ls -altr $f", you need to run the 'cd' command to go to the current directory pdir.
#!/bin/sh
pdir=`pwd`
if [ $# -lt 1 ]; then
echo current directory $pdir
ls -altr
echo
fi
for f in $*
do
# directory
if [ -d $f ]; then
echo current directory $f
cd $f
ls -latr
echo
fi
# but you can test file/dir
# regular file only
if [ -f $f ]; then
pfile=`basename $f`
pdir=`dirname $f`
echo current directory $pdir
cd $pdir
ls -altr $pfile
echo
fi
done
thanks that works when i added the change directory cd command. lastly is to fix the display. when i run it gives me this directory listing where the display for the current directory was repeated.
$ sh dir_file_list.sh /u02/app/eatv/dev/out/*.txt
current directory /u02/app/eatv/dev/out
-rw-r--r-- 1 ccalftdv ccalogrp 7 Jun 30 09:30 sample.txt
current directory /u02/app/eatv/dev/out
-rw-r--r-- 1 ccalftdv ccalogrp 7 Jun 30 17:05 sample2.txt
current directory /u02/app/eatv/dev/out
-rw-r--r-- 1 ccalftdv ccalogrp 7 Jun 30 17:05 sample3.txt
$
it should look like this:
current directory /u02/app/eatv/dev/out
-rw-r--r-- 1 ccalftdv ccalogrp 7 Jun 30 09:30 sample.txt
-rw-r--r-- 1 ccalftdv ccalogrp 7 Jun 30 17:05 sample2.txt
-rw-r--r-- 1 ccalftdv ccalogrp 7 Jun 30 17:05 sample3.txt
---------- Post updated at 01:09 PM ---------- Previous update was at 12:38 PM ----------
pdir=`pwd`
if [ -f $# ]; then
pfile=`basename $#`
pdir=`dirname $#`
fi
if [ -d $# ]; then
pdir=`$#`
fi
echo current directory $pdir
if [ $# -lt 1 ]; then
ls -altr
echo
fi
for f in $*
do
# directory
if [ -d $f ]; then
cd $f
ls -latr
echo
fi
# but you can test file/dir
# regular file only
if [ -f $f ]; then
pfile=`basename $f`
pdir=`dirname $f`
cd $pdir
ls -altr $pfile
fi
done
i modified the script a bit and i was able to get the expected results.
$ sh dir_file_list.sh
current directory /export/home/ccalftdv/apps
total 152
drwxr-sr-x 3 ccalftdv ccalogrp 512 Sep 13 2006 dev
-rwxr-xr-x 1 ccalftdv ccalogrp 5586 Jun 08 15:55 cca_monthly_load_file
ksh
-rwxr--r-- 1 ccalftdv ccalogrp 5729 Jun 08 15:55 load_file.ksh
-rwxrwxrwx 1 ccalftdv ccalogrp 63 Jun 11 15:14 display_date.sh
-rwxr-xr-x 1 ccalftdv ccalogrp 680 Jun 25 08:50 cca_monthly_load_data
ini
-rw-r--r-- 1 ccalftdv ccalogrp 679 Jun 25 10:28 load_data.ini
drwxr-sr-x 4 ccalftdv ccalogrp 512 Jun 29 13:53 ..
-rw-r----- 1 ccalftdv ccalogrp 1298 Jun 30 08:26 dir_list_ccalloc_in.s
-rw-r----- 1 ccalftdv ccalogrp 1298 Jun 30 08:26 dir_list_ccalloc_out.
h
-rw-r----- 1 ccalftdv ccalogrp 166 Jun 30 09:32 sample.sh
-rw-r----- 1 ccalftdv ccalogrp 1357 Jun 30 09:39 dir_list_eatv_out.sh
-rw-r--r-- 1 ccalftdv ccalogrp 385 Jun 30 10:49 sample2.sh
-rw-r--r-- 1 ccalftdv ccalogrp 375 Jun 30 11:13 sample3.sh
-rw-r--r-- 1 ccalftdv ccalogrp 9 Jun 30 17:04 sample2.txtw
-rw-r--r-- 1 ccalftdv ccalogrp 454 Jul 01 12:54 dir_file_list.sh.bak
drwxr-sr-x 3 ccalftdv ccalogrp 512 Jul 01 12:54 .
-rw-r--r-- 1 ccalftdv ccalogrp 1595 Jul 01 12:58 dir_file_list.sh
$ sh dir_file_list.sh /u02/app/eatv/dev/out
current directory /export/home/ccalftdv/apps
total 32
drwxrwxr-x 4 oracle eatvgrp 256 Jun 19 09:41 ..
-rw-r--r-- 1 ccalftdv ccalogrp 7 Jun 30 09:30 sample.csv
-rw-r--r-- 1 ccalftdv ccalogrp 7 Jun 30 09:30 sample.txt
-rw-r--r-- 1 ccalftdv ccalogrp 7 Jun 30 17:05 sample2.txt
-rw-r--r-- 1 ccalftdv ccalogrp 7 Jun 30 17:05 sample3.txt
drwxrwxr-x 2 oracle eatvgrp 256 Jun 30 17:05 .
$ sh dir_file_list.sh /u02/app/eatv/dev/out/*.csv
current directory /export/home/ccalftdv/apps
-rw-r--r-- 1 ccalftdv ccalogrp 7 Jun 30 09:30 sample.csv
$ sh dir_file_list.sh /u02/app/eatv/dev/out/*.txt
current directory /export/home/ccalftdv/apps
-rw-r--r-- 1 ccalftdv ccalogrp 7 Jun 30 09:30 sample.txt
-rw-r--r-- 1 ccalftdv ccalogrp 7 Jun 30 17:05 sample2.txt
-rw-r--r-- 1 ccalftdv ccalogrp 7 Jun 30 17:05 sample3.txt
$
---------- Post updated at 03:38 PM ---------- Previous update was at 01:09 PM ----------
pdir=`pwd`
if [ $# -lt 1 ]; then
echo current directory $pdir
ls -latr
echo
else
p1=$1
if [ -d $p1 ]; then
pdir=$p1
echo current directory $pdir
cd $pdir
ls -latr
echo
elif [ -f $p1 ]; then
pdir=`dirname $p1`
echo current directory $pdir
cd $pdir
for f in $*
do
pfile=`basename $f`
ls -altr $pfile
done
else
echo $p1 not found
fi
fi
# put a white space
i revised the code a bit.