[1+1=2]

OneAndOneIs2

« A message I sent Yahoo re. their toolbarTwitter on trial »

Tue, Aug 24, 2010

[Icon][Icon]Useful command-line things

• Post categories: Omni, FOSS, Technology, My Life, Helpful

Pretty much everything I do is command-line based these days. So I thought I'd post a few of the things I've found useful lately.

  • {}s when moving or copying files.
    You know when you want to copy a file in a directory that isn't your current one? e.g. you're in your home/ and you want to do something to Firefox's userChrome settings. This means copying userChrome-example.css to userChrome.css but the path is a PITA to type twice.
    So instead, you can use curly brackets to make one statement do the work of two:
    cp .mozilla/firefox/profile/chrome/{userChrome-example.css,userChrome.css}
    - much better!
  • Useful when you want to edit a file in a non-local directory, as an alias in your .bashrc:
    function vai {
    found=`find . -path *$1`
    vi $found
    }
    So, having created your userChrome file, instead of
    vi .mozilla/firefox/xvuz3pf5.default/chrome/userChrome.css
    you can use the above and simply vai userChrome.css
  • If you want to edit a file that isn't just in another directory, but on another server, but using your own local text editor with its nice config files, you don't have to copy it over, edit it, copy it back. Instead, do it all at once:
    vim scp://user@host.name/path/to/file
  • Lastly, regex find&replace - often handy, often tricky. So here's an example of one, you can extrapolate from there:
    You want to take a list of filenames
    DSC001.JPEG
    DSC002.JPEG
    DSC003.JPEG
    DSC004.JPEG
    and change them into
    photo_001.jpg
    photo_002.jpg
    photo_003.jpg
    photo_004.jpg
    The command would be:
    :%s/^.\{3\}\([0-9]\{3\}\).*/photo_\1.jpg/g
    which, broken down, means:
    :       - ex command
    %       - every line
    s       - find & replace
    /       - beginning of first term
    ^       - Start at the first character of the line
    .       - any character
    \{3\}   - Three repetitions of the previous character
              (so in this case, any three characters at the start of the line)
    \(      - Opening parentheses of a part of the search that we want to be able 
              to use in the replace text
    [0-9]   - match any number, from 0-9
    \{3\}   - Three repetitions of previous character - here, any three numbers
    \)      - closing parentheses
    .*      - any number of any characters
    /       - End of search term, beginning of replace
    photo_  - replacement text
    \1      - First parenthesised text in the search term, i.e. first \(text\)
    .jpg    - replacement text
    /       - End of replacement term

Hopefully, something or other in there might be useful to the geeks amongst you who use the CLI (or a decent text editor ;o). For anyone else, this post was a baffling waste of time.

Such is life!

3 comments

Dion Moult
Comment from: Dion Moult [Visitor] · http://thinkmoult.com/
Useful post! As for the regex one in order to execute it directly in the command line you can do:
ls | while read file; do new_name=(replace using regex or whatever); mv "$file" "$new_name"; done
25/08/10 @ 03:02
pdh
Comment from: pdh [Visitor] · http://www.simply-click.org/
Snuck a vim regex in to your "command-line things" didn't you :p.

perl -pi -e 's/^.{3}([0-9]{3}).*/photo_$1.jpg/g' filename.txt

Is how I would do that from the command line.
25/08/10 @ 20:25
oneandoneis2
Comment from: oneandoneis2 [Member] · http://geekblog.oneandoneis2.org/
@pdh: Well? I run vim, not gvim :P
26/08/10 @ 12:46

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
This is a captcha-picture. It is used to prevent mass-access by robots.
Please enter the characters from the image above. (case insensitive)
 

[Links][icon] My links

[Icon][Icon] Hmm.. new look for twitter? I hope it gets less "Ick! Change! Put it back!" nonsense than Facebook..
08/02/12

[Icon][Icon] Facebook Syndication Error
11/02/12

[Icon][Icon] I last listened to:
Johann Pachelbel - Canon in D major

[Icon][Icon] Most recent photo:
Submersible houseboat

[Icon][Icon]About Me

[Icon][Icon]About this blog

[Icon][Icon]My LQ profile

[Icon][Icon]My /. profile

[Icon][Icon]My Wishlist

[Icon]MyCommerce

[FSF Associate Member]


February 2012
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29        

Search

User tools

XML Feeds

eXTReMe Tracker

Valid XHTML 1.0 Transitional

Valid CSS!

[Valid RSS feed]

powered by b2evolution