LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 05-14-2009, 10:39 AM   #1
Hi_This_is_Dev
Member
 
Registered: May 2009
Location: India
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254

Rep: Reputation: 18
Question Linux classes [:keyword:]


Hi All,


The rm command removes all files whose names are in lower case:

Code:
[root@localhost ~]# touch abc
[root@localhost ~]# touch a


[root@localhost ~]# rm [[:lower:]]
rm: remove regular empty file `a'? y
however, as you can see that the file "abc" is not being prompted for removal.

So, the question is: How to make use of Linux classes that can work with strings, that is file/directory names consisting of more than one character?


Thanks in advance!

Dev.
 
Old 05-14-2009, 10:45 AM   #2
dave.donaghy
LQ Newbie
 
Registered: Apr 2009
Posts: 6

Rep: Reputation: 1
A * indicates "one or more of the preceding character".

Since, in your example, the "preceding character" is a class consisting of any lower-case alphabetic character, you can use this:

# echo rm [[:lower:]]*

(I always find that the echo keeps things safe, especially when doing something dangerous, like rm with wildcards, until I'm sure it's correct.)

... and then this ...

# rm [[:lower:]]*
 
Old 05-15-2009, 07:47 AM   #3
Hi_This_is_Dev
Member
 
Registered: May 2009
Location: India
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254

Original Poster
Rep: Reputation: 18
Thumbs up

Hi Dave,

Thanks! It serves the purpose!

Here is my finding:

Code:
[root@localhost ~]# rm [[:lower:]]*
rm: remove regular empty file `a'? y
rm: remove regular file `anaconda-ks.cfg'? n
rm: remove regular empty file `d'? n
rm: remove regular file `data'? 
[root@localhost ~]#

Quote:
Originally Posted by dave.donaghy View Post
A * indicates "one or more of the preceding character".

Since, in your example, the "preceding character" is a class consisting of any lower-case alphabetic character, you can use this:

# echo rm [[:lower:]]*

(I always find that the echo keeps things safe, especially when doing something dangerous, like rm with wildcards, until I'm sure it's correct.)

... and then this ...

# rm [[:lower:]]*
 
Old 05-15-2009, 08:02 AM   #4
Hi_This_is_Dev
Member
 
Registered: May 2009
Location: India
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254

Original Poster
Rep: Reputation: 18
Now here is another scenario:

1) Delete all files which have no extension names- do keep the ones which have them.

2) Delete all files which have extensions names- don't delete the ones which have them.

*Note: Using dot (.) in the criteria may not server the purpose in cases where a file name has multiple dots in it.


Example# 1:
Code:
[root@localhost ~]# ls
0    44   anaconda-ks.cfg  h                   PingReport
1    5    d                hi                  Ping Report for the Date of:
11   556  data             install.log         PingStatus.txt
2    56   Desktop          install.log.syslog  s
22   6    e                jj                  t
3    7    er               k                   work
333  8    f                logout-script.txt   z
4    9    g                names
[root@localhost ~]# rm *.*
rm: remove regular file `anaconda-ks.cfg'? n
rm: remove regular file `install.log'? n
rm: remove regular file `install.log.syslog'? n
rm: remove regular file `logout-script.txt'? n
rm: remove regular file `PingStatus.txt'? n
[root@localhost ~]#

Example# 2: This includes only those file names which begin with a dot (.)
Code:
[root@localhost ~]# echo rm .* | sort
rm . .. .abc.swp .bash_history .bash_logout .bash_profile .bashrc .cshrc .data.swp .dmrc .eggcups .esd_auth .gconf .gconfd .gnome .gnome2 .gnome2_private .gstreamer-0.10 .gtkrc-1.2-gnome2 .ICEauthority .lesshst .metacity .mozilla .nautilus .recently-used .recently-used.xbel .redhat .tcshrc .thumbnails .Trash .xsession-errors
[root@localhost ~]#
 
Old 05-15-2009, 08:17 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
Whenever you need to change the meaning of a character, the typical thing is to "escape" it with "\". The details depend on the context, but---in general---the use of "\" toggles the meaning of a character from whatever the default is in that situation. You can slog thru something like the Advanced Bash Scripting Guide---or you can simply experiment.

One example:

sed 's/./DOT/g' ##replaces all single characters with "DOT"

sed 's/\./DOT/g' ##replaces all literal "."s with "DOT"

(Obviously?) this only applies to characters which may have a special meaning in the context.....
 
Old 05-15-2009, 08:42 AM   #6
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
FYI. In your first example, rm [[:lower:]]* will remove files that start with a lower case letter. The "*" in the shell globs "zero or more characters". It isn't used as in grep, sed or awk where it means "zero or more of the preceding character". You can however enable normal regular expressions with the "extglob" option.

Also, in another post, you were looking at hidden files. Be careful if you try to delete hidden files, because ".*" will also match ".." which is the parent directory.
Use ".[^.]*" instead.

There is another gotcha in the order of characters. The $LC_CTYPE locale variable can effect which characters match a regular expression.
 
Old 05-15-2009, 09:23 AM   #7
Hi_This_is_Dev
Member
 
Registered: May 2009
Location: India
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254

Original Poster
Rep: Reputation: 18
Okay, pixellany and jschiwal. I will try that tomorrow because my tummy is aching so much because I have not taken any meals today. I must leave the lab now!
 
  


Reply

Tags
classes, linux


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
how to grep a "keyword" beside another keyword? xiawinter Linux - Software 7 12-29-2007 01:50 AM
Linux Classes dudeman41465 Linux - Software 4 10-07-2005 04:20 AM
Linux Certification classes? wardialer Linux - Certification 2 04-26-2005 03:54 AM
OOP (PHP) classes and extended classes ldp Programming 3 03-05-2005 12:45 PM
Linux Classes and Schools hondaman Linux - Software 3 12-08-2004 06:35 PM


All times are GMT -5. The time now is 05:44 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration