Linux - NewbieThis 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
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.
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.
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.
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?
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"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.