Shell
Using bash wildcards
Shell Tips
November 4, 2006 | COMMENTS

Common way for rename a mispelled file is to use mv but many dudes will first write the curent name then the new name (ie. mv old_file new_file). Stop ! Use the wildcards ! 😃

Most used wildcard is the asterix "*" (match all characters) but other usefull wildcards exist like the question mark "?" that match for a single character. In some case, as a large directories, that’s avoid you to use a grep on your ls command and give you a more concise listing.

# Question Mark Wildcard "?"

nicolas@grimm:~$ ls

pic1.jpg pic1.txt pic2.jpg pic3.jpg pic4.jpg pic5.jpg pic-big.jpg pic.jpg pic-small.jpg pic.txt pic1.txt pic2.txt pic1.bmp
nicolas@grimm:~$ ls pic?.jpg

pic1.jpg pic2.jpg pic3.jpg pic4.jpg pic5.jpg

Other usefful wildcard will be square brackets "[]" and curly brackets "{}". First let you match a range of values, second let you define a list of values.

# Square Brackets Wildcard "[]"

nicolas@grimm:~$ ls pic[ 1-3].jpg

pic1.jpg pic2.jpg pic3.jpg

# Curly Brackets Wildcard "{}"

nicolas@grimm:~$ ls pic1.{txt,jpg,bmp}

pic1.txt pic1.jpg pic1.bmp

You can use those wildcards simultanously for a better search.

nicolas@grimm:~$ ls pic[1-3].{txt,jpg}

pic1.jpg pic1.txt pic2.jpg pic3.jpg

We have seen how to perform our ls search but you can also use those bash wildcards in any of your bash commands !

nicolas@grimm:~$ mv a_too_{short,long}_file_name_with_lot_of_ch4r4ct3r3.txt

will result in renaming the file a_too_short_file_name_with_lot_of_ch4r4ct3r3.txt to a_too_long_file*_name_with_lot_of_ch4r4ct3r3.txt*

nicolas@grimm:~$ cp pic[1-3].jpg dest

will result in copying pic1.jpg, pic2.jpg and pic3.jpg into the directory dest

Working quickly with some usefull BASH Shortcuts
Shell Tips
October 29, 2006 | COMMENTS

Last day, one of my friends was doing some stuff on its Mac OSX Term without using any shortcuts. I suggest some of them (which are POSIX, GNU or Shell keys combinations) to him and he answer me : Why don’t you post it on shell-tips.com ?!

Those shortcuts are for Bash shell but it’s probably work with other advance shells.

