USA Linux Users Group Forum Index
Log in Register FAQ Memberlist Search USA Linux Users Group Forum Index Album

Regular expressions in command line arguments

 
Post new topic   Reply to topic   printer-friendly view    USA Linux Users Group Forum Index » System Administration and Security
View previous topic :: View next topic  
Author Message
eggmatters
Jr. Member


Joined: 17 Oct 2024
Posts: 60

PostPosted: Tue Mar 11, 2024 8:13 pm    Post subject: Regular expressions in command line arguments Reply with quote

I was curious if, in bash, you can use complex regular expressions within your command line arguments. Bash supports the * wildcard but let's say I have a list of files with a prefix, a number, and an extension. I think a neat feature would be the ability to specify a regular expression so you can move, remove, copy, etc. some of the files that you want. For example, here is a list of arbitrary files:

Code:

-rw-rw-rw-   1 meggers staff  543 Mar 10 16:26 tc66.i
-rw-rw-rw-   1 meggers staff  541 Mar 10 16:26 tc67.i
-rw-rw-rw-   1 meggers staff  559 Mar 10 16:26 tc68.i
-rw-rw-rw-   1 meggers staff  545 Mar 10 16:26 tc69.i
-rw-rw-rw-   1 meggers staff  545 Mar 10 16:26 tc70.i
-rw-rw-rw-   1 meggers staff  545 Mar 10 16:26 tc71.i
-rw-rw-rw-   1 meggers staff  533 Mar 10 16:26 tc72.i
-rw-rw-rw-   1 meggers staff  536 Mar 10 16:26 tc73.i
-rw-rw-rw-   1 meggers staff  537 Mar 10 16:26 tc74.i


wouldn't it be cool if I could remove say the files numbered from 66 to 71 (which is exactly what prompted this) by typing:

Code:


rm tc[65-71].i



btw, the man says nothing about this. I am familiar enough with system calls that I would be more than willing to write such a routine if you all think it would be worthwhile.



_________________
Debian Gnu/Linux 3.1 r2 "sarge"
kernel 2.6.8-2-i386
Back to top
View user's profile Send private message Yahoo Messenger
platinummonkey
Advanced Member


Joined: 01 Mar 2024
Posts: 732
Location: Texas

PostPosted: Tue Mar 11, 2024 11:18 pm    Post subject: Reply with quote

i think grep might be up your alley Wink

here are some non regex ways in bash to do some things:

rename files
Code:
for i in *.ma*; do mv "$i" "${i:0:${#i}-9}"; done  # cuts off the last 9 characters of the filename
for i in *.mb*; do mv "$i" "${i/.mb*}".mb; done   #  renames any file names that have for ex. filename.mb.23939 to just filename.mb (helpful for those funky archives or versionings..)


move files
Code:
for i in *; do mv "$i" /path/to/directory; done


copy files
Code:
for i in *; do cp "$i" /path/to/directory; done  #just copies them to somewhere else.
for i in *; do cp "$i" "$i".bak; done #backs them up


These aren't regex's really, just simple loops, but they are fairly easy to remember and type out so Razz You could add them to your .bashrc and have paths/extensions, etc as different inputs Razz I just type them out Razz

simple grep example:
Code:
grep -ie "/regex/" /path/to/directory | cp  /to/directory #copies some files into some directory that match the regex
Smile



_________________
desktop - FreeBSD 7.2
laptop & server - Archlinux i686 kernel26 2.6.32.10-1
- TAMULinux-2.0.2-ALPHA
USB Boot - Archlinux i686 kernel26 2.6.32.10-1 USB boot
Back to top
View user's profile Send private message Visit poster's website AIM Address
eggmatters
Jr. Member


Joined: 17 Oct 2024
Posts: 60

PostPosted: Tue Mar 11, 2024 11:27 pm    Post subject: Reply with quote

Excellent, that's a great idea. Thanks for your reply!



_________________
Debian Gnu/Linux 3.1 r2 "sarge"
kernel 2.6.8-2-i386
Back to top
View user's profile Send private message Yahoo Messenger
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    USA Linux Users Group Forum Index » System Administration and Security All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All content © 2024-2009 - Usa Linux Users Group
This forum is powered by phpBB. © 2024-2009 phpBB Group
Theme created by phpBBStyles.com and modified by Crouse