LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 12-26-2005, 09:51 AM   #1
LocoMojo
Member
 
Registered: Oct 2004
Distribution: Slackware 12
Posts: 165

Rep: Reputation: 30
Using ls


I have a directory containing .jpg files like this:

Quote:
jim@slack:~/xmas-05$ ls
_img_0241.jpg _img_0250.jpg _img_0259.jpg img_0246.jpg img_0255.jpg
_img_0242.jpg _img_0251.jpg _img_0260.jpg img_0247.jpg img_0256.jpg
_img_0243.jpg _img_0252.jpg _img_0261.jpg img_0248.jpg img_0257.jpg
_img_0244.jpg _img_0253.jpg _img_0262.jpg img_0249.jpg img_0258.jpg
_img_0245.jpg _img_0254.jpg img_0241.jpg img_0250.jpg img_0259.jpg
_img_0246.jpg _img_0255.jpg img_0242.jpg img_0251.jpg img_0260.jpg
_img_0247.jpg _img_0256.jpg img_0243.jpg img_0252.jpg img_0261.jpg
_img_0248.jpg _img_0257.jpg img_0244.jpg img_0253.jpg img_0262.jpg
_img_0249.jpg _img_0258.jpg img_0245.jpg img_0254.jpg
I'm trying to write a script to create a photo gallery.

In this directory the files that start with an underscore "_" are thumbnails and the files without the leading underscore are full size pictures.

In the script I'm writing I need to list only the full size pictures, the ones without the leading underscore. It's easy to list only the thumbnails by using:

Quote:
ls _*.jpg
But for the life of me I can't figure out how to list only the files without the leading underscore.

Any ideas?

Thanks.

LocoMojo

Last edited by LocoMojo; 12-26-2005 at 09:54 AM.
 
Old 12-26-2005, 09:54 AM   #2
ashamril
Member
 
Registered: Jan 2003
Location: cyberjaya
Distribution: rh mdk deb fed suse
Posts: 99

Rep: Reputation: 15
$ ls | grep -v _*.jpg
 
Old 12-26-2005, 10:01 AM   #3
LocoMojo
Member
 
Registered: Oct 2004
Distribution: Slackware 12
Posts: 165

Original Poster
Rep: Reputation: 30
Hi Ashamril,

Thanks for your reply, but your suggestion didn't work.

I got:

Quote:
jim@slack:~/xmas-05$ ls | grep -v _*.jpg
Binary file _img_0242.jpg matches
Binary file _img_0243.jpg matches
Binary file _img_0244.jpg matches
Binary file _img_0245.jpg matches
Binary file _img_0246.jpg matches
Binary file _img_0247.jpg matches
Binary file _img_0248.jpg matches
Binary file _img_0249.jpg matches
Binary file _img_0250.jpg matches
Binary file _img_0251.jpg matches
Binary file _img_0252.jpg matches
Binary file _img_0253.jpg matches
Binary file _img_0254.jpg matches
Binary file _img_0255.jpg matches
Binary file _img_0256.jpg matches
Binary file _img_0257.jpg matches
Binary file _img_0258.jpg matches
Binary file _img_0259.jpg matches
Binary file _img_0260.jpg matches
Binary file _img_0261.jpg matches
Binary file _img_0262.jpg matches
Any other ideas? I tried using ls | cut -c but that doesn't work either.

LocoMojo
 
Old 12-26-2005, 10:12 AM   #4
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
Try "ls [^_]*.jpg".
 
Old 12-26-2005, 10:16 AM   #5
LocoMojo
Member
 
Registered: Oct 2004
Distribution: Slackware 12
Posts: 165

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by btmiller
Try "ls [^_]*.jpg".
Hello btmiller,

That worked like a charm...thanks!!!

What does that mean? I want to read about it, where would I find information about it in the man pages? Is it regex?

Thanks again!

LocoMojo
 
Old 12-26-2005, 10:20 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You could use the file pattern [^_]*.jpg

i.e. ls [^_].jpg

The "carat" symbol negates the character match if it is the first symbol.

The grep example didn't work because you didn't have a wild card character after the underscore.
ls | '^_.*\.jpg'
There is a difference between the asterisk character in the shell, versus in a regular expression. An asterisk in the shell expands to any character or characters. In a regular expression, it expands to zero or more occurences of the previous character. Because the 'dot' character is used as a wild card in regular expressions, if you want to match the actual character, you need to escape it by preceding it with a backslash.
 
Old 12-26-2005, 10:33 AM   #7
LocoMojo
Member
 
Registered: Oct 2004
Distribution: Slackware 12
Posts: 165

Original Poster
Rep: Reputation: 30
jschiwal,

Thanks for that.

I learned something new. I didn't know about the different uses of wildcards in the shell and regular expressions.

I've never really gotten into learning about regular expressions. Obviously they're very useful so I should get into learning about it. Know of any good sources of information about regular expressions for beginners?

Thanks again.

LocoMojo
 
Old 12-27-2005, 11:50 AM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
The bash reference may have information on it.

Here is a Regular Expression tutorial on the web: http://www.grymoire.com/Unix/Regular.html

A Linux Focus Article: http://www.linuxfocus.org/English/Ju...article53.html

There is a book called "Linux Shells by Example" which may have the best examples and exercises on Regular Expressions. They are used for VIM, sed, awk and other programs.
http://www.amazon.com/gp/product/013...lance&n=283155

Also, the O'Reilly book "Sed & Awk" has a chapter on Regular Expressions. The 1st Edition is available on the web. If you google for it you may find it.

Here is an example that I have found useful. Suppose that you have downloaded several files in Pan, but because people aren't very original in naming them, you have many that look like: catpic.jpg catpic_copy_2.jpg catpic_copy_3.jpg.

There may be several like that, and some may be duplicates. If there are duplicates, you want to get rid of them.

Code:
for file in *_copy_[[:digit:]].jpg; do
cmp "${file}" "${file/_copy_[[:digit:]]/}" && rm "${file}"
done
I highlighted the metacharacter "[[:digit:]]" used. The "${file/_copy_[[:digit:]]/}" expression will expand "catpic_copy_2.jpg" to "catpic.jpg"

Last edited by jschiwal; 12-27-2005 at 11:56 AM.
 
  


Reply



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



LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:08 AM.

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
Open Source Consulting | Domain Registration