LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Using ls (https://www.linuxquestions.org/questions/linux-newbie-8/using-ls-396549/)

LocoMojo 12-26-2005 09:51 AM

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

ashamril 12-26-2005 09:54 AM

$ ls | grep -v _*.jpg

LocoMojo 12-26-2005 10:01 AM

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

btmiller 12-26-2005 10:12 AM

Try "ls [^_]*.jpg".

LocoMojo 12-26-2005 10:16 AM

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

jschiwal 12-26-2005 10:20 AM

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.

LocoMojo 12-26-2005 10:33 AM

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

jschiwal 12-27-2005 11:50 AM

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"


All times are GMT -5. The time now is 10:18 PM.