Control Keys combinations (CTRL+KEY)

  1. ctrl+a : move your cursor to the beginning of the line
  2. ctrl+e : move your cursor to the end of the line
  3. ctrl+k : delete any characters from your cursor to the end of the line
  4. ctrl+u : delete any characters from your cursor to the beginning of the line
  5. ctrl+w : delete previous word
  6. ctrl+t : transpose two previous characters
  7. ctrl+y : yank/recover the last deletion
  8. ctrl+d : delete one character at the cursor position
  9. ctrl+h : delete one character before the cursor
  10. ctrl+f : move forward (or use the right arrow ! 😃
  11. ctrl+b : move backward (or use the left arrow ! 😃
  12. ctrl+r : find character sequence in history (completion mode)
  13. ctrl+g : escape from completion mode
  14. ctrl+v : Literal next (LNEXT)

NB: LNEXT interpret the next character as a string. eg : for symbolize a CR+LF* you must do the key combination ctrl+v+return, that will print ^M.*

Escape Keys combinations (ESC+KEY)

  1. esc+d : delete from the cursor position to the end of the word
  2. esc+f : move forward a word
  3. esc+b : move backward a word
  4. esc+t : transpose two adjacent words

Other common keys

  1. Use up/down arrows to move thru the bash command history
  2. Use left/right arrows to move on the current line
  3. Use tabulation key (TAB) for auto-complete a command name or a file name
  4. Use exclamation key + command name for repeat last similar command (ex. : !vi will recall the last vi command)
CLI workaround for Windows XP : Using GREP (QGREP)
Shell Tips
October 21, 2006 | COMMENTS

In our list of "CLI workaround for Windows XP", a missing command is GREP. Now, you can have a GREP-Like command with the DOS command QGREP. Get the [Windows Server 2003 Resource Kit tools][1] on microsoft.com.

# display help

QGREP /?

usage: qgrep [-?BELOXlnzvxy][-e string][-f file][-i file][strings][files] -? - print this message
-B - match pattern if at beginning of line
-E - match pattern if at end of line
-L - treat search strings literally (fgrep)
-O - print seek offset before each matching line
-X - treat search strings as regular expressions (grep)
-l - print only file name if file contains match
-n - print line number before each matching line
-z - print matching lines in MSC error message format
-v - print only lines not containing a match
-x - print lines that match exactly (-BE)
-y - treat upper and lower case as equivalent
-e - treat next argument literally as a search string
-f - read search strings from file named by next argument (- = stdin)
-i - read file list from file named by next argument (- = stdin)
White space separates search strings unless the argument is prefixed with -e, e.g., ‘qgrep "all out" x.y’ means find either "all" or "out" in x.y, while ‘qgrep -e "all out" x.y’ means find "all out".

# Example for UNIX-Like regexp search

QGREP -X "[a-Z]" MYFILE.TXT

System Requirements

Supported Operating Systems: Windows Server 2003; Windows XP

  • 30 MB of free disk space
  • Windows XP
  • Windows XP SP1
  • Windows Server 2003 family
Customize your DOS/CMD Prompt
Shell Tips
October 21, 2006 | COMMENTS

If you want to tune your PROMPT you just need to use... the PROMPT command ! By default the prompt is set to the current path, but you can customize it a little.

# Display prompt help

PROMPT /?

# Customize your prompt

PROMPT $T $D$_$P $F

# Will give this result in my cmd window

17:11:55,31 sam. 21/10/2006

D:\Documents and Settings\Nicolas>

CLI workaround for Windows XP : Using LIST (LESS-Like)
Shell Tips
October 21, 2006 | COMMENTS

In our list of "CLI workaround for Windows XP", a missing command is LESS. Now, you can have a LESS-Like command with the DOS command LIST. Get the [Windows Server 2003 Resource Kit tools][1] on microsoft.com. While not 100% functionally-equivalent to most versions of the UNIX LESS command, list does support text searching and bi-directional scrolling. This command is useful when examining log files (such as web server logs), configuration files, and software "read me" files when you don’t want to open a Notepad window.

Usage

filename
REQUIRED - The filename you wish to view

-s:STRING
Performs a case insensitive search and highlights the first line matching your search text

-g:LINE#
The list display will jump to the specified line number, displaying it at the bottom of the screen. Note that list starts its line numbering from line 0.

While running the list command, here are a few of the functions you may perform:

Cursor keys
Use the cursor keys to scroll through the file. If the file is too wide to view onscreen at once, the left and right cursor keys will be active.

Page Up, Page Down Keys Scrolls through the text file up or down one page at a time

Home, End Keys Jumps to the beginning or end of the file

W
Toggles text word-wrap

Q or the ESC key Quit the list display

/ Perform a case-sensitive search of the string you specify

\ Perform a case-insensitive search of the string you specify

n or F3
Searches for the next match to the search string

N Searches for the previous match to the search string

G Jump to the specified line number

F Open a new file without going back to the DOS prompt

? Displays a full list of commands

Note that by entering more than one filename after the list command you can view more than one file. Although only one file is displayed at a time, the following commands switch between files specified on the command line:

CTRL PAGE-DOWN Displays the next file specified on the command line, or wraps to the first file.

CTRL PAGE-UP Displays the previous file specified on the command line, or wraps to the last file.

System Requirements

Supported Operating Systems: Windows Server 2003; Windows XP

  • 30 MB of free disk space
  • Windows XP
  • Windows XP SP1
  • Windows Server 2003 family