Wednesday, September 27, 2006
Monday, September 25, 2006
Command find
find -name 'mypage.htm'
In the above command the system would search for any file named mypage.htm in the current directory and any subdirectory.
find / -name 'mypage.htm'
In the above example the system would search for any file named mypage.htm on the root and all subdirectories from the root.
find -name 'file*'
In the above example the system would search for any file beginning with file in the current directory and any subdirectory.
find -name '*' -size +1000k
In the above example the system would search for any file that is larger then 1000k.
Here then is an example of how to search in the current directory and all subdirectories for files ending with the the extensions ".class" and ".sh" using the Unix
find
command:find . -type f \( -name "*.class" -o -name "*.sh" \)That should work on all types of Unix systems, including vanilla Unix, Linux, BSD, freeBSD, AIX, Solaris, and Cygwin.
While I'm in the neighborhood, here is an example of how to search the current directory for files that end in any of three different files extensions:
find . -type f \( -name "*cache" -o -name "*xml" -o -name "*html" \)(FWIW, I did that one on a Mac OS/X machine.)
In these examples I always include the "-type f" option, which tells
find
to only display files, and specifically not directories. That's not always necessary, but when someone tells me they want to find files, I always include it.
The grep Command
The grep command is useful for finding specific character strings in a file. For example, if you want to find every reference made to "coffee" in the file sneakers.txt, you would type:
grep coffee sneakers.txt |
You would see every line in that file where the word "coffee" is found.
Grep is one such tool. Let's say for example we wanted to find wherepid_t is defined. We could issue:
os161-1.10.original/kern]$ grep -irnH pid_t *
find tools
find commands
################
##Perl Script to find phrases in any file inside a directory or subdirectory
#!/bin/bash# findstring.sh:
# Find a particular string in binaries in a specified directory.
directory=/home/jacarde1/
fstring="characters" # Here write the word you want to find.
for file in $( find $directory -type f -name '*' | sort )
do
strings -f $file | grep "$fstring" | sed -e "s%$directory%%"
# In the "sed" expression,
#+ it is necessary to substitute for the normal "/" delimiter
#+ because "/" happens to be one of the characters filtered out.
# Failure to do so gives an error message (try it).
done
exit 0
#################################################
grep smug files | {search files for lines with 'smug'} |
grep '^smug' files | {'smug' at the start of a line} |
grep 'smug$' files | {'smug' at the end of a line} |
grep '^smug$' files | {lines containing only 'smug'} |
grep '\^s' files | {lines starting with '^s', "\" escapes the ^} |
grep '[Ss]mug' files | {search for 'Smug' or 'smug'} |
grep 'B[oO][bB]' files | {search for BOB, Bob, BOb or BoB } |
grep '^$' files | {search for blank lines} |
grep '[0-9][0-9]' file | {search for pairs of numeric digits} |
grep '^From: ' /usr/mail/$USER | {list your mail} |
grep '[a-zA-Z]' | {any line with at least one letter} |
grep '[^a-zA-Z0-9] | {anything not a letter or number} |
grep '[0-9]\{3\}-[0-9]\{4\}' | {999-9999, like phone numbers} |
grep '^.$' | {lines with exactly one character} |
grep '"smug"' | {'smug' within double quotes} |
grep '"*smug"*' | {'smug', with or without quotes} |
grep '^\.' | {any line that starts with a Period "."} |
grep '^\.[a-z][a-z]' | {line start with "." and 2 lc letters} |
% grep BOB tmpfile | {search 'tmpfile' for 'BOB' anywhere in a line} |
% grep -i -w blkptr * | {search files in CWD for word blkptr, any case} |
% grep run[- ]time *.txt | {find 'run time' or 'run-time' in all txt files} |
% who | grep root | {pipe who to grep, look for root} |
egrep example
$ egrep ' paper | people ' fortunes
Line printer paper is strongest at preforations.
Man Year: 730 people working feverishly until noon.
Nudists are people who wear one-button suits.
$
Examples
To use an extended pattern that contains some of the pattern-matching characters +, ?, |, (, and ), enter:
egrep "\( *([[:lower:][:upper:]]*|[:digit:]*\)" my.txt
This displays lines that contain letters in parentheses or digits in parentheses, but not parenthesized letter-digit combinations. It matches (y) and (783902), but not (alpha19c).
Note: When using the egrep command, \ ( (backslash followed by open parenthesis) or \ ( (backslash followed by close parenthesis) match parentheses in the text, but ( (open parenthesis) and ) (closed parenthesis) are special characters that group parts of the pattern. The reverse is true when using the grep command.
Let say you want to search for string fprintf, vprintf and sprintf using grep, usually what you do is
egrep "fprintf|vprintf|sprintf" *.c
You may be ask why don’t just uses the word “printf”? If uses the word printf, it will return all of them but also include printf itself. But in this case i don’t want to grep other printf besides f,v,s printf. Thats the square brackets comes in to lessen your trouble.
egrep "[sfv]printf" *.c
It simply return the result with any character specified in [ ] with word printf concatenated.
The square brackets can be used with other RE symbols, here is another example, let say I want to gets all lists with words start with a character “a to f”, I can do this
egrep "^[a-f]" com-book.txt
It is case sensitive, I want all a to f including the upper case A to F.
egrep "^[a-fA-F]" com-book.txt
Thursday, September 21, 2006
Wednesday, September 20, 2006
Monday, September 18, 2006
< The list of Vim commands >
Working with files | |
Vim command | Action |
:e filename | Open a new file. You can use the Tab key for automatic file name completion, just like at the shell command prompt. |
:w filename | Save changes to a file. If you don't specify a file name, Vim saves as the file name you were editing. For saving the file under a different name, specify the file name. |
:q | Quit Vim. If you have unsaved changes, Vim refuses to exit. |
:q! | Exit Vim without saving changes. |
:wq | Write the file and exit. |
:x | Almost the same as :wq, write the file and exit if you've made changes to the file. If you haven't made any changes to the file, Vim exits without writing the file. |
Moving in the file | |
These Vim commands and keys work both in command mode and visual mode. | |
Vim command | Action |
j or Up Arrow | Move the cursor up one line. |
k or Down Arrow | Down one line. |
l or Right Arrow | Right one character. |
h or Left Arrow | Left one character. |
e | To the end of a word. |
E | To the end of a whitespace-delimited word. |
b | To the beginning of a word. |
B | To the beginning of a whitespace-delimited word. |
0 | To the beginning of a line. |
^ | To the first non-whitespace character of a line. |
$ | To the end of a line. |
H | To the first line of the screen. |
M | To the middle line of the screen. |
L | To the the last line of the screen. |
:n | Jump to line number n. For example, to jump to line 42, you'd type :42 |
Inserting and overwriting text | |
Vim command | Action |
i | Insert before cursor. |
I | Insert to the start of the current line. |
a | Append after cursor. |
A | Append to the end of the current line. |
o | Open a new line below and insert. |
O | Open a new line above and insert. |
C | Change the rest of the current line. |
r | Overwrite one character. After overwriting the single character, go back to command mode. |
R | Enter insert mode but replace characters rather than inserting. |
The ESC key | Exit insert/overwrite mode and go back to command mode. |
Deleting text | |
Vim command | Action |
x | Delete characters under the cursor. |
X | Delete characters before the cursor. |
dd or :d | Delete the current line. |
Entering visual mode | |
Vim command | Action |
v | Start highlighting characters. Use the normal movement keys and commands to select text for highlighting. |
V | Start highlighting lines. |
The ESC key | Exit visual mode and return to command mode. |
Editing blocks of text | |
The Vim commands marked with (V) work in visual mode, when you've selected some text. The other commands work in the command mode, when you haven't selected any text. | |
Vim command | Action |
~ | Change the case of characters. This works both in visual and command mode. In visual mode, change the case of highlighted characters. In command mode, change the case of the character uder cursor. |
> (V) | Shift right. |
< (V) | Shift left. |
c (V) | Change the highlighted text. |
y (V) | Yank the highlighted text. In Winblows terms, "copy the selected text to clipboard." |
d (V) | Delete the highlighted text. In Winblows terms, "cut the selected text to clipboard." |
yy or :y or Y | Yank the current line. You don't need to highlight it first. |
dd or :d | Delete the current line. Again, you don't need to highlight it first. |
p | In Winblows terms, "paste" the contents of the "clipboard". In Vim terms, you "put" the text you yanked or deleted. Put characters after the cursor. Put lines below the current line. |
P | Put characters before the cursor. Put lines above the current line. |
Undo and redo | |
Vim command | Action |
u | Undo the last action. |
U | Undo all the latest changes that were made to the current line. |
Ctrl + r | Redo. |
Search | |
Vim command | Action |
/pattern | Search the file for pattern. |
n | Scan for next search match in the same direction. |
N | Scan for next search match but opposite direction. |
Replace | |
Vim command | Action |
:rs/foo/bar/a | Substitute foo with bar. r determines the range and a determines the arguments. |
The range (r) can be | |
nothing | Work on current line only. |
number | Work on the line whose number you give. |
% | The whole file. |
Arguments (a) can be | |
g | Replace all occurrences in the line. Without this, Vim replaces only the first occurrences in each line. |
i | Ignore case for the search pattern. |
I | Don't ignore case. |
c | Confirm each substitution. You can type y to substitute this match, n to skip this match, a to substitute this and all the remaining matches ("Yes to all"), and q to quit substitution. |
Examples | |
:452s/foo/bar/ | Replace the first occurrence of the word foo with bar on line number 452. |
:s/foo/bar/g | Replace every occurrence of the word foo with bar on current line. |
:%s/foo/bar/g | Replace every occurrence of the word foo with bar in the whole file. |
:%s/foo/bar/gi | The same as above, but ignore the case of the pattern you want to substitute. This replaces foo, FOO, Foo, and so on. |
:%s/foo/bar/gc | Confirm every substitution. |
:%s/foo/bar/c | For each line on the file, replace the first occurrence of foo with bar and confirm every substitution. |
Linux help > Tips and cheat sheets > The Vim commands cheat sheet
Sunday, September 17, 2006
Aguila server (updating gentoo)
!!! Your current profile is deprecated and not supported anymore.
!!! Please upgrade to the following profile if possible:
default-linux/x86/2006.1/desktop
To upgrade do the following steps:
# There are several possible profiles that you can use to replace this one.
# default-linux/x86/2006.1
# default-linux/x86/2006.1/desktop
# default-linux/x86/2006.1/server
# default-linux/x86/no-nptl
# default-linux/x86/no-nptl/2.4
# By default, we use the desktop profile.
# emerge -n '>=sys-apps/portage-2.0.51'
# cd /etc
# rm make.profile
# ln -s ../usr/portage/profiles/default-linux/x86/2006.1/desktop make.profile
# This profile is deprecated and will be removed on or after November 1, 2006
====================
Automated
(View available profiles)
# eselect profile list
(Select the number of your desired profile from the list)
# eselect profile set
========
Gentoo Upgrade
http://www.gentoo.org/doc/en/gentoo-upgrading.xml
Updating your System
To keep your system in perfect shape (and not to mention install the latest security updates) you need to update your system regularly. Since Portage only checks the ebuilds in your Portage tree you first have to update your Portage tree. When your Portage tree is updated, you can update your system with emerge --update world. In the next example, we'll also use the --ask switch which will tell Portage to display the list of packages it wants to upgrade and ask you if you want to continue:
Code Listing 13: Updating your system |
# emerge --update --ask world |
Portage will then search for newer version of the applications you have installed. However, it will only verify the versions for the applications you have explicitly installed - not the dependencies. If you want to update every single package on your system, add the --deep argument:
Code Listing 14: Updating your entire system |
# emerge --update --deep world |
Since security updates also happen in packages you have not explicitly installed on your system (but that are pulled in as dependencies of other programs), it is recommended to run this command once in a while.
If you have altered any of your USE flags lately you might want to add --newuse as well. Portage will then verify if the change requires the installation of new packages or recompilation of existing ones:
Code Listing 15: Performing a full update |
# emerge --update --deep --newuse world |