Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash "for file in" exception

Tags:

bash

shell

I got this

for file in *; do
    any command
done

What I want to do is add an exception to the "for file in *; do".

Any ideas?

like image 959
Alejandro Ar Avatar asked Jun 24 '26 05:06

Alejandro Ar


2 Answers

If you wanted to skip files with a particular extension, for example, ".pl", you could do:

for file in *
do
    [ "${file##*.}" != "pl" ] && echo $file
done
like image 162
JRFerguson Avatar answered Jun 26 '26 21:06

JRFerguson


One way to do what I think you are asking is to loop through and check a file name with a if statement (or just grep -v the ls cmd):

for file in `ls`; do
    if [ "$file" == "something" ]; then
        # do something
    else
        # something else
    fi
done
like image 38
chown Avatar answered Jun 26 '26 20:06

chown