LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-31-2011, 06:33 AM   #1
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Rep: Reputation: 12
checking file name


Hi all

I need to check the file name e.g.
testbla_word.txt

is theire a command to search for only "bla_"?
so mainly, to check a part of a filename (not the entire filename, only a part of it)

Thx ^^
 
Old 03-31-2011, 06:51 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
You could use "ls *bla_*" to list all files containing that pattern. If some of the files are in subdirectories: find ./ -iname "*bla_*"

If you want to add these files in a bash array:
files=(*bla_*)

Now you can get the number of matches with:
${#files[@]}

You can access each filename individually:
echo ${file[0]}

for ((num=0; num<${#files[@]}; num++)); do
#do something with ${files[$num]}
done
 
Old 03-31-2011, 07:18 AM   #3
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
okay

let's try that
 
Old 03-31-2011, 07:28 AM   #4
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
this is what i got (i'm using KSH):
Code:
#!/usr/bin/ksh
ls *_error*
files=(*_error*)
${#files[@]}
echo ${file[0]}

for for ((num=0; num<${#files[@]}; num++)); do
echo tralala $num
done
output:
Code:
[root@host /]# ./check_test.sh
scp_cd.sh                                     100% 1225     1.2KB/s   00:00
bla_error.txt
start_error.out
./scp_cd.sh[15]: syntax error at line 16 : `(' unexpected
is it possible that your solution might not work because i use korn shell?
 
Old 03-31-2011, 10:10 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Definitely. My examples were for the bash shell.

Using ls or find should work.

I'm not familiar with the korn shell. It may have similar array notation. If you want to process a list of files produced by ls or find, look at using xargs as an option.

example:
find . -iname "*bla_*" -print0 | xargs -0 mv -t destdir/
 
Old 03-31-2011, 10:22 AM   #6
newlinuxuser11
LQ Newbie
 
Registered: Mar 2011
Posts: 9

Rep: Reputation: 0
Well, I'm not an expert by any stretch of imagination and I can only share the way of doing it as I learned with unixacademy DVD set. It is one-liner and much simpler than the examples above. There two tasks 1. list the files and 2. find the match. The simple command below will do it without problem. I'm sure there are more sophisticated ways of doing that, like using awk (I'm just starting with it), but this one should work for you, and it is easy to remember and to type when you need it next time.

ls| grep "text"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
checking path to file xwishmasterx Linux - Newbie 6 11-14-2010 06:54 PM
Any log file for checking file permission change history in RHEL 5.1? bilalcochin Linux - Newbie 3 04-02-2010 10:57 AM
checking sha1 file trueromance Fedora - Installation 3 12-25-2005 12:17 PM
Checking a file is complete. carlmarshall Linux - Software 3 12-14-2005 10:46 AM
Checking a file for numbers chrisk5527 Linux - General 1 09-12-2003 09:50 AM


All times are GMT -5. The time now is 01:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration