Visit Our UNIX and Linux User Community


Getting all file names in a folder.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Getting all file names in a folder.
# 1  
Old 06-28-2001
Error Getting all file names in a folder.

Hi All,
I need help to create a shell script that does following:

- do ls -1 and let all file names in the current folder
- do wc for each file and if wc > 0 then send a mail.

I am new to unix and do not know how to get filename one by one in a loop, so that wc can be done. I have done rest of the things but just looping all files is left.

Please help!

Thanks
# 2  
Old 06-28-2001
can you try this:
Code:
ls -1 > flist
for f in `cat flist`
do
  x=`wc $f | cut -c1-8`
  if [ x -gt 0 ]
  then
    echo "send mail"
  else
    echo "do nothing"
 fi
done

this leads me to ask what is the better way to
extract the character column from a wc command,
instead of cut -c1-8... any idea, anyone ??

HTH....any comment is appreciated. thanks.

added code tags for readability --oombera

Last edited by oombera; 02-19-2004 at 10:27 AM..
# 3  
Old 06-28-2001
Wonderful.

Quite not all, but your code gave me enough idea to implement this. Following is what I have done and its working great.

Thanks a lot for your help.

For your thinking about using cut, well I used awk as its easier and will give me complete field regardless of its size.
Code:
#!/bin/ksh

strPath=`echo "/app70s/d001/staging/Test/BadFiles/"$1`

for file in `ls -l $strPath | grep -v drwx | awk '{print $9}'`
do
   echo ":" $file
   intCount=`wc $strPath/$file | awk '{print $1}'`
   if [ $intCount != 0 ]
     then
       echo $file " contains bad records. Rows are failing." | mailx -s "Session Failed" mail.address@mail.server.com;
   fi
done

added code tags for readability --oombera

Last edited by oombera; 02-19-2004 at 10:27 AM..
# 4  
Old 06-28-2001
a problem with what you have writen is if the file/dir in question is not rwx for the owner. ie drx-


i would try something to the effect of
Code:
case "$1" in
'-d')
DIR=${2:?"bad or missing Directory"}
for filelist in `ls $DIR`;do
        filesize=`ls -l $DIR/$filelist|awk '{ print $5 }'`
        if [[ $filesize == 0 ]];then
                echo "$filelist is $filesize"|mailx -s "SUBJECT" user@host
        fi
done
;;

*)
        echo ""
        echo "Usage: for $0 [-d] /directory/to/search"
        echo ""
        ;;
esac


Last edited by Optimus_P; 06-28-2001 at 05:31 PM..
# 5  
Old 06-28-2001
Totally agree. Actually my application is custom for my environment and so I did not bothered about finding any dir because BadFiles folder should not have any sub-folders.

But what you told me is totally acceptable and I'm sure going to implement this. Just in case if someone creates a folder in future!

Thanks a lot for your inputs.

Adarsh
# 6  
Old 06-28-2001
ok i posted something completely diffearnt and works for any env.
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #968
Difficulty: Medium
In 2011. Kernel.org began offering Linux kernel release information in JSON format.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to tar/rsync/rm multiple folder names

hi all, i attach a link with what im trying to do automatically via script but i have some questions i need answering please, bear in mind i am really new to bash scripting, the only thing i know how to do is commands in scripts like cd rm tar rsync cp stuff like that i have mutiple project... (48 Replies)
Discussion started by: robertkwild
48 Replies

2. Shell Programming and Scripting

Exclude certain file names while selectingData files coming in different names in a file name called

Data files coming in different names in a file name called process.txt. 1. shipments_yyyymmdd.gz 2 Order_yyyymmdd.gz 3. Invoice_yyyymmdd.gz 4. globalorder_yyyymmdd.gz The process needs to discard all the below files and only process two of the 4 file names available ... (1 Reply)
Discussion started by: dsravanam
1 Replies

3. Shell Programming and Scripting

Checking Multiple File existance in a UNIX folder(Note: File names are all different)

HI Guys, I have some 8 files with different name and extensions. I need to check if they are present in a specific folder or not and also want that script to show me which all are not present. I can write if condition for each file but from a developer perspective , i feel that is not a good... (3 Replies)
Discussion started by: shankarpanda003
3 Replies

4. Shell Programming and Scripting

Remove trailing space from file and folder names

I have a folder that contains many sub folders and files. This tree has to be backed up to an archive system. According to the tech support, one of the archives is failing to back up due to the possibility of trailing spaces on file and folder names. Therefore, I would like to have a script... (16 Replies)
Discussion started by: vipertech
16 Replies

5. Homework & Coursework Questions

Bash script for printing folder names and their sizes

1. The problem statement, all variables and given/known data: The task is to create a script that would reproduce the output of 'du' command, but in a different way: what 'du' does is: <size> <folder name>and what is needed is <folder name> <size>We need to show only 10 folders which are the... (3 Replies)
Discussion started by: UncleIS
3 Replies

6. Shell Programming and Scripting

Bash script for printing folder names and their sizes

Good day, everyone! I'm very new to bash scripting. Our teacher gave us a task to create a script that basically does the same job the 'du' command does, with the difference that 'du' command gives an output in the form of <size> <folder name>and what we need is <folder name> <size>As for... (1 Reply)
Discussion started by: UncleIS
1 Replies

7. Shell Programming and Scripting

Renaming File Names in a folder/Dir

Hi Team, I'm new to Unix shell scripting . I've the following requirement A folder contains the list of files with the following format ab.name.11.first ab.name.12.second ab.name.13.third ---------- I have to rename the above file to like below ... (6 Replies)
Discussion started by: smile689
6 Replies

8. Shell Programming and Scripting

editing names of files in multiple folder

I have 1000's of directories which is named as numbers. Each directory contains multiple files. Each of these directories have a file named "att". I need to rename all the att files by adding the directory name followed by "_" then att for each of the directories. Directories 120 att... (2 Replies)
Discussion started by: Lucky Ali
2 Replies

9. Shell Programming and Scripting

Script to move files with similar names to folder

I have in directory /media/AUDIO/WAVE many .mp3 files with names like: my filename_01of02.mp3 my filename_02of02.mp3 Your File_01of06.mp3 Your File_02of06.mp3 etc.... In the same directory, /media/AUDIO/WAVE, I have many folders with names like 9780743579490 9780743579491 etc.. Inside... (7 Replies)
Discussion started by: glev2005
7 Replies

10. UNIX for Dummies Questions & Answers

Working with folder names

I have the following directory structure: /maindir /maindir/product1/ /maindir/product1/type1 /maindir/product1/type2 /maindir/product1/type3 /maindir/product2/ /maindir/product2/type1 /maindir/product2/type2 /maindir/product2/type3 /maindir/product2/type4 ... I'm able to traverse... (6 Replies)
Discussion started by: ricksj
6 Replies

Featured Tech Videos