Bash, how do I test for *.txt files in a directory with IF?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Currently: RHEL/Centos 5.x-6.x servers, Mint Desktop
Posts: 41
Rep:
Bash, how do I test for *.txt files in a directory with IF?
Hello, I have a BASH script to process *.txt files in a directory.
The script works fine, but I would like to put the main module inside an IF statement to test for *.txt files. I thought this would be a simple:
if [ -e /data/files/*.txt ] ...
but that doesn't seem to work with a wildcard.
[z535104@bmcsnt01 Servers]$ ls *.sh
test.sh
[z535104@bmcsnt01 Servers]$ if [ -e ./*.sh ]
> then
> echo true;
> else
> echo false;
> fi
true
[z535104@bmcsnt01 Servers]$ ls *.toad
ls: *.toad: No such file or directory
[z535104@bmcsnt01 Servers]$ if [ -e ./*.toad ]
> then
> echo true;
> else
> echo false;
> fi
false
Distribution: Currently: RHEL/Centos 5.x-6.x servers, Mint Desktop
Posts: 41
Original Poster
Rep:
That if [ -e ./*.txt ] only works if there is ONE file in the directory; if there are two or more, the IF statement doesn't work.
In the module, I want to put the whole FOR statement inside an IF condition.
Okay, here is my script module:
# .txt file mover
txtprep_txtmover() {
for x in /data/138files/*.txt
do
fprefix=$(basename ${x} .txt) # gets file prefix
fname=${fprefix#*-*} # Get form name
cname=${fprefix%*-*} # Get company name
echo "Processing $fprefix" >> "$REPORTPATH/txtpreplog.txt"
# Convert company name to uppercase
cname=`echo $cname | tr a-z A-Z`
# Set form destination directory for Variform
DESTDIR="$BASEPATH/$cname/$fname"
#Test for directory, if not there, note in txtpreplog.txt
if [ -d "$DESTDIR" ]
then
# note exception data file for PS2PDF processing
echo "$DESTDIR $fprefix.txt" >> "$REPORTPATH/fileloc.txt"
mv $x $DESTDIR
else
echo "No Destination $DESTDIR" >> "$REPORTPATH/txtpreplog.txt"
fi
done
}
Distribution: Currently: RHEL/Centos 5.x-6.x servers, Mint Desktop
Posts: 41
Original Poster
Rep:
Okay, I think I understand now-
in " if [ `ls ./*.sh|wc -l` -gt 0 ]"
`ls ./*.sh|wc -l` is getting a directory list of each .sh file, wc -l is counting each line of the ls output, one line for each file. The output of the line altogether is an integer, 0 for no files, etc.
The IF condition is testing the number of files being greater than (-gt) zero.
Originally posted by severian23 That does work, can you tell me what you did in the IF condition?
I'd like to understand the logic of it.
Sure..
Basicly looking for any files that met the test condition.. with the ls command
Pipe that through Wordcount -lines.. that gives you an number.
That number -greaterthan 0 pulls a true
if [ `ls ./*.sh|wc -l` -gt 0 ] # list all sh files , then count them. back ticks force this to happen first... then you get your 'test' using numbers.
> then
> echo true
> else
> echo false
> fi
true
[z535104@bmcsnt01 Servers]$ ls *.sh
1.sh 2.sh test.sh
Hope that helps you out..
Sorry if I was too basic, I don't know what level of experiance you have...
If you have any other questions.. Just letme know..
.
v
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.