_ - thanks for -
___ ___ _____ _____ ___ ___ _| |___ - visiting -
| _| . | | | . | | . |_ -|
|___|___|_|_|_|_|_|_|__,|_|_|___|___|
a bunch of commands for your bash. .-=-.
________________________________________________-={{{*}}}=-________
- Usage: '-=-'
to go into vi and edit. To quit: :wq q
Version : 1.4
Last update: 28-November-2006
index
Chapter: intro
about this file
using this file
info about commands
usage summaries
Chapter: tools
general
sort
uniq
grep
egrep
tr
sed
awk
cut
cat
Chapter: basic commands
remove
rename
copying
less
ls
find
mv
mkdir
diff
history
touch
calculate
Chapter: ftp
ftp
chmod under ftp
gftp
other ways to ftp
Chapter: bash
general bash tricks
screen
regular expression
expansion
wildcards
Chapter: control structures
for i in
if
case and while
until
select
Chapter: texteditors
vi
pico
Chapter: scripts
general
commandline arguments
functions
running scripts from bin
ascii
imagetools
firefox
trouble booting into graphic mode
installation of rpm's
partitions
gimp
checking file integrity with md5sum
console / konsole and configure your commandline
alias
editing .bashrc
editing ~/.inputrc
changing the default runlevel
running cronjobs
system information
networking
less useful
users
automounting on access to a folder (have not tried this yet)
setting variables
words and dictionary
tar
dd
symlinks and hardlinks
finger
permissions
how to kill things
sleep
date
networking
encription
the rest of loose commands
Chapter: more experienced
general
streams
tee
system scripts
bash
Chapter: xmms
mp3
flac
mpc
ape
cue
Chapter: kde
special keys
konqueror
kate
kde desktop
using your numpad as a mouse
Chapter: suse specific
some general things
open office
dual boot xp and suse10 using ntldr
mail
kmail
rkhunter
ssh
chkrootkit
apache
php
running mod_rewrite
mysql
skype
other good links to explore
< index >
intro - about this file
This nerdstuffs contains a lot of text that helped me get my Suse 10.0
working the way i want it to work. The things that are really specific for Suse are
in a separate section, the rest should work on most distro's (i hope ..).
It contains information gathered from various fora and online guides as well
as things i figured out myself.
This text contains a lot of examples you can use on the commandline.
The format is always: first the command and then the explanation.
It is out here to try and help you get underway and whip your linux into shape ;)
< index >
intro - using this file
Save the text you are reading right now as: ~/bin/commands.txt
Place the following in a text file that you save as ~/bin/commands :
#!/bin/sh
# less the commands-file to edit and read
# usage: commands
less ~/bin/commands.txt
Next to make it executable: chmod +x ~/bin/commands
By typing <commands> anywhere on the commandline you will open this file to read or edit.
If this does not work:
Check out 'running scripts from bin' later in this text to see how to use it from any location.
< index >
intro - info about commands
apropos <program name or topic>
Give me the list of the commands that have something to to do with my topic. Emulates man -k
whereis <program name>
Find a program. It gives output such as this when run on kate: /opt/kde3/bin/kate
ls -l $(whereis mplayer)
Get a more detailed listing from whereis.
which <program name>
Where is a program located.
locate <name>
Locate a piece of software on your system. Use updatedb first to update the database of all stuff on your system.
See "suse specific - some general things" is locate is not on your system.
man <program name or command>
Gives you the manual pages for a program.
< index >
intro - usage summaries
See also rute: http://rute.2038bug.com/node7.html.gz
[ ]
Brackets in man pages of commands mean that the stuff in between is optional.
The brackets are not to be typed.
...
The ellipses ... mean that <file> can be given repeatedly,
and these also are never actually typed.
< >
Mean the you have to replace your own text with <something>.
$ <x>
Means you have to execute command <x> as a regular user (not root). Don't type the $.
# <x>
Means you have to execute command <x> as root. Don't type the #.
< index >
tools - general
sort
Sorts standard input then outputs the sorted result on standard output.
uniq
Given a sorted stream of data from standard input, it removes duplicate lines of data
(i.e., it makes sure that every line is unique).
grep
Examines each line of data it receives from standard input and
outputs every line that contains a specified pattern of characters.
Filter out lines.
fmt
Reads text from standard input, then outputs formatted text on standard output.
pr
Takes text input from standard input and splits the data into pages with page breaks,
headers and footers in preparation for printing.
head
Outputs the first few lines of its input. Useful for getting the header of a file.
tail
Outputs the last few lines of its input.
Useful for things like getting the most recent entries from a log file.
tr
Translates characters.
Can be used to perform tasks such as upper/lowercase conversions or changing line
termination characters from one type to another
(for example, converting DOS text files into Unix style text files).
sed
Stream editor. Can perform more sophisticated text translations than tr.
awk
An entire programming language designed for constructing filters.
Extremely powerful, also for doing math on streams of text.
cut
Select columns or cut based on delimiters.
cat
Operating on files or groups of files. Outputs text-files.
http://www.linuxquestions.org/questions/showthread.php?threadid=327916
Sometimes your locale settings might screw things up. From man grep:
To obtain the traditional interpretation of bracket expressions, you can use the C locale by setting the LC_ALL environment variable to the value C.
export LC_ALL='C'
Setting the locale setting traditional interpretation of bracket expressions.
< index >
tools - sort
sort lines of text files.
sort < new.txt
cat new.txt |sort > <sorted_new.txt>
Has the same effect: use new.txt as input for sorting. Second option directs the output to a textfile.
cut -d',' -f1 your_file | sort -u
Sort a file alphabetically based on the first column before the delimiter comma (,).
sort -u file.txt
Sorts the words in alphabetical order. The -u option stands for unique, and specifies that duplicate lines of text should be stripped.
sort +2 file
Sort the contents of a text-file based on the last the characters of each line.
find . -name *.xcf|xargs ls -l|sort -k 6
Find *.xcf files recursively and sort them based on the date. Sort on column six.
< index >
tools - uniq
Report or omit duplicate lines.
cat <test.txt> |uniq > <test.txt>
Pipe the output of uniq to the inputfile.
uniq <file>
To show only unique lines from <file>.
uniq -d <file>
To show only the non-unique lines once.
So show the lines which are present twice in the file.
sort <file>| uniq
If the lines are not ordered yet, this removes non-consequtive duplicate lines spread out through the file.
< index >
tools - grep
Print lines matching a pattern.
Usually you will run this as a pipe command to filter out the useful stuff from a long list of output.
cat <commands.txt> |grep new
Does the same as:
grep new <commands.txt>
So search through a text-file for the text 'new'
cat <commands.txt> |grep new -C 1
Does the same as:
grep new -C 1 <commands.txt>
grep new -1 <commands.txt>
Same as before but now we also see the lines above and under our query-result displayed. Usefull to see the result in its context.
find . -print | xargs grep <keyword>
Find files recursively that contain keyword.
find -name "comm*" |xargs grep 'example'
Find, in all files starting with <comm>, the text <example>.
grep 'example' *
find |xargs grep 'example'
Both do the same, find in all files the text <example>.
grep 'example' * -n
Find, in all files (in lower dirs and this dir), the text <example> and display the line numbers (option -n).
Output is in the format of: line-number:text on that line. Example: 34:texttofind
grep -n texttofind <textfile.txt>
Find, in <textfile.txt>, the text <texttofind> and display the line numbers.
Output is in the format of line-number:text on that line. Example: 34:texttofind
grep 'example' <./nerdstuffs/*> -c
Show only the number of hits from all files in this directory for the query-text <example>.
find . -type f -exec grep -l -i "root" {} \;
Search for the word: root in the current directory and recurse into lower directories.
grep "begin.*end" testfile
If a line contains 'begin<anythingelse>end' it will be printed. You can use multiple .*
grep -R -l xyz *
does the same as:
find . -name "*" -exec grep -l xyz {} \;
Find all files with xyz in them. Does not display the lines on which they are.
grep -v ^$ <file>
grep . <file>
Delete all blank lines from a textfile.
cat textfile.txt |grep -v '[[a-z]]*'
Display only lines without letters, so show only lines that have only numbers on them.
if [ `ls -l | egrep '^d' | wc -l` -eq 0 ]; then echo "zero"; else echo "more dirs"; fi
Using egrep (extended regexp, grep -E) to grab the dirs from the directory listing. Testing whether line count matches zero.
It evaluates whether there are more than zero directories in the current dir.
find . -name '*.html' -o -name '*.php'|egrep -v 'old|bla' |nl
Find all files with extensions .html and .php recursively. Exclude the dirs with old or bla in them. Count the number of matches with nl.
man find | grep paths
Grep a man page.
grep .3....8 <filename>
Look for a phone-number that has a 3 on the second position and an 8 at the end. Example of the dot as a wildcard for a single character.
grep -w 't[a-c]e'
Matches the word (-w option) tae, tbe and tce.
grep -w 'kr.*n'
Matches krypton and kremlin. . for any character and * for zero or more repeats.
grep -w 'thr[^a-f]*t'
Matches the words throughput and thrust. The ^ after the first bracket means to match any character except the characters listed.
thrift is not matched.
grep myfile\.txt <somefile>
Escaping characters used in regular expressions with a backslash. These need to be escaped: . \ [ ] * + ?
fgrep is an alternative to grep. The difference is that while grep (the more commonly used command) matches regular expressions,
fgrep matches literal strings.
grep -w 'x\{3,5\}'
Will match at least three but not more than five x's, that is xxx, xxxx, or xxxxx.
grep '\<trot\>'
Matches only the word trot.
grep '^foo'
Look for lines that start with foo.
grep GNU < gnu_lines.txt
A different way of piping a file to grep.
Expressions used in regular expressions:
\( \)
can surround several strings, separated by \|.
This notation will match any of these strings. ( grep only.)
Other useful syntax switches for grep:
-R
Recursive, so look also in lower dirs.
-i
Case-insensitive.
-A
Print number of lines after the hit.
-B
Print number of lines before the hit.
--colour
Gives a colour to the hit.
-c
Only show the number of hits from a file.
< index >
tools - egrep
egrep -w '(th|sh).*rt'
Matches any word starting with th or st and ending in rt. Egrep for extended regular expressions.
+ ? \< \> ( ) | are the characters used in extented regular expressions.
egrep 'array\[[0-9]+\]' file
When using the + use egrep because it is extended regular expression. Matches array[25]
Expressions in extended regular expressions:
+
is analogous to \{1,\}. It does the same as * but matches one or more characters instead of zero or more characters.
?
is analogous to \{1\}. It matches zero or one character.
\< \>
can surround a string to match only whole words (grep and egrep).
( )
can surround several strings, separated by |. This notation will match any of these strings (egrep only).
< index >
tools - tr
Translate characters.
cat file| tr 'A-Z' 'a-z'
Translate uppercase characters to their corresponding lowercases.
cat file| tr 'a-z' 'A-Z' > uppercasefile.txt
Direct the output from tr to a file. Convert all characters from the inputfile to uppercase characters.
tr -d '\n' < file
Delete all newlines from a file.
< index >
tools - sed
http://wiki.linuxquestions.org/wiki/Sed
http://sed.sourceforge.net/
Sed (and grep) can read from stdin so you can try your regular expressions by just invoking sed like so:
sed 's/^someregexp/replacement/g'
And start typing away to see if you can match the regexp that you entered in sed.
sed 's/test/replace/g' < file.txt
sed 's/test/replace/g' file.txt <second_file>
Two ways of redirecting a file (or files) to sed.
echo "test"| sed 's/test/bar/g'
Echo a string into sed.
In shell scripts, invoke sed like this:
#!/bin/sh
# usage: script.sh
sed [-n] '
whole script
'
Run a sed script on a textfile:
sed -f sed_script.txt textfile.txt > new_textfile.txt
The sed_script.txt looks like this:
# this is a sed script
# usage: $ sed -f sed_script.txt textfile.txt > new_textfile.txt
# start replacing all things that must be replaced
# replace / with _
s/\//_/g
./script.sh textfile.txt > new_textfile.txt
Execute a sed script as an executable script.
The script.sh looks like this:
#!/usr/bin/sed -f
# save your commands in this script.sh; chmod u+x script.sh
# call it like so: $ ./script.sh textfile.txt > new_textfile.txt
# start replacing all things that must be replaced
# replace / with _
s/\//_/g
In order to make a sed-script executable you need to change the permissions first:
$ chmod u+x script.sh
1,10{
s/foo/bar/g
s/sheep/bar/g
}
Placed in a script, it will substitute only on lines 1 to 10 two commands on the same single address.
You can also use regexp in the address: 1,/bar/ from line 1 until the line that contains bar.
sed -e '1,100d' -i file.txt
delete the first hundred lines of a file and save it to the same file (-i).
sed -i 's/texttofind/texttoputin/g' textfile.txt
Have sed work on a file directly by using the -i option. Replace texttofind with texttoputin.
sed -i.bak 's/Daniel/Danny/g' <textfile>
Save a backup.bak of the file before replacing things.
ls | sed -n 's/^[A-Z]/&/p'
List all files in the current dir that start with an uppercase. Pipes the output of ls through sed.
replace _bind(server, host); with _bind(server, HostToIP(host)); s/\(_bind([^,]*, \)\([^)]*\)/\1HostToIP(\2)/g
sed 's/\/$//g'
Replace, in lines ending with a slash, the slash with nothing.
sed 's/foo$/bar/g'
Replace, in lines ending with foo, foo with bar.
sed ':a;N;$!ba;s/\n//g' file
Remove all newlines (except for the last newline) from a file. Emulates tr -d '\n' < file
sed 's/users/someothers/g' textfile.txt
In a textfile replace users with someothers in a textfile called textfile.txt.
sed '$aline of text' textfile
sed '$s/$/\nline of text/' textfile.txt
Add a line of text at the end of a file.
a=`wc -l textfile.txt | awk '{print $1}'`
sed $a'a line of text' textfile.txt
A difficult way of adding text (line of text) to the end of a file.
sed '$r othertextfile.txt' textfile.txt
Adding text from another textfile to a textfile.
sed -n '$=' textfile.txt
Have sed count the number of lines from a file, emulates wc -l textfile.txt | awk '{print $1}'
sed -n -e '=;p' textfile.txt
Execute two consecutive commands by separating them with a semicolon. = insert line number. p for print line (on the next line).
sed 's/^/ /g' textfile.txt
Insert spaces to the beginning of each line.
sed -n '/foo/,/bar/p' textfile.txt
Display only lines from foo to bar.
sed -n 4,6p textfile.txt
Show only lines from 4 to 6 from a textfile.
sed 4,6d textfile.txt
Show all lines except lines 4 till 6 from a textfile.
sed G textfile.txt
Doublespace a file, print two enters instead of one.
sed 's/\(.\)\1\(.\)\2\(.\)\3/bar/' textfile.txt
Find patterns like bookkeeper with a tripple repeat.
sed -e '1!G;h;$!d' textfile.txt
Print the lines of a file in reverse order. Emulates tac.
sed -e '1,10s/foo/bar/g' myfile2.txt
On lines 1 to 10 (inclusive) substitute foo with bar.
sed -e '/^$/,/^END/s/foo/bar/g' textfile.txt
In the block of text starting with a blank line (^$) until the characters END, substitute foo with bar.
sed -e 's:/usr/local:/usr:g' mylist.txt
Use the colon as a separator; replace /usr/local with /usr
sed -e 's/\//bar/g' textfile.txt
Escaping the separator with a backslash to replace a slash (/) with bar.
sed -e 's/<[^>]*>//g' textfile.txt
Remove all html tags from a text. Tags that start with < and continue with a lot of single non > ([^>]*) and end in >
sed -n '/<[^>]*>/p' textfile.txt
Print out only lines that contain opening or closing html tags.
sed -e 's/^/bar :/'
sed -e 's/.*/bar : &/' textfile.txt
Replace the beginning of a line with bar. ^ means beginning of a line in regexp. .* stands for a line of everything. & previous match is inserted.
In textfile.txt which has three numbers separated by spaces: 1 2 3
sed -e 's/\(.*\) \(.*\) \(.*\)/first match \1 second \2 third \3/' textfile.txt
Print out the three matches. \(.*\) matches any number of consecutive single characters. \(.*\) \(.*\) finds the spaces in between 'words' or characters.
Print out the logical regions with \x. \1 for the first match.
sed -n 's/.*foo\(.*\)bar.*/\1/p' textfile.txt
Only print out things between the last foo and bar. Cut between.
input file: Synchronization complete (1 item transferred, 31 skipped, 0 failures)
sed -n 's/.*transferred,\ \(.*\)skipped.*/\1/p' file
sed -n 's/\(.*\)\ \(.*\)\ skipped\(.*\)/\2/p' file
Two way of cutting in between (transferred,) and ( skipped). Cut between.
sed '/pattern/d' textfile
Delete lines matching pattern.
sed 's/$/.jpg&/g'
sed 's/\(.*\)$/\1.jpg/g'
Insert '.jpg' before the end of every line.
to only insert text in non-empty lines at the end of the line:
sed 's/[^^]$/.jpg&/g'
Only insert text '.jpg' on non-empty lines, at the end of the line. The circumflex (^) directly behind the [ negates.
cat test.txt | sed -e "s/foo/$SOMEVARIABLE/g"
sed 's/'`echo ${SOMEVARIABLE}`'//g' test.txt
Replace foo with the contents of a variable. (use variable).
sed '/foo/!s/'"${test// /\\ }"'/bar/g' file
Replace a variable (containing spaces), with bar, except on lines that contain foo.
cat test.txt | sed -n $SOMELINEVARIABLE',4p'
Use a variable to display lines from SOMELINEVARIABLE to line 4. (use variable).
sed 's/[ ]*$//' filename
Deletes all trailing spaces from each line.
sed -n '/^foo,/p' aa.txt
Match the pattern foo in the space before the first comma (,).
sed -n '/^foo.*,/p' aa.txt
Same as before, match a pattern foo before the first comma (,) but also allow something behind the pattern.
sed -n '/^[^,]*foo.*,/p' aa.txt
If there is something before and after the pattern allowed. The pattern is anything foo anything followed by comma.
sed -n 's/\ *\([A-Z][^:]*:\).*/\1/p' file
Find a pattern like: (spaces)Capitalized_word:anything and output only Capitalized_word:
sed -e '/./{H;$!d;}' -e 'x;/192.168.1.1\|foo/d;' file
Delete paragraphs that contain 192.168.1.1 OR foo.
sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;'
Print paragraph if it contains AAA (source: sed one-liners.)
sed 's/\x27/bar/g'
sed "s/'/bar/g" file
Replacing apostrophes (') with bar. \x only works in GNU sed. Standard sed don't support it.
sed -n '/,/p' <file>
Search a file for occurance of a comma (,)
sed 's/\([^\ ]\{0,\}\)\ \(.*\)/\2\1/g' <file>
sed -r 's/([^ ]{0,}) (.*)/\2\1/g' <file>
Example for the use of the -r option in sed which makes sed use sed extended regular expressions, so you can leave out the \
echo "4381, -1222333, and 70000: - 44555666 1234567890 words"|\
sed ':a;s/\B[0-9]\{3\}\>/,&/;ta'
The output: 4,381, -1,222,333, and 70,000: - 44,555,666 1,234,567,890 words
Example of how to split numbers into three numbers, split by comma's by use of branching to function a.
sed -r "s/\<(foo|bar)[a-z]+/\U&/g" <file>
sed 's/\<\(foo\|bar\)[a-z]\{0,\}/\U&/g' <file>
Two ways of uppercasing all words starting with foo or bar with the \U option.
echo "foobar foo"|sed 's,\<foo\>,replaced,g'
Example for the use of word boundery markers \< \> It replaces foo only when it is a single word.
# sed script to delete a block if /regex/ matches inside it
# written by Russell Davies
# can also be modified to change, substitute ...
:t
/start/,/end/ { # For each line between these block markers..
/end/!{ # If we are not at the /end/ marker
$!{ # nor the last line of the file,
N; # add the Next line to the pattern space
bt
} # and branch (loop back) to the :t label.
} # This line matches the /end/ marker.
/regex/d; # If /regex/ matches, delete the block.
} # Otherwise, the block will be printed.
#---end of script---
sed '/regexp/,+1d' <file>
Delete the line containing regexp, and the next line.
sed -e :a -e '$!N;s/\n<<=/ /;ta' -e 'P;D' <file>
Join lines containing <<= to the previous line and substitute <<= with a space.
sed ':a;$!N;s/\n//;ta;' <file>
Join all lines together.
sed ':a;$!N;s/\n/foo/;ta;' <file>
Join all lines together and replace the endline with foo
sed '/RE1/,/RE2/{;/RE1/b;/RE2/b;s/^/>>/;}' <file>
Substitute beginning of line with >> on lines between RE1 and RE2 but not ON lines containing RE1 and RE2.
sed '/RE1/,/RE2/{;/RE1/b;/RE2/b;s/foo/bar/;}' <file>
Substitute foo with bar on lines between RE1 and RE2 but not ON lines containing RE1 and RE2.
sed -n '/\[HEADLINE\]/,/\[Words\]/{;/\[HEADLINE\]/b;/\[Words\]/b;s/.*/&/p;}' infile.txt
Substitute, anything between [HEADLINE] and [Words] with anything between [HEADLINE] and [Words], on lines between [HEADLINE] and [Words] but not ON lines containing [HEADLINE] and [Words]. So basicly, print anything between [HEADLINE] and [Words]. Works like html tagging, stops when end-tag is found.
sed "s/$TERM\$/bar/g" <file>
sed "s/$TERM"'$/bar/g' <file>
Examples of the use of a variable $TERM and an endline for sed (\$). If $TERM echos xterm, substitute on lines that end in xterm,
xterm with bar.
sed "s/\\\\'/''/g" <somefile>
Substitutes \' into ''
1: Because we're replacing single quotes, we use double quotes in the shell quoting.
2: Because we're using shell double quotes, getting a slosh \ into the sed string requires writing two sloshes.
3: Because slosh is likewise special to sed, specifying a literal slosh in
the sed string requires writing two sloshes to sed. Being in double quotes,
they must also be doubles, thus the four sloshes.
4. In a script it would be:
#!/bin/sed -f
s/\\'/''/g
sed "s/don't/bar/g" <file>
sed 's/don'\''t/bar/g' <file>
Replace don't with bar. Single quote.
for f in *.html; do sed -i '/^ *$/d' $f; done
In all files called *.html, delete all blank lines and save into the file itself (-i option).
sed '/^[ \t]*$/d' <file>
Remove all lines containing only spaces or tabs.
The basic structure for sed is usually: 's///' file
Or sed 'search/match/output/' (not literally!).
sed 's/\(.*\)-[^-]*/\1/g'
Cut before using the delimiter minus (-).
sed 's/.*-//g'
Cut after delimiter minus (-). Leaves as output only the part before the delimiter.
\(.*\) stands for: match any character.
\1 for first match found.
\2 second match found.
> is the character we are looking for.
substitute/anything until>anything/output only the first match found/
would be: (our textfile is >info1>info2>info3)
sed 's/\(.*\)>.*/\1/' textfile
output is: >info1>info2
Only print out characters between the beginning of the line and the last >. \1 is the stuff found. Cut after delimiter.
to cut before, and print only the second match found:
sed 's/.*>\(.*\)/\1/' textfile
output is: info3
Only print out characters between delimiter (>) and the end of the line ($). \1 is the stuff found. Cut before delimiter.
sed -n 's#.*(\(.\{1,\}\)).*#\1#p' text
Return only characters between parenthesis. (match)nomatch. Cut between.
sed '/./,/^$/!d' <file>
Delete multiple consecutive blank lines and leave one. Allows 0 blanks at top, 1 at EOF. Emulates cat -s <file>
sed '/^$/N;/\n$/D' <file>
Delete multiple consecutive blank lines and leave one. Allows 1 blank at top, 0 at EOF. Emulates cat -s <file>
expr 0 `ls -l | grep '^-' | sed 's/^\([^ ]*[ ]*\)\{4,4\}\([0-9]*\).*$/ + \2/'`
Add the number of bytes per file to the next. Expr for equating 0 + <bytes for file 1> + etc. The bytes are in the fifth column of ls -l
All regular files start with a - so we grep for them first. (source: rute).
sed '/foo$/rfile2' <file>
Append the text from file2 when 'foo' is matched on a line in file. Read from file2.
sed '1,2{=;}' <file>
Number lines 1 to 2.
sed -n '/foo/{:a;N;/bar/!ba;p;}' <file>
If foo is followed by bar later on the text, print only the part between the two regexp. Otherwise don't print anything.
sed -n '/foo/,+1p' file
If the regexp 'foo' is matched also print the next line.
sed -n '1~2p' file
sed -n '2~2p' file
Print only respectively uneven or even lines from a file.
sed -n '1~3p;2~3p' file
Print only the first two lines of a block of three lines. Line 1 and 2 module 3.
sed -n '1~3p' file; sed -n '2~3p' file
First print all first lines of blocks of three (line 1 module 3); then print the second lines of block of three.
sed -n '0,/foo/p' <file>
Match regexp 'foo' from the first line until regexp 'foo' is matched. So print from line 1 to line containing regexp. Delimite regexp.
sed 's/foo/bar/ig' <file>
The i option causes sed to perform a case-insensitive search for 'foo' and substitute it with bar.
sed -n '/foo/{p;q;}' <file>
Print only the first match of 'foo'.
sed '0,/foo/{//d;}' <file>
Delete only the first match of 'foo'.
sed '0,/foo/s//bar/' <file>
Substitute only the first matched 'foo' into 'bar'.
sed 's/\([^,]\)"\([^,]\)/\1 \2/g;s/\([^,]\)"\([^,]\)/\1 \2/g' file
In a comma separated file (CSV) and optionally " delimited file like this:
"WAIVED/CES "GOOD","WAIVED BASED ON CES & "A" PB ,"WAIVED BASED ON PB & "A" PAID"
Only keep " at the beginning or end of the line, and the ones which neighbour the ,
It goes through each line twice since the first run does not find all "s we want to delete.
Output:
"WAIVED/CES GOOD","WAIVED BASED ON CES & A PB ,"WAIVED BASED ON PB & A PAID"
switches in sed:
-n no output. Usually used together with p.
-p print output.
d
delete.
G and h
get stuff into holdspace.
s
substitute.
g
global, when substituting replace all occurances of REGEXP
Commands insert, append and change:
i\some_insert
add the insert before each line. Specify multiple line insert by ending them with a backslash and ending the last line without backslash.
i\
some_insert1\
some_insert2\
third and last insert
a\append_some
Append a insert after lines with something. Works the same as insert.
c\change_into_this
change the line into change_into_this.
sed '7a\text' file
Simple example of appending 'text' after the 7th line.
sed '/foo/a\bar' file
sed '/foo/s/$/\nbar/' file
Two ways of appending 'text' on the next line whenever 'foo' is found.
sed -e '$a\thisisnowthelastline' file
Append text after the last line is found.
sed '$d' file
Delete the last line from a file.
sed -i 's/#[0-9a-zA-Z]\{6\};/<font\ color=\"&\">&<\/font>/g' infile > outfile.html
Display all html color codes as those colors in a html-file. #ed2342; becomes <font color="#ed2342;">#ed2342;</font>
Useful for testing the html colors in css files and such.
Keep working on the original infile and keep the outfile next to it as a reference.
# this script replaces foo in the input file with the output of a command (ls in this example)
# usage: save this text as search_replace_outputcommand.sed
# sed -f search_replace_outputcommand.sed inputfile
s/foo/\n/
T
P
e ls
D
# end of script
5,9{
c\
append
}
Specifying the address (the lines) on which the change (can also be insert or append) will be executed.
example in a sed script file:
/#include <termios\.h>/{
i\
#ifdef SYSV
a\
#else\
#include <sgtty.h>\
#endif
}
That would search for lines `#include <termios.h>' and then would write:
#ifdef SYSV
#include <termios.h>
#else
#include <sgtty.h>
#endif
The same on multiple lines from the commandline:
Sed -e '/#include <termios\.h>/{' \
-e 'i\' \
-e '#ifdef SYSV' \
-e 'a\' \
-e '#else\' \
-e '#include <sgtty.h>\' \
-e '#endif' \
-e '}'
Using semicolons (;) as separators we can do it in one line from the commandline:
sed -e '/#include <termios\.h>/{;i\' -e '#ifdef SYSV' -e 'a\' -e '#else\' \
-e '#include <sgtty.h>\' -e '#endif' -e '}'
$d Delete the last line of a file.
5!s/foo/bar/
Except on line 5, replace foo with bar. ! is used to negate.
/thebest/!s/foo/bar/ Replace foo with bar, unless thebest is found.
Adressing:
\{i,j\} matches between <i> and <j>, inclusive, sequences.
\{i,\} matches more thanor equal to <i> sequences.
\{,j\} matches at most (or equal) <j> sequences.
17,/foo/d delete all lines from line 17 to foo.
/^$/d
matches all empty lines. Same as /./!d
In regular expressions:
[list]
find a single character from the list. Usually some character class like: [a-z]
[^list] negate, so find any character that is not in the list.
To include ] in the list, make it the first char.
To include `-' in the list, make it the first or last.
RE1\|RE2
matches RE1 or RE2.
^main.*(.*)
find a line with main at the beginning. Some or one characters followed by a (, some chars and a ).
^#
matches # at a beginning of line, comments.
sed -e '/^#/d' textfile.txt
deletes all lines containing comments, starting with #
A.\{9\}$
match the last tenth A on each line.
^.\{,15\}A
match the last "A" on the first 16 chars of the line.
[^\t\|^ ]
Not a tab and not a space.
< index >
tools - awk
For some advanced examples see: /usr/share/awk
gawk -F '' '{ print NF}' <inputfile>
Count the number of characters on each line.
awk '{ print $2, $1 }' textfile.txt
Print the two first columns from a textfile in reverse order.
awk 'length > 72' textfile.txt
Print the lines that contain more than 72 characters from a textfile.
yes | head -28 | awk '{ print "hi" }'
Print hi 28 times.
df |sed '1d'|awk '{if ($5 > 90) print $5,$6}'|sort -r
df |sed '1d'|awk '$5 > 90 {print $5,$6}'|sort -r
Print the percentage of disk space used (if it contains more then 90 percent) and on which partition, sorted.
echo $((`wc -l textfile.txt|awk '{print $1}'`+2))
Calculate: add 2 to the number of lines from line-count of file textfile.txt done by wc. Grab the number from the output with awk.
sed -e 's/<hr \/>/&\n/g' store.html |echo $((`wc -l|awk '{print $1}'`+2))
Do some replacing with sed before adding something to the number of lines done by a wordcount.
awk '/^foo/ {print;}' <file>
Print lines when the line starts with pattern foo.
awk -F',' '{if ($1=="foo") print;}' aa.txt
Print line when the first column contains only foo. Columns are separated by comma's (,).
awk 'BEGIN{FIELDWIDTHS = "10 25 9"}; {print $3 $2 $1}' <file>
Print three field of different fixed length in reverse order.
awk '(NR%2)==1' file.in
Print all uneven lines from a file.
awk '!(NR%2)==1' file.in
Print all even lines from a file.
du -s ./* |sort -nr|awk '$1 > 500 {print $1,$2}'
Print all files > 500k in a listed sorted format.
< index >
tools - cut
Let's say our textfile.txt looks like this:
>info1>info2>info3
cut -d ">" -f 1-3 textfile.txt
output is: >info1>info2
Gives you everything up to a delimiter. Cut after delimiter.
rev infile | cut -d'>' -f1 | rev
output is: info3
Gives you everything after the delimiter. Cut before delimiter.
echo "abc/def/ghi" | cut -d'/' -f2
Returns def; specify the delimiter with -d and select the second field.
< index >
tools - cat
cat <file>
Print the contents of a textfile; concatenate files and print on the standard output.
tac <file>
Print the contents of a file while the lines in reverse order.
cat <file1> >> <oldfile>
Append file1 to an oldfile.
cat * >> bigfile
Append all files in current dir to one file.
cat -n textfile.txt
Insert the line number (count lines) at the beginning of each line in a textfile.
cat > newtext
Type away after this command. Exit and save with <Ctrl>d.
By default this will overwrite the allready existing file. To have your bash not overwrite files when they are already present use:
set -o noclobber
cat <filename> [<some other filename>]
Cat can print out multiple files in one go.
cat textfile > ./*.default/textfile
Save the contents of a file to a file in a directory ending in .default
Works only if there is only one match for the directory!.
cat ./*.default/textfile
List the contents of a file called textfile from all directories ending in .default
cat -s <file>
Delete multiple consequtive blank lines and leave one and print the contents. Emulates sed '/./,/^$/!d' <file>
j=1;for i in `cat file1`;do echo -e "$i\t`sed -n $j'p' file2`";j=$((j+1));done
Print cell one of line x of file1 followed by a tab and then cell one of line x of file2.
cat -n file
cat file |nl
Two ways of including the line-number on the start of each line, in the output of a file.
tac file|tail +3|tac
Delete the last two lines of file (or output).
ls |tail +2|tac|tail +3|tac
Delete the first line, and the two last lines of the output of ls.
< index >
basic commands - remove
rm artwork/figure[2-7]
Specify a range of files to delete by specifying the range between brackets []
rm -i *.fm
Remove files interactively.
rm -Rf *_??.png
Remove all (pictures, photos) named something_23.png and recurse down in lower dirs. So leave photo 1-9.
The question mark (?) works different from regular expressions here.
rm -- --backup
rm ./--backup
Both delete the file --backup . -- specifies that there are no more options with -- following so rm treats it like the filename.
rmdir <some dir>
Specifically remove a dir when it is empty.
rm -R <somedir>
Remove a directory and it's contents.
< index >
basic commands - rename
rename ' ' '_' *
Does the same as:
for i in *; do mv "$i" `echo $i | tr ' ' '_'`; done
Replace all spaces with underscores when moving files.
rename picture picture0 picture?.jpg
rename picture picture0 picture??.jpg
These two consequtive commands will rename any number between 1 and 999 which were named like:
picture1.jpg picture10.jpg into picture001.jpg picture010.jpg
ls *.txt | while read f;do echo mv $f ${f%.txt}.TXT;done > 1
Rename all .txt files to .TXT. The echo .. > 1 causes all commands to be written to file 1.
Look at that file to see what you will rename and run it with: sh 1
< index >
basic commands - copying
cp -u <file> <destination>
Copy a file with option u for update: copy only when the SOURCE file is newer than the destination file or when the destination file is missing.
cp *thumb* *sized* ../newdir
Copy all files with 'thumb' and 'sized' in the names to a directory that's one level higher to a subdir called newdir. Uses filename expansion.
cp <file1> <file2> <file3> <directory>
Copy multiple files into a directory.
cp -R <dir1> <dir2>
cp -dpR <dir1> <dir2>
Copy the contents of the directory dir1. If directory dir2 does not exist, it is created. Otherwise, it creates a directory named dir1 within directory dir2. It is better the use the options -dp since they preseve the attributes of files and reproduce symlinks.
cp ~/somedir/textfile.txt .
Copy the file to current dir and keep the same name.
for i in `find . -name "*.img" -o -name "*.hdr"|grep -v outputdir`;\
do cp -i $i ./outputdir/`dirname $i |sed 's/^.\///g'``basename $i | sed 's/con_/_con/g'`; done
Copy all .hdr and .img files to ./output dir and name them <directory_name><basename>
Explanation:
for i in `find . -name "*.img" -o -name "*.hdr"|grep -v outputdir`;
Find all files with extensions .hdr and .img in all subdirectories. Exclude the output directory (since it may also contain files from previous runs) with grep.
do cp -i $i
Copy the file from its original location to
(the -i option is to specify that we don't want to overwrite any files in the outputdir so
if a file already exists it will prompt you with a question whether to overwrite or not)
./outputdir/
(copy to) ./outputdir specifies the output directory that will contain all copied&renamed files.
`dirname $i |sed 's/^.\///g'`
Take the original directory-name, it will output the patient prefixes like 4001. Strip the leading ./ with sed to give a name like 4001
`basename $i | sed 's/con_/_con/g'`;
Specify the name of the file to copy to. Replace con_ with _con
< index >
basic commands - less
Opposite of more, read the content of a file and scroll through it.
o <filename>
o is short for less, so display the contents of a filename. Defined in alias: alias o='less'.
options for less:
<PgUp> or b
Scroll back one page.
<PgDown> or space
Scroll forward one page.
g
Go to the beginning of the file.
G
Go to the end of the text file.
1G
Go to the beginning of the text file.
/pattern
Search forward in the text file for an occurence of the specified characters.
?pattern
Search backwards in the text file for an occurence of the specified characters.
n
Repeat the previous search.
N
Repeat the previous search backwards.
?pattern
Search backwards in the text for an orrurance of the specified characters.
q
Quit.
v
to enter evil edit mode vi. <Esc> :wq <Enter>(you're now back into less) q to exit less.
m<z>
The letter m followed by another letter to mark position in the file.
'<z>
The tick followed by the letter you used to mark the file to go back to the mark.
<Shift>G
Go to the end of the file.
less /proc/uptime|head
Pipe some input to head to get the first ten lines of output. Looks like cat.
< index >
basic commands - ls
List directory contents.
ll
Aliased for ls -l. List of files and directories that do not start with a dot (normally hidden files and directories).
ls -d */
List only directories in the current directory, not starting with a dot (.)
ls -la |grep ^d
List only directories in the current directory, also display directories starting with a dot (.)
ls *
List all dirs and recurse one dir down.
ls /*
Display the directories and the contents of one recursive directory.
ls -l /etc /bin
List the files in the /bin directory and the /etc directory in long format.
ls -lt |head
Display the ten newest files from a directory output.
ls -1 -t | tail -3
Prints three oldest files (or directories).
rm `ls -1 -t | tail -3`
Remove the 3 oldest files. Things between backquotes get executed.
rm -R `ls -1 -t | tail -1`
rm -R $(ls -1 -t | tail -1)
Remove the oldest directory.
cat `ls -1 -t |head -2`
Display the (text)contents of the two newest files.
for i in `ls -1 -t |head -2`; do cat $i; done
Same as command above (cat) but more flexible.
ls ?oo
Lists files named boo and foo. ? specifies one character whereas * means multiple characters.
ls *.{cpp,c,cxx}
List only files that have the extensions: .cpp, .c and .cxx. Called glob expression.
ls -l /dev/hda
Shows a b on the far left of the listing, which means that your hard disk is a block device (source: rute node 21).
ls -l /dev/dsp
Your sound recorder is a character device and you can see that by the c in the directory listing on the beginning of the line.
stat <file_or_dir>
This shows you a more complete listing on access times and modification time of a file.
ls -li
List the inodes for each file in front of the filelist. Useful for checking out whether two files are hardlinked to each other.
Note that the number of links pointing to a file is also displayed in the third column.
< index >
basic commands - find
find . -name '*.php'
find . -name '*.php'
find . -name '*.php' -exec rm {} \;
An example for selectively removing files recusively with find and rm.
Todo: How can I do multiple extensions here?
find . -name '*.html' -o -name '*.php' |wc -l
Count the number of files with a couple extensions recursively. Specify multiple extensions in find with -o.
find . -type f -print|wc -l
Displays the total number of files in the current working directory and all of its subdirectories. Type f is for files. wc -l for count number of lines.
find . -name <directoryname> -type d
Search for directories.
find -size [[+|-]]<size>
Finds only files that have a size larger (for +) or smaller (for -) than <size> kilobytes.
find <directory> [<directory> ...]
find `find . -name <directorypattern> -type d` -name <filenamepattern>
Starts find in each of the specified directories.
find . -type f -exec ls '-al' '{}' ';'
Executing commands in find.
find . -name *.txt |xargs mv --target-directory=./some_dir
Find all files *.txt recursively and mv them to some_dir.
for i in `find -name "*"`;do dir=$PWD;cd $(dirname "$i");echo "$PWD/$(basename $i)";cd $dir; done
Return the absolute path and filename of files and directories.
Does not work for filenames and directories that contain spaces.
find ./ -type f
Returns all files from the current directory down. Files in the output will all have a path starting with "./" (hence a relative path).
find /etc -type f
Returns all files below /etc. All outputted files will have paths starting with /etc/
(absolute paths) because you supplied it the absolute path (/etc) to start with.
find . -name "*.jpg" -o -name "*.txt"
Find all files with the extensions jpg and txt from the current dirextory (and recurse to lower dirs).
find . -type f -iregex ".*\.jpe?g"
Find all files with extensions: jpg and jpeg.
find . -type f -iregex ".*\.jpe?g" -exec du -k {} \;| awk '{ total += $1 } END\
{ print total/1024 " Mb total" }'
Using find and awk to add the number of mb's for all files with extensions .jpg and .jpeg. Output in the form of: 0.0078125 Mb total
Option: iregex for case-insentive regular expression matching.
find . -type f -iregex ".*\.jpe?g" -ls | awk 'BEGIN {print "0"}; {print $7, "+"}; END {print "p"}' | dc
Using find and awk to add the number of mb's for all files with extensions .jpg and .jpeg. Output is just the total of the size column from ls.
find -ls | awk '$11 != "." {total+=$7;printf("%12d\t", $7);for(i=11;i<=NF;i++){printf("%s", $i)}printf("\n")}END{print "Total: ", total}'
Another find and awk to count the total filesize for all files. Also prints the filesize and file per file in a listing.
find . -iname <filename>
Find files with <filename> case-insensitive and recursively.
If you want to include wildcards and not completely specify the filename use quotes, so "te*"
find ./ -mtime -1 -print
Find files and folders recursively that been modified in the last 24 hours.
find ./ -mtime -1 -type f -print| xargs tar -czf ../tartest.tar.gz
Find files and folders recursively that been modified in the last 24 hours and pack them into one tar one level up.
< index >
basic commands - mv
Move files or directories.
mv <file1> <file2>
If file2 does not exist, then file1 is renamed file2. If file2 exists, its contents are replaced with the contents of file1.
mv all-20050412/* .
move all files in directory <all-20050412> to current directory.
mv -i <file1> <file2>
Move with (interactive) option specified, if file2 exists, the user is prompted before it is overwritten with the contents of file1.
for i in *.zip; do mv $i ${i/foo?ar/foobar}; done
Rename (move) all files called foorar,fooxar to foobar.
< index >
basic commands - mkdir
Create directories.
mkdir -p <somedir>/<someotherdir>
Create a directory in a subdirectory without first having to cd to the directory with the -p option.
mkdir somedir1 somedir2
Create multiple directories in one command.
< index >
basic commands - diff
Find differences between two files.
diff 01.txt 02.txt -y > new_text.txt
Displays the results in two clever colums in a text file.
diff -u 01.txt 02.txt
Output NUM (default 3) lines of unified context. It displays the filenames and last dates the files where edited.
Can also be used for applying patches together with the command patch (source: http://www.linuxjournal.com/article/1237).
diff ./dir1 ./dir2
Do a comparison to see which files are missing or present in these dirs.
If same files are present, a diff between them is done.
diff -r ./dir1 ./dir2
A recursive diff, a comparison to see which files are missing or present in these dirs.
If same files are present, a diff between them is done.
diff <(ls dir1) <(ls dir2)
Doing a diff on two directories using process substitution.
< index >
basic commands - history
history |grep ls
An example to filter 'the commands starting with ls' from your shell-history.
history -c
Clear the history file.
< index >
basic commands - touch
Generates files.
touch logfile-`date -dyesterday '+%Y-%m-%d'`
Generate the file 'logfile-2006-02-03' (yesterdays's date)
for i in $(seq 1 100); do touch test$i; done
Generate empty files test1 to test100.
< index >
basic commands - calculate
echo $((2+2))
echo $[2+2]
Calculate whole numbers on the commandline. $[] is not a standard unix notation. Use expr for that.
The ((...)) command uses these operators: '==', '!=', '>', '<', '>=', and '<='.
expr <arithmetic expression>
expr 5 + 10 '*' 2 '*' $1
expr \( 10 - 3 \) % 3
7 modulo three gives 1 as output.
expr \( 10 - 1 \) / 3
9 divided by three gives 3 as output.
Calculate an arithmetic expression. Single protect the inside text from interpretation by the shell. Expr is a standard notation.
expr 5 % 2
expr 4 % 2
Modulo to test for un/evenness of numbers, returns 0 for even numbers and 1 for un-even numbers.
man bc
Calculate things with non-whole numbers on the commandline.
cal [[0-12] 1-9999]
cal 3 2006
Print out the calendar for a year or month.
du -s /home/* |sort -nr
Get the size in kb of all homedirs if you are root. When not logged in as root you only see your own.
j=1;for i in `cat file1`;do echo $((i+`sed -n $j'p' file2`));j=$((j+1));done
Adding each number on line x of file1 to the number on line x of file2.
for (( i = 1; i <= `wc -l file1| awk '{ print $1}'`; i++ ));\
do echo $((`sed -n $i'p' file1`+`sed -n $i'p' file2`)); done
Another way of adding the totals of each line from two different lines together.
if let "1+1"; then echo not zero; else echo zero; fi
if ((1+1)); then echo not zero; else echo zero; fi
Two ways of evaluating an arithmetic expression. If the value of the expression is non-zero, the return status is 0; otherwise the return status is 1.
< index >
ftp - ftp
An example of a log for an ftp-session:
user@linux:~> cd xxxxxxxxxx/test/
user@linux:~/xxxxxx/test> ftp ftp.xxxxxxx.com
Connected to ftp.xxxxx.com.
220 ProFTPD 1.2.9 Server (ProFTPD) [xxxxxxxxx.com]
Name (ftp.xxxxxxxx.com:user): xxxxxxx
331 Password required for xxxxxx
Password:
230 User xxxxxxxx logged in.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
229 Entering Extended Passive Mode (|||35338|)
150 Opening ASCII mode data connection for file list
-rw------- 1 xxxxxx psaserv 330 Oct 18 17:18 README.txt
drwxr-xr-x 2 xxxxxx psacln 4096 Dec 1 12:56 test
226-Transfer complete.
226 Quotas off
ftp> lcd
Local directory now /home/user
ftp> lcd xxxxxxxx/test/
Local directory now /home/user/xxxxxxxxx/test
ftp> ls
229 Entering Extended Passive Mode (|||35340|)
150 Opening ASCII mode data connection for file list
-rw------- 1 xxxxxx psaserv 330 Oct 18 17:18 README.txt
drwxr-xr-x 2 xxxxxx psacln 4096 Dec 1 12:56 test
226-Transfer complete.
226 Quotas off
ftp> cd test
250 CWD command successful.
ftp> mput *.txt
mput robots.txt [anpqy?]? y
229 Entering Extended Passive Mode (|||35345|)
150 Opening BINARY mode data connection for robots.txt
100% |*************************************| 286 1.80 MB/s 00:00 ETA
226 Transfer complete.
286 bytes sent in 00:00 (1.91 KB/s)
ftp> ls
229 Entering Extended Passive Mode (|||35346|)
150 Opening ASCII mode data connection for file list
-rw-r--r-- 1 xxxxxx psacln 286 Dec 1 13:01 robots.txt
226-Transfer complete.
226 Quotas off
ftp> bye
221 Goodbye.
Mput * will ask you [anpqy?]. Type p to go into "prompt off" to not confirm the upload of every file but to do it non-interactively.
The [anpqy?] stand for 'all' (which answers yes to this and all subsequent files), 'no' (don't download this file), 'prompt off', 'quit' to terminate the current mget and 'yes' (download this file) with the '?' returning a short list of this stuff.
ftp> mput *
mput robots.txt [anpqy?]? p
Interactive mode: off.
local: robots.txt remote: robots.txt
229 Entering Extended Passive Mode (|||35420|)
150 Opening BINARY mode data connection for robots.txt
100% |*************************************| 286 1.84 MB/s 00:00 ETA
226 Transfer complete.
286 bytes sent in 00:00 (1.88 KB/s)
local: robots2.txt remote: robots2.txt
229 Entering Extended Passive Mode (|||35422|)
150 Opening BINARY mode data connection for robots2.txt
100% |*************************************| 286 1.55 MB/s 00:00 ETA
226 Transfer complete.
286 bytes sent in 00:00 (1.91 KB/s)
In the standard FTP client, you use the shellescape operator, !, to run a command on the local system, such as !ls or !mkdir
Commands under ftp:
binary
Switch to binary mode to transfer files.
ascii
Switch to text-mode to transfer text-files.
get <filename>
Transfer a file from the remote location to your machine.
mput <filename>
Transfer a file from your machine to the remote location.
delete <filename>
Delete files.
< index >
ftp - chmod under ftp
CHange MODe (CHMOD) is the Unix command and system call to change the access permissions of a file or directory.
In other words, CHMOD means to set permission.
The most common chmod value (for scripts) is 755 which = (rwx r-x r-x)
read=4, write=2, execute=1. owner, group, public.
rwx = 111 in binary = 7
rw- = 110 in binary = 6
r-x = 101 in binary = 5
r-- = 100 in binary = 4
chmod 644 test.html
Make the file readable for everyone and writable for the owner.
chmod 755 test.html
Make the file readable for everyone and only writable for the owner.
chmod 711 test
Make this dir accessible for everyone and only writable by the owner.
< index >
ftp - gftp
gftp [[ftp://][user:pass@]ftp-site[:port][/directory]]
gftp-text
Get the command-line interface in an X terminal window.
Local commands: use lls to list the local directory; lmkdir to make a local directory;
and so on. (In the standard FTP client, you use the shellescape operator, !,
to run a command on the local system, such as !ls or !mkdir. gftp-text can't do a shell escape.)
You can download directories recursively (a directory and all of its files), or just a single file, by specifying the URL when you start gftp-text, like this:
$ gftp-text -d ftp://ftp.abc.xyz/somedir/
< index >
ftp - other ways to ftp
sftp://<IP-Address>
The easiest way to use ssh to transfer files securely with konqueror.
scp username@ssh.domain.com:./tmp/file.gz .
Transferring a file from a remote machine to a local machine.
scp file.txt user@remotehost:.
scp [-l <username>] [-r] [<remote_machine>:]<file> [<remote_machine>:]<file>
Transferring files with ssh to a server from a local source.
scp -r ./<local dir>/ username@domain.com:./<dir on the server>
Transferring files recursively with ssh to a server from a local source (overwrites the old files on the server without asking).
fish://
ftp over ssh with a graphical interface(konqueror) and drag and drop possibilities.
< index >
bash - general bash tricks
<Shift>arrow left/right
Toggle between multiple shell (window) numbers.
rm ./-badname
Deleting a file that starts with a hyphen (-) by specifying the full path.
If you don't specify the full path rm will think you are specifying an option.
< index >
bash - screen
Use screen to toggle between multiple windows in the same console or terminal emulator.
Source: http://www.kuro5hin.org/story/2004/3/9/16838/14935
screen
Start a screen session
<Ctrl>a c
Create a new screen.
<Ctrl>a p
Switch to the previous screen.
<Ctrl>a n
Switch to the next screen.
<Ctrl>a twice
Switch to the last screen.
<Ctrl>a N
Switch to screen number N.
<Ctrl>a A
Type a name for this screen.
<Ctrl>a "
Show all screens that are open.
<Ctrl>a [
Start a visual select mode to copy text. Move around with hjkl or arrow keys.
Space to start select, another space to end select and copy text into buffer. Use <Ctrl>a ] afterwards to paste the text.
<Ctrl>a ]
Paste copied text.
<Ctrl>a d
Detach screen session. All the programs in the screen session will continue to run.
screen -r
Reattach previous screen session.
<Ctrl>a k
Kill this screen. If you kill all screens screen will exit.
screen command
If screen is started this will spawn a new screen window and run that command in it.
<Ctrl>a ?
Get a list of commands you can use in screen.
screen -list
See all screen sessions available.
screen -S name
Create a name for the session so its easier to reattach.
<Ctrl>a h
Save a hardcopy to a file in your homedir called hardcopy.0
<Ctrl>a H
Start logging to a file in your homedir called screenlog.0
<Ctrl>a m
Repeat the last message from screen.
<Ctrl>a x
Lock this screen.
<Ctrl>a _
Monitor this window for silence. When it is silent for 30 sec it will display a message in the other window.
Splitting of regions:
<Ctrl>a S
Split this region and spawn a blank window.
<Ctrl>a :
Enter command mode for screen.
screen
In command mode: open a screen in the newly spawned region.
<Ctrl>a <Tab>
Switch focus to the other region.
remove
Kill this region so you go back to just one region.
Reattaching your session each time you restart a console:
screen -S name
Create a name for the session so its easier to reattach.
Put is in your ~/.bashrc:
# resume the previous screen session
screen -r name
< index >
bash - regular expression
See also: http://www.regular-expressions.info/
You can test regexp using sed.
Save a buch of stuff in a textfile and: sed 's/regexp/output/g' textfile
Or just do: sed -n 's/t.*r.*/&/p' and start typing away.
Sed will duplicate lines which matches tara, for example.
Remember that you have to escape each and every ( ) { } and + with a backslash so use \}
It might be easier too use kate where you can search for regexp with the search function.
if [[ string =~ s.ri.g ]]; then echo "yes"; else echo "no"; fi
A way of testing regexp.
[[ string =~ regexp ]] returns 0 with a match and 1 when there is a match.
\d
Matches a single character that is a digit.
\w
Matches a "word character" (alphanumeric characters plus underscore).
\s
Matches a whitespace character (includes tabs and line breaks).
\t
Match a tab character (ASCII 0x09).
\r
Carriage return (0x0D)
\n
Line feed (0x0A).
\r\n
Window's style to terminate lines.
\n
Unix text-ending of lines.
\xFF
Specify a character by its hexadecimal index in the character set.
E.g. \xA9 matches the copyright symbol in the Latin-1 character set.
\uFFFF
Insert a Unicode character. E.g. \u20A0 matches the euro currency sign.
.
The dot matches all characters and is short for [^\n] or [^\r\n] (windows)
Example: s.me matches some and same
[ ]
Specify a single character class
[ae]
Single character class for a and e.
Example: s[ae]me matches same and seme but not saeme
[0-9]
Single digit from 0-9 in a character class.
Example: s[0-9]me matches s1me and s0me
[0-9a-fA-F]
Match a single hexadecimal.
[0-9a-fA-FX]
Match a single hexadecimal or X
q[^x]
^ (caret) right after the opening bracket [ negates,
so matches any character not in the character class.
Example: qu in question / not iraq
cat|dog
| is or, and can be used as cat|dog|puss
Example: matches cat or dog alternatingly.
colou?r
? Means the preceding token is optional.
Example: matches color and colour
*
Match the preceding token zero or more times.
+
Match the preceding token once or more.
<.+>
Matches <EM> test </EM>
<[A-Za-z][A-Za-z0-9]*>
Matches a html tag like <html>
<[^<>}]+>
A quick match for a html tag, matches <html>
[0-9]{3}
Specify the number of repeats for the previous token or character class
in curly brackets.
Example: matches 000 012, not 12
\b[1-9][0-9]{3}\b
Matches a number between 1000 and 9999
\b[1-9][0-9]{2,4}\b
Specify a range of repeats. {2,3,4} is not allowed.
Matches a number between 100 and 99999
Set(Value)?
Round brackets () for grouping tokens together into a capturing group.
Example: matches Set and Setvalue
\p{L}
Matches a single character that has a given Unicode property (not really working in sed ?).
^$
Match a blank line.
Anchors:
^
Start of the string.
$
End of the string.
^b
The first b in bob.
\b
Match a word boundary or match a position between a character that can be matched
by \w and a character that can not be matched by \w
\B
Match every position where \b cannot match.
Character class and Description
[:alnum:]
Alphanumeric [a-zA-Z0-9]
[:alpha:]
Alphabetic [a-zA-Z]
[:blank:]
Spaces or tabs. [ \x09]
[:cntrl:]
Any control characters. [\x00-\x19\x7F]
[:digit:]
Numeric digits [0-9]
[:graph:]
Any visible characters (no whitespace). [!-~]
[:lower:]
Lower-case. [a-z]
[:print:]
Non-control characters. [ -~]
[:punct:]
Punctuation characters. [!-/:-@[-`{-~]
[:space:]
Whitespace. [ \t\v\f]
[:upper:]
Upper-case [A-Z]
[:xdigit:]
hex digits [0-9a-fA-F]
[[:alpha:]]
Matches the same as [a-zA-Z]
[[:alnum:]-_.]
Matches [a-zA-Z] as well as hyphens, underscores and periods.
< index >
bash - expansion
~
Expands to your home directory stored in $HOME, so /home/<yourusername>. Called tilde expansion.
~+
Expands to the current working directory stored in $PWD.
cat a{1,2,3}
Example of brace expansion. Cats the files a1 a2 and a3.
$VAR
${VAR}
Two examples of parameter expansion for anything that starts with a $.
The braces keeps bash from getting confused when VAR contains non-whole words.
${HOME:3:4}
Display, from the third character, four characters from the variable $HOME.
${#VAR}
Display the length of VAR.
${!H*}
Expands to all variables starting with H.
${HOME#/home/}
Strip /home/ from start of variable $HOME. Leaves you with just the $USER.
${BASH#*b}
Strip shortest match from the beginning of variable $BASH (b in this case). Leaves in/bash
${BASH##*b}
Strip longest match from the beginning of variable $BASH (/bin/b in this case). Leaves ash
${BASH%b*}
Delete shortest trailing match of characters (bash in this case) from the variable $BASH. Leaves /bin/
${BASH%%b*}
Delete longest trailing match of characters (bin/bash in this case) from the variable $BASH. Leaves /
${BASH/bash/bar}
Replace occurrence of bash with bar in the variable $BASH. (source: rute node 23)
${BASH/b*/}
Replace b followed by anything by nothing. Use # and % to denote resp. begin and end of the variable.
${BASH/ */}
Output the first word delimited by a space. Replace space followed by anything by nothing.
Note: all the examples should be echoed like so:
echo ${BASH}
Gives /bin/bash
echo ${HOME}
Gives /home/username ($USER)
Indirect Expansion:
Bash uses the value of the variable formed from the rest of parameter as the name of the variable.
This variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself.
Example:
some_var=value_of_some_var
var=some_var
echo ${!var}
Will give you: value_of_some_var
< index >
bash - wildcards
*
Matches any characters.
?
Matches any single character.
[characters]
Matches any character that is a member of the set characters or a range of characters.
For example, [A-Z] represents all uppercase letters.
[!characters]
Matches any character that is not a member of the set characters.
With wildcards we can construct patterns:
BACKUP.[0-9][0-9][0-9]
Another example of ranges.
This pattern matches any filename that begins with
the characters "BACKUP." followed by exactly 3 numerals.
[!a-z]*
Any filename that does not begin with a lowercase letter.
Tip: before removing things with wildcards, do ls <wildcards> first to see what is selected; then go <PgUp> and replace ls with rm.
< index >
control structures - for i in
for (( i = 1; i <= `wc -l 01_x16077_18S_fasta.txt| awk '{ print $1}'`; i++ ));do echo $i; done
Print a file line per line. In this case we echo only the line-number.
for i in ~/.*pro*; do cat $i; done
Display the contents of all files in ~/ that have 'pro' in them.
for (( i = 1, j = 1; i <= `wc -l 01_x16077_18S_fasta.txt| awk '{ print $1}'`; i++ ))do sed -n $i'p' 01_x16077_18S_fasta.txt|wc -c; sed -n $i'p' 01_x16077_18S_fasta.txt; done
Display the number of characters per line from a file.
for i in *.txt; do echo cp $i; done
for i in *.txt; do out=${i/foo/bar};echo cp $i $out; done
for i in *.txt; do out=${i/foo/bar};`cp $i $out`; done
Building up the command for copying (or anything else you want to do) with a for loop and a variable called out.
for i in *.txt; do out=${i/foo/bar};echo cp $i $out; done|sh
Instead of replacing the echo with `` you can pipe the commands into a subshell, by adding "|sh" onto the end of the line.
for i in cows sheep chickens pigs; do echo "$i is a farm animal"; done
Input in a for loop can be defined like so.
for i in /somedir/*/*.txt; do echo "These are all textfiles: $i"; done
Example of looping over glob expressions. For allows you to specify an absolute dir. The filenames may contain spaces.
for img in *.tif; do echo convert $img file0$((1+${img:4:2})).tif --append newfilename${img:4:6}; done | sh
If the files are named file01.tif. Do some adding to the number so for file01.tif the output is:
convert file01.tif file02.tif --append newfilename01.tif
for ((i=0; i<10; i++)); do echo $i; done
Count from 0 to 9 using a for construct.
< index >
control structures - if
X=1
if test "$X" -gt "1" ; then
echo "$X is greater than 1"
elif test "$X" -lt "1" ; then
echo "$X is less than 1"
else
echo "$X is equal to 1"
fi
< index >
control structures - case and while
while test "$1" != "" ; do
case $1 in
--help|-h)
echo " myprog.sh [--test|--help|--version]"
exit 0
;;
-*)
echo "No such option $1"
exit 1
;;
esac
shift
done
This is an example of a small menu to process options passed to the script from the commandline.
while read name; do echo ${name}; done < file
Read a file (and echo it) line per line.
< index >
control structures - until
i=0; until [ $i -ge 10 ]; do i=$(($i+1)); echo $i;done
An example of the use of an until loop construct which counts to 10.
< index >
control structures - select
test="1 2 3 4"
select blah in $test;
do
echo you picked number $blah \($REPLY\)
break;
done
An example for a select construct. It displays a list prefixed with numbers in front of all the options. It then asks the user to enter an option.
< index >
texteditors - vi
A console-based texteditor that's installed in any system.
http://www.udel.edu/topics/software/general/editors/unix/vi/commonvi.html
For learning vi try the command: vimtutor
vi newfile.txt
Start writing in a new file. i for insert mode to type text. If you're done typing <Esc> for returning to the command mode.
To save the text make sure you are in command mode by pressing <Esc> and than save your file by :w
To exit and save the file in one command press :wq
From the command <alias> i see that <o> is short for the <less> command. Through less you can also start vi by pressing <v> or <Insert>.
Short example to edit a file:
vi <somefile>
Edit file with vi from a commandline.
Once the file is opened:
/ftp <Enter>
Search for the word 'ftp'
0
(zero) Go to beginning of the line.
x
Remove individual characters.
dd
Delete one line.
i
Enter the Insert mode to type text.
<Esc>
To get back to the commandmode.
:wq
Write the file and quit.
q!
Don't save the changes, just exit vi.
Commandmode commands:
<Insert>
Go to insert mode, where you can edit a file.
3dd
Deletes three lines.
ZZ
Save and exit.
:set list
To display all formatting characters such as tabs and enters.
:set nolist
To turn off display formatting.
:set number
Prefix each line with the number of that line (see: set nonumber).
:set nonumber
Turn off line numbering (see: set number).
:h
Display help.
:syn on
Turn on pretty color syntax highlighting for programming files with code in them.
:syn off
Turn off syntax highlighting.
<Alt>u
Undo last action.
:e
Open file. Example: :e ~/.bashrc Use tab-completion to browse to the files.
:!<command>
Execute command line command.
Example :!pwd will suspend vi and execute the 'pwd' command from the command line.
.
Repeat last command.
:r <file>
Read in file. It pastes the text of that file from the cursor into current document.
<Ctrl>z
Suspend vi and current file. To go back to suspended vi type: fg to bring it back to foreground.
:sh
Create a bash subshell to type commands in.
When you are done, press <Ctrl>d or type exit <Enter> to return to vi.
:w
Save the current file.
:w <filename>
Save to file filename.
Multiple windows in vi in commandmode:
:new
Open new blank window.
:e
Open file. Example: :e ~/.bashrc Use tab-completion to browse to the files.
:wq
Save and close this window.
<Ctrl>+w n
Split the current window horizontally in two and start editing a new file in it.
:vne [<file>]
Split into a new vertical window and start editing a new or existing file in it.
<Ctrl>+w arrows
Move the cursor to other windows with up and down arrowkeys.
<Ctrl>+w r
Rotate windows downwards/rightwards.
<Ctrl>+w _
Maximum height for the current window.
<Ctrl>+w =
Make all windows equallt in height and wide.
<Ctrl>+w o
Close all other windows besides the current one. Does the same as :on
<Ctrl>+w q
Close this window.
:on!
Close all other windows and override non-saved files stopping from closing.
Moving commands in commandmode:
gg
Beginning of file.
G
End of file.
e
End of word.
}
Next paragraph.
{
Previous paragraph.
^
or a
First character of line.
0
First position of line.
$
Last position of line.
:ma z
Mark line with z.
mz
Mark line with z.
:'z
Return to line with mark z.
`z
Move cursor to mark z.
z<enter>
Position line with cursor at top of screen.
z.
Position line with cursor at middle of screen.
z-
Position line with cursor at bottom of screen.
w
Beginning of next word.
b
Beginning of previous word.
Editing commands in commandmode:
The general form is [n] operator [m] object.
If the current line is the object of the operator then the object is the same as the operator: cc, dd, yy.
x
Delete character under cursor.
3x
Delete 3 characters under cursor.
p
Paste character from register (clipboard) after character under the cursor.
Cut and paste letters.
2p
Paste characters from register 2 times.
c$
Change to the end of the line.
c^
Change to the begin of the line (delete as well).
Puts characters from the begin to the character before current in register.
y
Begin a yank.
yy
Yank current line. yy p is copy pasting of lines. 5yy to yank five lines.
dG
Delete to end of file.
dgg
Delete to beginning of file.
d'z
Delete to line marked with z (includes line marked). Copy paste a number of lines.
3dw
Delete three words.
d}
Delete this paragraph.
:,$s/foo/bar/gc
Search and replace with confirmation starting at line below cursor.
:map <F2> /[A-Z]<cr>~0
Example how to map F2 to a function that lowercases.
Afterwards repeatedly pressing F2 will lowercase the character behind the cursor.
:map <F2> /[a-z]<cr>~0
Example how to map F2 to a function that uppercases.
!}tr 'q' 'e'
For this paragraph, translate q into e.
:,$s/^[\ ]\{5\}//gc
Remove the first five spaces from each line, with confirmation.
J
Join lines. The next line will be added to the end of the current line.
gU<arrow left>
Uppercase the character under the cursor.
gUw
Uppercase the word under cursor, from the character under cursor.
guw
Lowercase the word under cursor, from the character under cursor.
Editing commands that leave you in insertmode afterwards:
A or $a
Append at end of line and stay in insert mode.
O
Insert a blank line above the current line.
o
Insert a blank line after the current line.
I or _i
Start typing at the beginning of the line.
S
Substitute entire line by whats typed next.
cw
Change word. Delete the word under cursor.
Insertmode commands:
<Esc>
Go to commandmode. Does the same as <Alt><Shift>;
<Ctrl>a
Paste text from buffer under cursor. To get text into the buffer, use delete for instance.
<Alt>u
Undo.
help in vi:
:h
Open the helpfile in a new window.
<Tab>
Tab to the next helplink for a subject.
<Ctrl>]
When the cursor is over a link, follow that link to that subject.
<Enter>
When the cursor is over a link, follow that link to that subject.
<Ctrl>t
Return to the helppage you were before.
ZQ
Close the helpwindow and return to your file.
:h <topic>
Look up pages on the specified topic.
:h help
Explanation about the usage of help files in vi.
Handling multiple files:
vi <file1> <file2>
Open vi with multiple files at once.
:next
Switch to the next file, also :n
:previous
Switch to the previous file, also :N
:file
Show the filename for the current document.
Other useful things:
:readDIR
Insert the current directory in this file.
:1,3!sort
Sorting lines with a command
Search and replace for all lines in marked section in commandmode:
mz
Mark the last line of the text block you want to change.
Now move to the first line of the block you want to change.
:,'zs/^/bar/g
From the current line to the marked line (,'z), replace (s///g) beginning of the line (^) with bar.
:,'zg/^$/d
From the current line to the marked line, delete all empty lines (^$).
More advanced commands in commandmode:
:%!sed -n '1~2p'
Delete all even lines. See also the sed commands. % means the command works on all lines.
:%!sed -n '2~2p'
Delete all uneven (odd) lines. See also the sed commands. % means the command works on all lines.
:%!awk 'FNR%2'
Delete all even lines using awk.
:map # jdd# 1G#:unmap #
This maps # to delete all even lines. Repeatedly pressing # will keep on deleting even lines again.
:map # ddj# 1G#:unmap #
This maps # to delete all uneven lines. Repeatedly pressing # will keep on deleting uneven lines again.
:,$s/\n//gc
String all lines together. Delete newlines / endlines.
:,'zs/\n//g
First mark the end of the section with mz; Deletes all endlines in marked section like join lines.
:,'zs/\t\([^\t]\)/ \1/gc
Replace last tab from the start of a line with two spaces. Usefull for formatting tabs to spaces in code.
Does not really work really userfriendly but it saves some time.
:map
Show all mappings and browse with space per page.
Commandline options when starting vi:
-o *
Open all files in the current dir in separate windows.
Errors and solutions:
No write since last change
When trying to close a file without saving sometimes vi still has it's buffers opened. To delete all buffers do
:1,9999bd!
< index >
texteditors - pico
A console-based text editor.
Also check out: http://oit.uta.edu/cs/unix/editors/pico/pico.html#Saving_Files
Additional startup options for pico: http://www.computerhope.com/unix/upico.htm
I like to start it with: pico -e -k
-e is for tab-complemtion
-k lets you cut text from your cursor to the end of the line.
So i put these as an alias into ~/.bashrc
pico Documents/nerdstuffs/commands.txt
Open a file with pico.
<Ctrl>X
To exit pico and return to the shell.
<Ctrl>O
Save the file, if it does not give you a name try <Ctrl>T to browse to the file,
S to select dirs and files.
<Ctrl>R
To open a file if you know the exact location type that,
otherwise you next do <Ctrl>T to browse dirs and press S the select dirs and files.
<Ctrl>K
Cut a line of text.
<Ctrl>U
To paste that line of text. This works as copy/paste but a littlebit strange since
you start with cutting the line you want to copy/paste.
<Ctrl>space
Move curser to next word.
<Ctrl>G
For help.
The middle mouse button works in pico so if you want to paste a link into pico from somewhere, select that text in your browser (no need to press <Ctrl>C), go to pico and press you middle mouse button to paste the selected text from the other window.
Arrow keys, enter, backspace and delete keys all work as in 'normal' texteditors.
< index >
scripts - general
A 'here script' has the following in its body:
cat <<- _EOF_
<content>
_EOF_
Everything in content is saved to a file when the script is run like: script.sh > index.html
normal if construction:
if [ -f .bash_profile ]
then echo "you have bash_profile!"
elif [ -f .bashrc ]
echo "you have only bashrc. This could be an else without a condition"
fi
if [ "${test1}" = "${test3}" ] ;then echo "suc" ;else echo "fail" ;fi
if [ "${test1/ing/}" = "${test3}" ] ;then echo "suc" ;else echo "fail" ;fi
if [ "`echo ${test1}|sed 's/ing//g'`" = "${test3}" ] ;then echo "suc" ;else echo "fail" ;fi
Testing two strings for equality.
[ <expression> ]
test expression
Two forms of testing an expression such as: -f .bash_profile
to test whether such a regular file exists.
! [ <expression> ]
for negation of the expression. So ! [ -f filename ] to check if a file doesn't exist.
-d file
true if file is a directory.
-e file
True if file exists.
-f file
True if file exists and is a regular file.
-L file
True if file is a symbolic link.
-r file
True if file is a file readable by you.
-w file
True if file is a file writable by you.
-x file
True if file is a file executable by you.
file1 -nt file2
True if file1 is newer than (according to modification time) file2
file1 -ot file2
True if file1 is older than file2
-z string
True if string is empty.
-n string
True if string is not empty.
string1 = string2
True if string1 equals string2.
string1 != string2
True if string1 does not equal string2.
The semicolon (;) allows you to have more than one command on one line.
exit 0
exit a script with exit status 0 = success
id -u
Gives you info on the numeric id of the current user. 0 = for root.
Try id to see both real and effective ids for this user.
if [ $(id -u) = "0" ]; then echo "root"
fi
if [ $(id -u) != "0" ]; then
echo "you are not root" >&2
exit 1
fi
If a user is not root send the error message to standard error instead of putting it into the file we are writing into.
#!/bin/bash -x
The -x option tells bash to turn tracing on which allows you to troubleshoot your script.
set -x
Put tracing on in a commandline to have more output when running scripts. +x to turn off.
echo -n "enter some text >"
# The -n option tells echo not to output a carriage return at the end of the prompt.
if read -t 3 response; then echo "great, you made it"
fi
shift is a shell builtin that operates on the positional parameters.
Each time you invoke shift, it "shifts" all the positional parameters down by one. $2 becomes $1
echo $?
Echo the exit status of the last command executed.
trap "rm $TEMP_FILE; exit" SIGHUP SIGINT SIGTERM
Perform actions when a user kills the script.
echo -e "firstline \n secondline"
Put in an <ENTER> in your output with the echo command.
TEMP_FILE=$TEMP_DIR/printfile.$$.$RANDOM
The $TEMP_DIR variable contains either /tmp or ~/tmp depending on the availability of the directory. It is common practice to embed the name of the program into the file name. We have done that with the string "printfile". Next, we use the $$ shell variable to embed the process id (pid) of the program. This further helps identify what process is responsible for the file. Surprisingly, the process id alone is not unpredictable enough to make the file safe, so we add the $RANDOM shell variable to append a random number to the file name.
$# echos the number of arguments passed to the script when executing.
out=$(command)
out=`command`
Two ways of assigning the ouput of a command to a variable. $(..) is the POSIX command substitution syntax.
echo '$'
Output a literal $. Single quotes protect the enclosed text from the shell.
echo "$1"
Text inside double quotes allows the shell to evaluate variables and such.
Is used to protect breaking of the text caused by whitespaces.
x=`ls *`
Variable takes the output of the execution of a command. Back-qoutes cause the inside text to be run and replace by the output.
x=$(ls *)
Identical to x=`ls *`
X=`expr $1 + 50 '*' 3`
Protext the * from the shell by single-quoting it.
echo $USER
Output the current username.
echo $HOME
Output the user-homedirectory.
. general_functions.sh
Put all your functions in another file and source them to use them.
The 'cat "$@"' construction permits getting input either from stdin or from files.
printenv
See all the variables set in your shell.
< index >
scripts - commandline arguments
$*
All arguments passed to a program.
$1, $2, $3, etc. Expand to the individual arguments passed to the program.
$@ Expands to all arguments.
$0
The name of the script itself. It is immune to shift.
$#
Expands to the number of arguments passed to a script.
$?
Expands to the exit code of the last script run. Whether it was succesfully exited or not.
$$
Expands to the process ID of the shell.
$!
Expands to the process ID of the last background process run. So when you run vi &, you can see the id with echo $!
< index >
scripts - functions
function usage ()
{
echo "Usage:"
echo " myprog.sh [--test|--help|--version]"
}
# call the function usage
usage
< index >
scripts - running scripts from bin
You can view the list of directories where your shell looks for scripts to execute (called PATH) with the following command:
$ echo $PATH
This will return a colon separated list of directories that will be searched if a specific path name is not given when a command is attempted.
You can add directories to your path with the following command, where directory is the name of the directory you want to add:
$ export PATH=$PATH:directory
For me ~/bin was already added.
A better way would be to edit your .bash_profile file to include the above command. That way, it would be done automatically every time you log in.
Move your script into your ~/bin directory and you're all set. Now you just have to type:
chmod 711 my_script
To make it executable.
And execute it with:
my_script
and your script will run.
Current scripts in my ~/bin can be found when 'userscripts' is run.
~/.bash_logout
Is the script run when logging out from the shell.
< index >
ascii
man ascii
Displays the ascii character set with tables.
ps2ascii <somefile>.pdf |less
Browse through the textoutput of a pdf file.
asciiview moon.gif
View an ascii-version of an image (uses aalib).
bb
Nice ascii demo in bash. See: http://aa-project.sourceforge.net/
figlet example
Convert text to ascii-art with figlets.
uni2ascii -D < unicode.txt > htmlcode.txt
Convert unicode (from .nfo files) into html-code (using the -D option) to display on websites.
figlet -I2
/usr/share/games/figlet/fonts
Displays the figlet font directory.
ls -R `figlet -I2` |sort|grep flf
for i in `find \`figlet -I2\` -name *flf`; do echo $i; done;
Display a long list of all flf font-files in the standard figlet.flf folder.
figlet -f digital figlet
Specify a font.flf to use.
figlet -f `figlet -I2`/user_added/smpoison sdlfks
Apparently figlet does not search recursively in its directories for fonts. Example how to specify the dir relatively.
for i in `find \`figlet -I2\` -name *flf`; do echo $i;figlet -f $i example; done;
Display 'example' as ascii for all fonts found in the figlet-font directory and recurse down. Print the used font first.
for i in `find \`figlet -I2\` -name *flf`; do echo $i >> textfile.txt; figlet -f $i example >> textfile.txt; done;
Display 'example' as ascii for all fonts found in the figlet-font directory and recurse down.
Save to a textfile and print the font-name first.
figlet -p < ~/.bashrc
Output of a file in figlets.
ls | figlet -f small -tp
Pipe the output of a command through to figlet. Displays the directory-listing in ascii-art.
Configuring your console for viewing ascii with uni-code characters: (Suse specific)
go to settings -> font -> and select unicode.
go to settings -> save session profile as: unicode_profile
Next time you want a unicode konsole you can run:
konsole --profile unicode_profile
mplayer -vo aa <inputfile>
xterm -fn 5x7 -geometry 250x80 -e "mplayer -vo aa:driver=curses <inputfile>"
Watch movies in ascii with mplayer supporting aalib.
Jave is a very good program for editing ascii-text as well as meddling with figlet-fonts.
Download jave from: http://www.jave.de/download/download.html
Unzip it and start it with: java -jar jave5.jar
for i in `find /usr/share/games/figlet/fonts/ -name "*.flf"`; do ln -s $i ln_`echo $i|sed 's#.*/##g'`; done
Symlinking the figlets from /usr/share/games/figlet/fonts/ (my figlet directory) to the /jave/fonts (first cd to this directory).
All symlinks get the prefix ln_ so it's easier to remove them later on.
< index >
imagetools
ImageMagick has a convert tool which you can use to create thumb-nails like so:
for i in `ls *jpg`; do convert -thumbnail 150x150 $i `basename "$i" .jpg`.png; done
< index >
firefox
~/.mozilla/firefox/<something with some characters>.default/
The standard location for your profile folder where you also can find your bookmark-file.
Some keys that do things:
<Ctrl><PgUp>
Move to the first opened tab (If there are more tab-windows opened, that is).
<Ctrl><PgDown>
Move to the last opened tab.
<BackSpace>
Go back one page.
<Ctrl><ArrowUp>
Go back one page
<Ctrl><ArrowUp>
Go forward one page.
<Esc>
Stop loading the current page.
<Ctrl>l
Go to the location bar.
<Ctrl>h
Open the history window where you can type search-terms to find visited pages.
<Ctrl>b
Open the bookmarks folder in a left window. Here you can type search-terms to find bookmarks.
<Ctrl>j
Go to the search window and
type search-terms to look them up in google (or any other search-page thats selected).
<Ctrl>t
Spawn new tab window.
<Ctrl>w
Close the current tab window.
<Ctrl><Shift>=
Increase font size.
<Ctrl>-
Decrease font size.
< index >
trouble booting into graphic mode
startx
Start the gui (graphical user interface) xwindows when you are in textmode.
sax2
Interactive help to set up your graphics card.
/etc/X11/xorg.conf
The config file where the information on your monitor setup is saved.
< index >
installation of rpm's
rpm -qa
What rpm-packages are already installed? (possibly through a package-manager like yast2)
rpm -q xmms
rpm -qa | grep xmms
rpm -qa | grep 'x..s'
These will show you whether xmms is installed. You can use wildcards (regexp) like dots.
rpm -e <foobar>
The next simplest command to un-install a package.
rpm -i package
Installing a package.
rpm -Va
Verify your entire system and see what might be missing.
rpm -qf /usr/X11R6/bin/xjewel
Let's say you run across a file that you don't recognize. This command finds out which package owns it.
rpm -qpi koules-1.2-2.i386.rpm
You find a new koules rpm, but you don't know what it is. This finds out some information on it.
rpm -qpl koules-1.2-2.i386.rpm
See what files the koules rpm installs.
< index >
partitions
fdisk -l
For viewing your partitions (as root).
cat /etc/fstab
Shows you how your partitions are mounted.
Per line the settings for each partition are there: eg. an automount partition:
/dev/hdb5 /windows/E vfat users,gid=users,umask=0002,iocharset=utf8 0 0
< index >
gimp
ttf & gtk for nice downloadable fonts
For editing your shortcuts goto: File, Preferences, Interface, check 'use dynamic keyboard shortcuts'.
Afterwards you can hover over the menu item you want the shortcut for and press the key (-combination) you want to use for it and you see it appear behind the menu item.
Gimp can also save to ascii text format.
< index >
checking file integrity with md5sum
After a download you may want to check whether the file has come through properly.
md5sum file.txt
Displays the md5sum for files (and can take a long time for big files). Check this number against the MD5SUMS (or a name like it) file which could be on the server you have downloaded that file from.
If you want to match to md5sum directly (instead of checking by eye) use: md5sum -c foobar.md5 or in our case md5sum -c md5sums
(so you don't have to list the actual downloaded file in this command since it searches it from the md5sums-file)
It gives a lot of output:
SUSE-10.0-EvalDVD-i386-GM.iso: OK
md5sum: SUSE-10.0-EvalDVD-i386-GM.iso.torrent: No such file or directory
SUSE-10.0-EvalDVD-i386-GM.iso.torrent: FAILED open or read
But the message we are interested in is the OK, don't bother about the rest of the files which weren't present.
To check the md5sum from a just burned dvd: md5sum /dev/dvd
If this doesn't work try the next solution:
http://lists.samba.org/archive/linux/2005-July/014002.html
I suspect you'll find that it'll be reading trailing zeros/nuls/rubbish at the end, which changes the MD5 hash.
There might be a prettier way, but try:
* finding the size of the ISO in bytes
* dd if=/dev/hdc | head --bytes=<size> | md5sum
$ dd if=/dev/dvd | head --bytes=3621957632 | md5sum
You can also check the md5sum of .isos with the k3b cdburn utility.
md5sum <file1> <file2>
Check multiple files at once with md5sum. Use globbing * for a whole dir.
< index >
console / konsole and configure your commandline
general key bindings in your console / terminal:
For more keys see http://www.faqs.org/docs/bashman/bashref_93.html#SEC100
<Tab>
Use tab-completion to complete the first letters of a command or file you typed.
<Tab><Tab>
List all possible commands.
<Ctrl>r
Backward search through the commandline history.
Type the beginning of the command you are looking for,
hit <Ctrl>r multiple times to browse back,
hit <Ctrl>s multiple times to browse forward, and hit <ENTER> to execute it.
<Ctrl>c to cancel.
<Shift><Insert>
Paste text from the clipboard.
<Ctrl><Shift><Insert>
Paste (PRIMARY) selected text. This does the same as the middle mouse button.
<Ctrl>a
Go to the beginning of the line.
<Ctrl>e
Go to the end of the line.
<Alt>f
Move forward to the end of the next word. Words are composed of letters and digits.
<Alt>b
Move back to the start of the current or previous word.
Words are composed of letters and digits.
<Ctrl>l
Clear the screen. <Shift><PgUp> to see what was cleared.
<Alt><
Move to the first line in the history.
<Alt>>
Move to the end of the input history, i.e., the line currently being entered.
<Ctrl><Alt>y
Insert the first argument to the previous command
(usually the second word on the previous line) at point.
Usefull for doing ls ~/somedir/someotherdir followed by cd <Ctrl><Alt>y
<Alt>.
Insert last argument to the previous command (the last word of the previous history entry).
With an argument, behave exactly like yank-nth-arg.
Successive calls to yank-last-arg move back through the history list,
inserting the last argument of each line in turn. Usefull for doing ls ~/somedir/someotherdir
followed by cd <Alt>. (mutliple times to browse back).
If you want to use it in a command you can use $_
<Esc>.
Does the same as <Alt>.
^old^new
Useful for fixing typos on the commandline.
Suppose you did ls ./we which should have been wer, you do ^we^wer
<Ctrl>t
Drag the character before the cursor forward over the character at the cursor,
moving the cursor forward as well. If the insertion point is at the end of the line,
then this transposes the last two characters of the line. Negative arguments have no effect.
<Alt>t
Drag the word before point past the word after point, moving point past that word as well.
If the insertion point is at the end of the line,
this transposes the last two words on the line.
<Alt>u
Uppercase the current (or following) word.
With a negative argument, uppercase the previous word, but do not move the cursor.
<Alt>l
Lowercase the current (or following) word.
With a negative argument, lowercase the previous word, but do not move the cursor.
<Ctrl>u
Kill backward from the cursor to the beginning of the current line.
The killed text is saved on the kill-ring.
<Ctrl>k
Kill the text from point to the end of the line.
<Ctrl>y
Yank (paste) the top of the kill ring into the buffer at point.
Useful after <Ctrl>u or k, so it works like undo the last kill actions.
<Ctrl>w
Kill backward, the word behind point, using white space as a word boundary.
The killed text is saved on the kill-ring.
<Ctrl>h
Erase backwards from the cursor, character per character.
On other systems this would be done with backspace.
<Del>
Erase forward from the cursor. Does the same as <Ctrl>d
<Alt>d
Kill forward from point to the end of the current word, or if between words,
to the end of the next word. (M-d) for meta d so also <Esc> followed by d.
<Alt>?
List the possible completions of the text before point.
Example: cat somete<Alt>? shows you might have sometext.txt and some2text.txt
<Alt><Shift>8
Insert all completions of the text before point that would have been generated
by possible-completions. Example cat somete<Alt><Shift>8 cats both. Is <Alt>*
<Alt><Shift>[
Perform filename completion and insert the list of possible completions enclosed within braces
so the list is available to the shell. Is <Alt>{
<Alt>/
Filename completion whereas <TAB> might give you bash command completion as well.
<Alt>r
Undo all changes made to this line.
<Ctrl>]
A character is read and point is moved to the next occurrence of that character.
A negative count searches for previous occurrences.
<Ctrl><Alt>]
A character is read and point is moved to the previous occurrence of that character.
A negative count searches for subsequent occurrences.
<Alt>=
Bash command completion.
Example: type ap<Esc>= to list all available commands starting with ap.
Terminal window behaviour:
<Ctrl><Alt>n
Open new shell tab window with a new standard session.
<Shift>arrowkeys
Move around the tab windows with arrowkeys left and right.
nb.1 In all these examples, instead of using <Alt>d for meta d you can also use <Esc> followed by d.
nb.2 Point refers to the current cursor position, and mark refers to a cursor position saved by the set-mark command. The text between the point and mark is referred to as the region.
nb.3 the Ctrl key is always case insensitive; hence Ctrl-D (i.e. Ctrl-Shift-D) and Ctrl-d (i.e. Ctrl-D) are identical.
nb.4 The <Ctrl> key is in other tutorials sometimes displayed as a caret (not in this text).
bind -P
list your key bindings.
bind -p
List your key bindings in a more terse format so
can use it in /etc/inputrc or preferably ~/.inputrc
bind -V
Lists the current Readline variable names and values.
showkey -a
Any character you type will show output in the same form as in bind -P.
Type <Ctrl>d to terminate showkey.
pushd <Somedir>
cd to the dir specified. Use popd to get cd back to the directory where you executed pushd.
popd
cd back to the directory where you executed pushd.
dirs
Show the remembered directories (stored by using pushd).
^word1^word2
Replace occurance of word1 with word2 in last command executed.
Example: ls /var followed by ^var^home gives the result of ls /home
User added key-bindings:
<Alt>w
Show all possible completions for a command from history. (See editing .bashrc)
Example: cp <Alt>w gives you the list with previously executed copies.
< index >
alias
less ~/.bashrc
The config file where you can set your aliases for individual users.
alias
List all aliases currently loaded in your bash.
alias linuxquestions='firefox www.linuxquestions.org'
Make an alias to start firefox-browser with the internet-address www.linuxquestions.org
< index >
editing .bashrc
for starting bash aliases, save/append them in home/user/.bashrc as follows:
# My startupaliases
# lsd is now the command for list only directories
alias lsd="ls -la | grep ^d"
# never remove without prompting
alias rm="rm -i"
from: http://www.geocities.com/h2428/petar/bash_hist.htm
# user added binds to special keys:
# <Alt>w to show all possible completions for a command from history.
bind '"\M-w"':"\"\C-k\C-ahistory | grep '^ *[0-9]* *\C-e.'\C-m\""
Setting the colour for your konsole can be done like so:
set | grep LS_COLORS
This gives you the entry for LS_COLORS which you can copy into your ~/.bashrc and modify.
Either log out and log back in to automatically run ~/.bashrc. Easier is to source it like so without have to log out/in: . ~/.bashrc
/etc/bash.bashrc.local
The file to set aliases for all users (including root).
Make a bash.bashrc.local file for root so it has colour:
cat /etc/bash.bashrc.local
# delete this file to get back to the normal situation.
# set the prompt for root.
# If I am root, set the prompt to bright red
if [ ${UID} -eq 0 ]; then
PROMPT_COLOR='1;31m'
else
PROMPT_COLOR='00m'
fi
PS1='\[\033[${PROMPT_COLOR}\]\$\033[00m\] :\w '
Either log out and log back in to automatically run /etc/bash.bashrc.local.
Easier is to source it like so without have to log out/in: . /etc/bash.bashrc.local
< index >
editing ~/.inputrc
Next is an example. History-search-backwards already works with <PgUp> and <PgDown>
Adding functionality to your commandline:
(as root) you can see that: cat /etc/profile tries to load ~/.inputrc
In ~/.inputrc we can add commands to execute in the commandline, such as forward history search and backward history seach:
vi ~/.inputrc
# user added functionalities:
# get <Ctrl>f forward search functionality through the command-history in console.
"\C-f": history-search-backward
# get <Ctrl>g backward search functionality through the command-history in console.
"\C-g": history-search-forward
< index >
changing the default runlevel
For instance to start linux text-mode only:
edit /etc/inittab.
Look for the line that says "id:5:initdefault:" and change the 5 to a 3.
telinit 3
Change the default runlevel only for the next time you boot your computer.
< index >
running cronjobs
Cronjobs are a way of running scripts at specific intervals or times which means you could leave the house.
To create a cronjob, type crontab -e and set it to run at the appropriate time:
* 6 * * * $HOME/script.sh >> $HOME/script.out 2>&1
This should run the script every minute for the 6 AM hour of everyday. The fields are minute, hour, day, month, day of week. See man crontab. The 2>&1 should output both errors and STDOUT.
If you want the scripts to run under your user ID, use "crontab -e" to edit your personal crontab to schedule the jobs. If you want them to run as root, su to root, then use "crontab -e" to put them in roots crontab.
< index >
system information
uname -a
(= Unix name with option "all") Info on your (local) server.
cat /proc/meminfo
Memory info.
hwinfo |less
Probe all hardware devices (takes some time to finish).
hwinfo |grep dvdrecorder -C 10
Probe all hardware devices and show only hits with info on your dvdrecorder (a littlebit sloppy).
cat /proc/cpuinfo
Cpu info, it shows the content of the file cpuinfo. Note that the files in the /proc directory are not real files, they are hooks to look at information available to the kernel.
system logs:
cat /var/log/messages |less
Also other logs can be found in /var/log/
cat /etc/fstab
When booting the system, the information in this file is used to mount devices.
cat /etc/init.d/README
cat /etc/xinetd.conf
cat /etc/inittab
Read this for some more info on the various scripts that are run for different run levels (init 3 and init 5 and others). This works a bit differently than (source: rute node 32).
/sbin/chkconfig --list
See what's started when booting your system.
set
Shows the complete set of environment variables such as aliases and shell functions.
set |grep <somecommand> can be useful for looking up the variables set for a specific command.
For example: set |grep bash
ulimit -a
Show all limits of your shell.
cat /etc/shells
Gives an overview of known shells on a Linux system.
cat /etc/*release
cat /etc/issue
Shows which distro you are using.
free
Prints out available free memory. You will notice two listings: swap space and physical memory.
echo $HOSTTYPE
Gives as output on my machine: i386
echo $OSTYPE
Gives as output linux, or the operating system that it is executed on.
< index >
networking
lspci
List all pci devices.
ifconfig
(as root) Configure a network interface.
lsusb
List usb devices.
netstat -an |grep LISTEN
Shows a list with the ports on which your computer is listening to. Netstat in general is useful for viewing your connections.
/etc/init.d/network status
Check your network status and connectivity.
route -e
(as root) View your routing table and gateway setup.
cat /etc/resolv.conf
View your domain name server setup (dns).
< index >
less useful:
cat /proc/stat
Shows some statistics on your cpu usage.
cat /dev/mouse
cat /dev/psaux
Read the output (gibberish) from your mouse.
chsh -l
List the shells which are available for you from the file /etc/shells. This command can also change your default shell, this is stored in /etc/password
< index >
users
cat /etc/passwd
Your default shell is set in the /etc/passwd file.
It also displays the full list of users registered on your machine.
An example of the format of each line is (source: rute node 14):
root:x:0:0:root:/root:/bin/bash
<username>:encrypted password:UID:GID:full name:home directory:the default shell for this user.
awk -F: ' $3 > XXX {print $1}' /etc/passwd
Only display the first column of /etc/passwd, so all users that are on your system.
lastlog
Usefull commando to display that users that have lately logged in.
cat /etc/shadow
(as root) Displays some extra info on the users on your system.
An example of the format of each line is:
root:hbflbfvlfbvlsdfbRHBDHBSDGVOSDTRHHCDOC&TETRRN:13123::::::
<username>:password-hash (* means the account is disabled):number of days since the password was changed counted from 1 jan 1970:
days before the password may not be changed:
days after which the password must be changed:days before expiration that user gets a warning:days after which password expires (-1=infinite):
days after which account will be disabled:not used
cat /etc/group
Display the users that belong to a group.
The format of each line is (source: rute node 14):
video:x:33:user
groupname:group password (usually not used):GID:comma separated list of user in this group
groups <username>
Print the groups to which a user belongs.
useradd -m <username>
You can add users manually (copy the template homedir from /etc/skel/) or use useradd and groupadd to add users automatically.
-m option creates a homedir for this user.
passwd <username>
(as root) set the password for this user.
userdel -r <username>
Delete a useraccount. The -r option removes the homedir.
tail -f /var/log/messages -n 20 | grep root
Grab the last 20 messages from the system log which have 'root' in them and continue following (-f).
System error log program (syslog) writes all its error messages to this log.
Normal user logon failures and succesfull root logins are logged.
/etc/security/limits.conf
Contains possible limits for CPU usage and other resources per user.
The user nobody is the default username that apache uses.
cd
echo ${PWD}
This changes directory to your /home/user. Then print the working directory. Stupid way of finding out what user you are.
< index >
automounting on access to a folder (have not tried this yet)
from http://www.novell.com/coolsolutions/feature/8973.html
An even better solution would be to configure autofs to mount the CD images automatically on access. Use something like the following in /etc/auto.master:
/iso /etc/auto.iso --timeout=10
And then this in /etc/auto.iso:
* -fstype=iso9660,ro,loop,unhide,check=r :/isos/&
Then you can automatically mount any file in /isos by just running cd /iso/filename. This makes it easy to have a changing list of CDs without manually mounting or editing the fstab or autofs configuration.
< index >
setting variables
An example to set a variable in a child process bash (source: rute):
x="test"
echo $x
bash
echo $x (you can see that the variable x is not set in the child process)
exit
To able to set the variable under the child process also:
x="test"
export x
bash
echo $x
< index >
words and dictionary
ispell <some_textfile>
Spellcheck textfile; press ? for help.
look <some_word>
If file is not specified, the file /usr/share/dict/words is used. It searches in your dictionary.
wc filename
Count the number of newlines, words, and bytes in files.
< index >
tar
tar tvf <filename>
tar tzvf <name_of_file.tar.gz> | less
View listing for a tar.gz archive
for i in `ls *.tar.gz`; do tar xvf $i; done
ls *tar.gz | xargs -t -i tar zxvf {}
Two ways of untarring a bunch of files.
zcat <filename> | less
zless <filename>
View the contents of compressed text-files.
less <filename>
View the contained files in an archive.
The following comes from Hornet (source: http://www.fluidgravity.com/?page_id=21):
Tar is solely an archiving application. Tar by its self wont compress files.
Now you ask? "then what is a .tar.gz"
It is a tar file that has been compressed with a different compression utility.
The .gz (gzip) is the compression application used to compress it.
code:
tar -cvf filename.tar /path/to/files
-c means create
-f means filename (-f should always be last when you using syntax)
-v Verbose will display all the files its puts in the tar and error you might
have incurred You should see the filename.tar file in what ever directory you
ran tar from.
If you want to make the tarball compressed then -z is the option you want to include in your syntax.
code:
tar -zvcf filename.tar.gz /path/to/files
Now when I make a tarball I like to keep all the path's from which the file is
in. For this use the -P (absolute path)
code:
tar -zPvcf filename.tar.gz /path/to/file
When I extract it I will see a new directory called /path
and under that I will see the "to" directory, and the "file" is under "to"
If want to backup ALL your files in your home directory EXCEPT the temp
directory. No problem.
code:
tar -zPvcf myhomebackup.tar.gz --exclude /home/milkman/temp /home/milkman
The --exclude will give you this option, just slip it in between the tar
filename and the path your going to backup. This will exclude the whole temp
directory. You say you just want to backup only
single files from all around the drive.
Make a file called locations (call it anything you like). In locations place the
full path to each file you want to backup on a new line. Please make sure that
you have the read rights to the files you are going to backup.
code:
/etc/mail/sendmail.cf
/usr/local/apache/conf/httpd.conf
/home/mikman/scripts
Now with the -T option I can tell it to use the locations file.
code:
tar -zPvcf backup.tar.gz -T locations
Now if you want to backup the whole drive. Then you will have to exclude lots of
files like /var/log/* and /usr/local/named/*
Using the -X option you can create an exclude file just like the locations file.
code:
tar -zPvcf fullbackup.tar.gz -X /path/to/excludefile -T /path/to/locationsfile
Now a month has gone by and you need to update your myhomebackup.tar.gz with new
or changed files. This requires a extra step ;)
You have to uncompress it first but not untar it.
code:
gunzip /path/to/myhomebackup.tar.gz
This will leave your myhomebackup.tar.gz mising the .gz.
Now we can update your tarball with -u and then we are going to compress it
again.
code:
tar -Puvf myhomebackup.tar /home/milkman | gzip mybackup.tar
It will add the .gz for you.
I have included a little perl script that I made so I can run it as cron job
evernight and get a full backup each time. It wouldn't be that hard to update
the tarball but I just like full backups. Feel free to use it.
If you want to extract the tarball that is compressed
code:
tar -zxvf filename.tar.gz
-x extract
If it is not compressed then
code:
tar -xvf filename.tar
code:
#!/usr/bin/perl
#sysbkup.pl
#Created by Milkman
#Change These paths to fit your needs.
my $filename="/home/sysbkup/backup";
my $exclude="/home/milkman/exclude";
my $data="/home/milkman/locations";
my $tar="\.tar";
my $gz="\.gz";
$file=$filename.$tar.$gz;
system ("tar -Pzcvf $file -X $exclude -T $data");
(source: http://www.fluidgravity.com/?page_id=21)
unrar e myfirst.rar
unrar e *.rar
Extract a rar file to the current directory.
unrar e -ad *.rar
Extract each rar to a directory with the name of the rar itself.
find . -name "*.zip" -exec unzip '{}' \;
Unzip multiple zip-files and recurse to lower directories (recursive).
for i in `find . -name "*.zip"`; do unzip -d `dirname $i` $i;done;
Unzip multiple zip-files and recurse to lower directories. Extract each zip to the directory it is in (recursive)(dirname).
for i in *.zip; do unzip -d ${i%.zip} $i; done
Unzip multiple zip-files and don't recurse to lower directories. Extract each zip to a directory called by the name of the zip.
Unzip -d <directory> <filename> leaves the original zip in place.
unzip -l somezip.zip
List the contents of a zipfile.
< index >
dd
dd if=/dev/dvd of=DiscX.iso
Creating an image of an dvd; Does this work at all?
dd if=/dev/zero bs=1024 count=<number of kilobytes> > <somefile>
Create an empty file, filled with zero's that are output from /dev/zero. Touch is more useful for creating an empty file if size does not matter.
< index >
symlinks and hardlinks
Making a symlink in KDE konqueror: drag the folder to the desired destination and go for the lowest option: link.
ln -s other ../
In the commandline make a (soft) symlink of the directory 'other' to a directory higher up.
You can also link files to other places.
Then when you go to that dir and: ls -al ,you can see the link displayed as follows:
lrwxrwxrwx 1 user linux 5 2006-01-20 14:08 other -> other
rm <E>
Removing symlinks (here named 'E')
ln <file1> <file2>
Makes a hardlink from file2 to the same data of file1.
Whereas soft-links have a '1' in the second column in ls -al, hard links have the number that displays the number of times a file is hardlinked.
find /path/to/dir/ -type l -printf "%p\t%l\n"
Use find to find symlinks on in that specific dir. Output is in the form: <path to symlink> <file linked to>
< index >
finger
finger -l
Return some settings and option for all users currently logged on.
finger @<hostname>
See who is logged in on <hostname>
< index >
permissions
getfacl /bin/chown
See which user has permissions to execute chown (or any command).
If you mess up the permissions in your system:
for i in $(rpm -qa); do rpm --setperms $i; done
This will restore the permissions of all files that come from rpm packages.
Probably also:
chkstat --set /etc/permissions
This should get your system back to a usable state.
umask
Sets the default permissions for newly created files; it is usually 022. The setting is stored in /etc/profile
< index >
how to kill things
ps -fu <your_user_name>
find out the PID (process ID) of a running process. Or use the command: jobs to get [nr]; to be able to kill %nr
jobs
Display list of active (and stopped) processes and their job names.
kill -15 <PID>
That should shut it down gracefully.
If that doesn't work, you can blow it out of the water with kill -9 <PID>
Kmenu, Sytem, Monitor, Ksysguard (a GUI) which will allow you to view all running processes
and kill any offending process. If the offending program has a visible window you can
Ctrl, Alt, Escape, position the skull and crossbones over the window and left-click.
xkill
if you then click on the naughty program it is killed.
%% or %+ refers to the current job, which the last job stopped while it was in the foreground.
%- refers to the previous job.
Naming a job can bring it to the foregound so %1 does the same as fg %1
Same goes for putting jobs in the background, %1 & does the same as bg %1 (source: rute node 12)
< index >
sleep
sleep <seconds>
Pauses for <seconds> seconds.
usleep <microseconds>
Pauses for <microseconds> microseconds (1/1,000,000 of a second).
< index >
date
hwclock --show; date ; date -u
The commands above show, in succession, the CMOS time (localtime unless specified), the local system time, and UTC system time. (source: http://susefaq.sourceforge.net/howto/time.html)
date -dtoday '+%Y%m%d'
Display the date as 20060205 for today.
date --date='2004-12-13 00:00:00 +0000' +%s
Display 13 december 2004 in seconds since 1 jan 1970.
< index >
networking
An example for doing a reverse lookup (source: rute node 30):
nslookup
set type=PTR
<ip-address>
Returns the hostname for a given ip-address.
less /etc/services
Returns a full listing of standard ports used.
cat /etc/exports
exportfs
(as root) Both show the configuration for the directories you're sharing through Network File System (NFS) (source: rute node 31).
< index >
encription
openssl des3 -salt -in infile.txt -out encryptedfile.txt
Encript a file using openssl. It will ask for a password.
openssl des3 -d -salt -in encryptedfile.txt -out normalfile.txt
Decript a file using openssl. It will ask for a password.
< index >
the rest of loose commands
wall <file
Write a message to all users on your system. Reads from a file and will display a popup window in a GUI. Related commands are write and mesg.
chkconfig --list
(as root) Check out the settings for each service like sshd, apache; whether they are started after a reboot and at wich runlevel.
host -t MX <domain>
Find the server that handles email for a domain.
host <domain>
Gives you the ip-address of a domain and the servers that handle the email.
at -f $HOME/bin/hi now + 5 minutes
Run a script (or command) in five minutes and have the output of the command emailed to you.
echo $(comm -3 list1.txt list2.txt)
Compare two files line by line and output only entries that are not shared by the files (-3). Looks a bit like diff but less verbose output.
mc
start the terminal file browser called midnight commander. (shell bash)
mcedit
Texteditor of midnight commander (mc), which is easy to use.
w3m www.google.com
Browse a link with the text-browser w3m. (is like lynx).
cat index.html | w3m -T text/html
View a html-file rendered as text with the text-browser w3m.
pwd
Print working directory displays the current directory you are in.
echo $TERM
Output the type of terminal you are using.
tty
Prints out the terminal number you are logged into.
chvt 2
Change virtual terminal to number 2 to tty2.
rsync -av --exclude tmp/ /home/directory/ /backup/home/directory
Backup your home directory, excluding any subdirectories named tmp.
mount isofilename.iso /mnt/iso/ -t iso9660 -o ro,loop=/dev/loop0
Mounting an image. Mount point is /mnt/iso
<some command> --help|less
See the help pages (can be different from man pages) for a given command. Example unrar --help|less
echo $MANPATH
Shows the location of the manpages.
info <some command>
info info
Start the info reader and browse through it with n, p and q to quit.
strings $(which <some command>)
If there is no --help page for a command you can use this to display some info on the program. Example: strings $(which unrar)
strings /bin/bash is a good page for finding info about bash. Strings finds readable bits of text from the input.
help
Display all reserved words which can't be used as variables in bash scripts.
info [<some command>]
Info pages contain some excellent reference and tutorial information in hypertext linked format.
type <some command>
Get info on a command, it's location or what is does.
cat /etc/hosts
Shows your hostname, here you can also change it to <new_hostname> then type, (as root):
hostname <new_hostname>
du -sh ~/<somedir>
Display the total disk usage in kb used by a directory.
du -sh ~/<somedir>/*
Display the disk usage in kb used by all the individual files in a directory.
du <dir>| sort -nr
Display the number of bytes from a directory and sort by largest dir.
file <filename>
Determine file type for a file.
cd -
Get back to the last directory you visited.
su
Type your root password to change user to the root user.
su -m
Log in as root and stay in current directory. (actually the standard option for su)
su -
Log in as root and get root's user environment.
sudo pico /etc/rkhunter.conf
Execute commands as root for a short time. First time you have to type root-password.
who or w
(as root?) See who is logged onto your system at the moment and w shows the cpu(CPU) intensive processes run. Long version of the command: users
users
(as root?) See who is logged onto your system. Short version of the command: who
exit
(as root) Logs you off as root. as normal user you exit the shell.
script <textfile>
Start logging every command and textoutput to a textfile. exit to stop logging.
help
Shows a list of commands built into your bash.
ls -a && history |grep 'ls -a'
The && command is your friend for running two consequtive commands. If the first one executes without problems the second is run.
history | awk '{print $2}' | awk 'BEGIN {FS="|"} {print $1}' | sort|uniq -c | sort -rn | head -10
history | cut -c 8-|cut -f1 -d" " | sort|uniq -c|sort -rn | head -n10
These two give you your 10 most used commands from history.
ls ./test || ls ./
The || stands for OR and will execute the second option if the first one fails.
xload &
Run xload (or any program) and put it in the background so you return to your prompt.
bg
After you have pressed <Ctrl>z to suspend a program that you did not start with the & option (to run it in bg), you can go back to it with bg.
fg to run it in foreground.
test -h <some_symbolic_link> && mkdir <lalaah>
Another example of the && command in combination with test: if the symbolic link exists make a directory called lalaah.
lsof | grep /media/dvdrecorder/
which program is using my dvd-recorder so i can't umount?
Per default it lists all files that are open.
For starting things when linux starts up, create an executable script and put it in:
/etc/init.d/boot.local
last -d
listing of last logged in users for non-local logins, Linux stores not only the host name of the remote host but its IP number as well. This option translates the IP number back into a hostname.
!!
means the last command that you have run.
!d
run the last command starting with a d (or any other letter) that you ran in the shell.
!cat:p
Show the last command you ran with cat in it. Do not execute. Does almost the same as <Ctrl>f, but <Ctrl>f puts it on the commandline and lets you browse.
for i in `find . -name "te*.txt"`;\
do echo -e dirname =`dirname $i` ,basename = `basename $i` ,\$i = $i;done;
Example for dirname and basename. dirname $i displays the directory of a file. basename $i outputs only the filename.
top
Display Linux tasks ordered by their CPU usage.
Special keys are:
k
Kill a process, type a process PID <Enter> to kill it.
f
Toggle which columns should be displayed.
r
Renice a process. Sets the priority given to this process,
+20 is lowest, -20 is highest priority.
renice +19 <pid>
Scheduling priority for processes can range from -20 (high) to +20 (low). +19 will make it disrupt your machine as little as possible. (source: rute node 12)
nice +<priority> <pid> is a means of setting the priority relatively to the former setting.
You can see the current nice priority setting with the top command under the column NI.
< index >
more experienced - general
nohup <command> &
Runs a command in the background, appending any output the command may produce to the file nohup.out in your home directory.
nohup has the useful feature that the command will continue to run even after you have logged out.
strings [-n <len>] <filename>
Writes out a binary file, but strips any unreadable characters.
hexdump /bin/cat
od /bin/cat
View the contents of binary files. Use file <file> to see what type of file it is first.
cat /etc/motd
The message that is displayed when you login to a text-terminal.
< index >
more experienced - streams
Example to see the output streams 1 (stdout) and 2 (stderr):
touch existing_file
ls existing_file non-existing_file 2>A
cat A
Now to see the standard output:
ls existing_file non-existing_file 1>A
cat A
So 1>A does the same as >A
ls existing_file non-existing_file 1>A 2>&1
cat A
Now A contains both the error message and the normal output. The >& is called a redirection operator. x >&y tells the shell to write pipe x into pipe y. Redirection is specified from right to left on the command-line. Hence, the above command means to mix stderr into stdout and then to redirect stdout to the file A.
command &> file
Redirect standard output and error output to a file in one command.
< index >
more experienced - tee
echo -e "4\n5\n7\n3\n2\n4" | sort | tee check.file | uniq
Use tee to redirect output of commands in a pipeline to a seperate file.
ls -l | tee savefile
View and save the output of a command
< index >
more experienced - system scripts
/etc/init.d/halt
This is the script that is called while shutting down the system.
/etc/init.d/boot.clock
This script sets up the clock during boot.
/etc/adjtime
This file may be causing your clock to drift since it sets up an amount of drift to correct when it sets the time.
< index >
more experienced - bash
Starting Bash with the --posix command-line option or executing set -o posix makes the bash conform to POSIX 1003.2'
time -p ls /*
Use time -p to measure the amount of time used to execute a command.
< index >
xmms - mp3
Adding .mp3 support for the audio-player xmms:
http://www.suseforums.net/index.php?showtopic=18000&hl=
We're now going to take a little time to configure YaST. The reasons we are doing this will be explained later in the guide. So, for now, just follow these
directions and don't worry about the whys.
Click on the little green bubble (I'm going to refer to this as SUSE from now on). Find System, then click on YaST (a green bubble with a yellow wrench over
top). Type your root password when prompted. At the next screen click on change source of installation. In this screen click add, then ftp,and type in the following:
Server name: ftp.suse.com
Dirctory on server: /pup/suse/i386/10.0/
Click ok. For the above example, I am using the official SUSE download sites. However, there are many international mirrors around the world that you can use in place of the official SUSE sites. To find a mirror close to you, go to the following web-page (using either Konqueror or Firefox):
http://www.novell.com/products/suselinux/downloads/ftp/int_mirrors.html
When you find a mirror close to you, open up the page, and navigate to the directory that contains a file (not file folder) called directory.yast.For example, if I am using the Riken Institute in Japan, the server name would be ftp.riken.jp and the directory on server would be /Linux/suse/suse/i386/10.0/SUSE-Linux10.0-GM-Extra .For now, though, the official SUSE sites will suffice.
Next, click add,then HTTP, and type the following (there are two entries this time, so you'll click ok twice):
Server name: packman.iu-bremen.de
Directory on server: /suse/10.0/
Server name: ftp.gwdg.de
Directory on server: /pub/linux/misc/suser-guru/rpm/10.0/
in yast -> installation sources add ftp:
mirrors.kernel.org/suse/i386/10.0/SUSE-Linux10.0-GM-Extra
in yast software:
search for lame and (almost all things with) mad
< index >
xmms - flac
For flac and mpc you can install rpm's, download and right-click -> actions -> install with YasT
flac: flac-xmms-1.1.2-4.i586.rpm from
http://suse.mirrors.tds.net/pub/opensuse/distribution/SL-10.0-OSS/inst-source/suse/i586/
< index >
xmms - mpc
Installing .mpc (musepack) support for xmms.
For flac and mpc you can install rpm's, download and right-click -> actions -> install with YasT
mpc: libmpcdec-1.2.2-1.pm.2.i586.rpm from
http://packman.links2linux.de/index.php4?action=631&vn=2
# http://packman.links2linux.de/?action=631
&
libmusepack-1.1-0.pm.2.i586.rpm from
http://packman.links2linux.de/index.php4?action=576&vn=2
# http://packman.links2linux.de/?action=576
&
xmms-musepack-1.1.2-0.pm.1.i586.rpm from
http://packman.links2linux.de/?action=577
< index >
xmms - ape
.ape (monkeys audio) support for suse/xmms:
In YasT, install nasm: nasm-0.98.38-47
Then download two packages (mac and xmms-mac) from sourceforge:
http://sourceforge.net/project/showfiles.php?group_id=123827
Unzip mac: (as non-root)
gunzip mac-3.99-u4-b4.tar.gz
tar xvf mac-3.99-u4-b4.tar
cd mac-3.99-u4-b4.tar
./configure
(as root) make install
(log out root: logout)
gunzip xmms-mac-0.3.1.tar.gz
tar xvf xmms-mac-0.3.1.tar
cd xmms-mac-0.3.1
./configure
make
(as root) make install
Notes:
installing xmms-mac-0.3.1 without mac-3.99-u4-b4 gives this error:
configure: error: *** MAC lib headers not installed - please install first ***
installing mac-3.99-u4-b4 without the YasT-package nasm gives an error
saying that nasm is needed.
< index >
xmms - cue
Flac and .ape files (as well as other music formats) can be accompanied by cuesheets which lets your player know how to divide the large file into song=portions when playing. It is not supposed to cut the files (although you can use it for just that) but just to play.
Get the plugin for xmms (XMMS mp3cue v0.94) over here: http://brianvictor.tripod.com/mp3cue.htm
< index >
kde - special keys
Alt (and other) keys for kde:
<Alt><F1>
The K menu shows up.
<Alt><F2>
Run a program.
<Alt><F3>
Go to the top-nemubar of the program you are running.
<Alt><F4>
Close a window/program.
<Alt><F5>
Overview/choose which window/desktop you want to view.
<Alt><Tab>
Scroll through open windows.
<Ctrl><Alt><Esc>
Graphical way of killing an application.
Click the skull on the window you want to kill. <Esc> again to to cancel.
<Ctrl><Alt><F1>
Open tty1
<Ctrl><Alt><F7>
(back to) graphical Xterminal.
<Ctrl><F1>
Switch to first desktop.
<Ctrl><F2>
Switch to second desktop.
<Ctrl><fn>
Switch to desktop n.
<Ctrl><Tab>
Switch desktops.
<Ctrl><Esc>
To start the graphical kde system guard (kpm) where you can kill things.
GUI:
<Ctrl><n>
New graphical window.
<Ctrl><s>
Save.
<Ctrl><c>
Copy selected text.
<Ctrl><x>
Cut selected text.
<Ctrl><v>
Paste contents of notepad.
<Ctrl><q>
Close running program.
<Ctrl><p>
Print page.
<Ctrl><t>
Open new tab-window.
<Ctrl><w>
Close current tab-window.
<Ctrl>Print
Copy a screenshot of the current screen to the clipboard.
Paste it to save it. There is also the command ksnapshot. To copy a screenshot from movies using mplayer, start mplayer with: mplayer -vo x11
Specific keys for various applications:
Konqueror:
<Ctrl>d
Spawn a duplicate window of konqueror.
<Shift><Del>
Delete selected file permanently.
<Ctrl><m>
Hide/show topmenu-bar.
<Ctrl><u>
Clear text-box.
<Ctrl><i>
Show info on selected item.
<Ctrl><o>
Open location.
< index >
kde - konqueror
man:/bash
In the location bar gives you graphical access to the man pages for various commands.
man:/<manpage>
The general command for any manpage.
Hold down <Ctrl> in konqueror to open in a new tab when it gives multiple hits in .gz format.
Logging into konqueror as root:
kdesu konqueror &
Run konqueror as superuser (root) as a separate process in the background (&).
<Ctrl>E
Run a commmand in the current window and view the output.
<Alt><F2> Run a command.
<Ctrl>w
Spawn a new konqueror window.
<Ctrl>q
Close the current konqueror window.
<Ctrl>
Starts 'access keys', which allows you to surf internetpages with your keyboard.
Example-page: http://docs.kde.org/stable/en/kdebase/konqueror/accesskeys.html
j and k
Scroll up and down on an internetpage.
< index >
kde - kate
The graphical texteditor for KDE.
Alternative text editors for kate are (in order of user-friendlyness): pico, joe, vi, ed.
kate <directory/text.txt> &
Open a file with kate.
<Ctrl>d
Comment.
<Ctrl><Shift>d
Uncomment.
<Ctrl><Shift>u
Lowercase-ivy.
<Ctrl>u
Uppercase-ivy.
selecting a block of text and pressing <Tab> will tabulate this text.
<Shift>delete
Go to end of file.
<Ctrl>s
Save file.
<Ctrl>w
Close file.
<Ctrl>q
Close kate.
< index >
kde - kde desktop
Nice themes for KDE:
http://www.kde-look.org/?xcontentmode=14
goto start -> control center -> appearance & themes -> theme manager
To change your KDE theme.
config files for kde-desktop:
~/.kde/share/config/kdesktoprc
If you want mulitple desktops: CommonDesktop=false
~/.kde/share/config/kickerrc
BackgroundTheme= gives you the background for the lower kicker panel aka taskbar.
It's possible your screen runs at a different DPI setting on each distro. You can find out what each has by the CLI with 'xdpyinfo |grep resolution' or system > monitor > X-server. For me: resolution: 83x89 dots per inch.
If you want to keep your dpi consistent, in KDE just edit '/etc/opt/kde3/share/config/kdm/kdmrc' and append what setting you want like so: ServerArgsLocal=-nolisten tcp -dpi 96. Then, logout, ctrl-alt-backspace, and login.
config files for Konsole:
~/.kde/share/config/konsolerc
In here you can set the schema which you want to run:
schema=<username>.schema
You can find the Konsole schema in: ~/.kde/share/apps/konsole
kstart --windowclass konsole --fullscreen --desktop 2 --skiptaskbar --activate konsole
Start a console on the second desktop in fullscreen modus.
Configure your short-keys in start -> control center -> regional & accessibility -> keyboard shortcuts.
See some of this config in: cat ~/.kde/share/config/khotkeysrc
Configure your mouse/window behaviour in start -> control center -> desktop -> window behaviour -> focus: policy: focus follows mouse.
< index >
kde - using your numpad as a mouse
For KDE, you go to Control Center, and open the "Peripherals" section. Select "Mouse", then click on the "Mouse Navigation" tab.
From there, you can enable mouse-by-numpad.
< index >
suse specific - some general things
For setting your hostname: YasT -> network services -> hostname and edit.
Yast -> dns and hostname.
YasT -> network devices -> edit (should show the same info as network services).
/usr/include/asm/errno.h points to /usr/include/asm-generic/errno.h
Which has all the basic error codes.
installation_sources -s
shows your installation sources for YasT. You can also edit them, or you can go to YasT -> installation sources to do it in a graphic menu.
My current setup:
Installation Sources:
[x]* SUSE LINUX Version 10.0 (cd:///;devices=/dev/hdc,/dev/hdd)
[ ] SUSE LINUX Version 10.0 (ftp://mirrors.kernel.org/suse/i386/10.0/SUSE-Linux10.0-GM-Extra)
[ ] SUSE LINUX Version 10.0 (ftp://ftp.nl.uu.net//pub/linux/suse/i386/10.0/SUSE-Linux10.0-GM-Extra/)
[ ] Packman (SuSE LINUX 10.0) (http://packman.iu-bremen.de/suse/10.0/)
[ ] guru (SUSE LINUX 10.0) (http://ftp.gwdg.de//pub/linux/misc/suser-guru/rpm/10.0/)
mirrors.kernel.org/opensuse/distribution/SL-10.0-OSS/inst-source
You uses other sources than software management so:
while starting YOU ( YaST2-Online-Update ) the default server list will be
updated from a "master" server, usually www.suse.de. This list will be stored
under /var/lib/YaST2/you/youservers. User specific servers can be added to
/etc/youservers.
Set YAST2_LOADFTPSERVER to "no", if you do not want to reload the server list.
YaST, system, /etc/sysconfig editor, system, YaST2, online update, YAST2_LOADFTPSERVER.
So basically, you make the change in /etc/youservers, then you set YAST2_LOADFTPSERVER to "yes"
Cleaning out the /tmp dir can be a good thing to prevent your harddisk from geting too full.
Goto: 'start' -> system -> control center -> system -> /etc/sysconfig editor -> system -> cron -> MAX_DIRS_IN_TMP
By default it will be set to 0. Set it to something like 5 days to clean out alot of tempfiles. -> finish to confirm. Do a reboot to see it it worked.
To run cron you can login at <Ctrl><Alt>F2 as root and run ./suse.de-clean-tmp from /etc/cron.daily/
Logout from the root account and get back to x-terminal with <Ctrl><Alt>F7
Things not installed per default:
- Locate is a nice tool to find stuff on your computer. In YasT -> Software management it is listed as 'findutils-locate' or as rpm: findutils-locate-4.2.23-5
< index >
suse specific - open office
OOo &
ooffice &
oofromtemplate &
Three ways of starting Open Office from the commandline.
< index >
suse specific - dual boot xp and suse10 using ntldr
Useful commands (as root):
cat /boot/grub/menu.lst
fdisk -l
as root, edit /boot/grub/menu.lst so that it says:
----------------------------------------------------------------------
# Modified by YaST2. Last modification on Mon Dec 12 18:50:18 CET 2005
color white/blue black/light-gray
default 0
timeout 8
gfxmenu (hd1,3)/boot/message
###Don't change this comment - YaST2 identifier: Original name: linux###
title SUSE LINUX 10.0
root (hd1,3)
kernel /boot/vmlinuz root=/dev/sda4 vga=0x31a selinux=0 resume=/dev/sda3 splash=silent
showopts
initrd /boot/initrd
###Don't change this comment - YaST2 identifier: Original name: failsafe###
title Failsafe -- SUSE LINUX 10.0
root (hd1,3)
kernel /boot/vmlinuz root=/dev/sda4 vga=normal showopts ide=nodma apm=off acpi=off
noresume selinux=0 edd=off 3
initrd /boot/initrd
title Windows XP
map (hd0) (hd1)
map (hd1) (hd0)
unhide (hd1,0)
hide (hd0,0)
rootnoverify (hd1,0)
chainloader +1
makeactive
--------------------------------------------------------------------------
booting from grub through windows:
http://enterprise.linux.com/article.pl?sid=05/02/16/1919205&tid=129&tid=49
as root:
linux:/home/user # dd if=/dev/hdb4 of=bootsect.lnx cbs=512 count=1
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.009368 seconds, 54.7 kB/s
chown user bootsect.lnx
To complete the transplant, place the file you created with dd or Bootpart on the Windows drive as C:\bootsect.lnx.
Now it's time to tell Windows about Linux. Again, there are two ways to go about this.
If you are doing everything manually,
fire up a text editor in Windows and edit the file c:\boot.ini. Add the line c:\bootsect.lnx="Linux" to the end of the file.
cat /etc/grub.conf
To see where grub is located on your hard-disk.
< index >
suse specific - mail
Suse uses nail as it's standard mail client. You can invoke it (for various users) with the commands: nail, mail.
Another email program you can use would be Kmail for kde-users.
cat /etc/aliases
Displays the way aliases are setup for your mail-system (source: rute node 33).
If you want to forward the email for your personal account to an external account do the following in /etc/aliases:
# user-added:
username: external-email@someotherdomainthatdoesnotexist.com
#
mail -s "Hello world from for `hostname`" sysadm@something.com < ~/<file_to_include>
Send an email. -s specifies the subject. sysadm@something.com is to to-address. Include a file in the email with <
You can also send email to users on your system: mail -s "test" <someusername>
Start typing away after the mail command and when you are done finish with a single dot (.) on a blank line to finish the message body.
mail
Check if there is email for you. Type the number in front of the email to read it. q quits the mail program.
cat textfile | mail -s 'This is brand new email' somebody@localhost
Specify the message-body to email from a textfile.
Commands in mail:
delete 1
Delete email number 1.
reply 3
Reply to email number 3.
mail -f
Enables you to read your old emails.
Mail uses the smtp daemon to send email. You can also use it yourself like so:
telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 <some_username>.localhost ESMTP Postfix
HELO localhost
250 <some_username>.localhost
MAIL FROM:<some_username>@localhost
250 Ok
RCPT TO: <some_username>@localhost
250 Ok
DATA
354 End data with <CR><LF>.<CR><LF>
Subject: testing
hello there
.
250 Ok: queued as CBF6A28AEF
QUIT
221 Bye
Connection closed by foreign host.
< index >
suse specific - kmail
Kmail is a mail client that has a graphical interface and can read your email from pop-servers.
It can store it's messages either in mbox format or maildir format (default).
This means that you can browse old mail as text files by going to the dir where kmail stores its mail:
For me: ~/.kde/share/apps/kmail/mail/
Disable notifications for each sent mail from the reciever: settings -> configure kmail -> automatically request message disposition notifications.
You can change the default browser for KDE with which kmail opens links like so: start -> control center -> kde components -> component chooser -> web browser -> in the following browser (click the button on the right) -> internet -> web browser -> firefox.
When browsing through a large folder of email use n to read the next message.
< index >
suse specific - rkhunter
install it in YasT.
rkhunter --checkall
(or -c)(as root)rkhunter preforms a full check of the system, printing out the results of each test to stdout.
< index >
suse specific - ssh
ssh Root login possible & Warning (SSH v1 allowed): according to this link from point 2: http://www.webhostgear.com/24_print.html
Copy and paste this line to edit the file for SSH logins
pico -w /etc/ssh/sshd_config
3. Find the line.
Protocol 2, 1
4. Uncomment it and change it to look like:
Protocol 2
5. Next, find the line
PermitRootLogin yes
6. Uncomment it and make it look like PermitRootLogin no
7. Save the file Ctrl+X then Y then enter
rcsshd status
(as root) Is the ssh daemon running?
ps -ef | grep ssh
(as root) Another way to check whether root is running a ssh-daemon.
/etc/init.d/sshd [start][stop][restarting]
(as root) Manually starting/stopping/restarting the ssh-daemon.
http://susewiki.org/index.php?title=SSH-agent
$ ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/home/joeuser/.ssh/id_dsa): #ENTER
Created directory '/home/joeuser/.ssh'.
Enter passphrase (empty for no passphrase): #USE A PASSPHRASE!!
Enter same passphrase again: #USE A PASSPHRASE AGAIN!!
Your identification has been saved in /home/joeuser/.ssh/id_dsa.
Your public key has been saved in /home/joeuser/.ssh/id_dsa.pub.
The key fingerprint is:
65:74:83:bf:e4:72:16:0b:73:5c:90:9b:bd:70:16:4a joeuser@somehost
how to login locally?
tail -f /var/log/messages
ssh -l loginname <remote_location>
Connect to somewhere using ssh.
ssh -l loginname <remote_location> "ls"
Executing commands on the remote machine. When using public keys no passwords are needed.
< index >
suse specific - chkrootkit
install it in YasT.
< index >
suse specific - apache
httpd2 -V
Check your apache version and config, also shows you where httpd.conf is located: /etc/apache2/httpd.conf
To access the http-server with a graphical interface:
go to start -> system -> control center -> Network services -> HTTP Server.
You can enable/disable the server here.
apache2ctl start
In a konsole (as root): manually starts the apache server; stop for stopping; restart for restarting.
To view the default-page in the root of your server enter this in a browser: http://localhost/
Do you get this error :?
httpd2: could not open document config file /etc/apache2/sysconfig.d/include.conf
Then edit /etc/apache2/httpd.conf with an # so you get this line:
# Include /etc/apache2/sysconfig.d/include.conf
You can do this through: start -> system -> file manager -> file manager - Super user mode, browse to the file and edit it with kate.
Next we need to configure apache to point to the right directories.
The default setting can be found at: (please note that these locations differ per distro, get the locations from the info from: httpd2 -V)
cat /etc/apache2/default-server.conf
So it serves its pages from /srv/www/htdocs
We leave this directory setting as it is and use symlinks to get to the proper directories under home/user.
We want symlinks so we edit this part so it looks like:
DocumentRoot "/srv/www/htdocs"
#
# Configure the DocumentRoot
#
<Directory "/srv/www/htdocs">
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
Options Indexes FollowSymLinks
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs-2.0/mod/core.html#options
# for more information.
# Options None
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
AllowOverride None
# Controls who can get stuff from this server.
Order allow,deny
Allow from all
</Directory>
Next we make the symlinks to link to our homedir where the pages are put.
Go to your homedir and drag the pages-dir to /srv/www/htdocs and choose link.
You might need to set the permissions to user: wwwrun , group: users.
The serverlog can be found at:
less /var/log/apache2/error_log
less /var/log/apache2/access_log
The main server setup-script can be found at: cat /etc/sysconfig/apache2
/etc/init.d/apache2 status
ps aux| grep httpd
To see whether apache is running.
< index >
suse specific - php
go to Yast -> online update and do an update.
In yast -> network services -> HTTP Server. -> modules -> edit, you can see whether php4 and include is enabled or not.
mod_php4-apache2 is an old package no longer continued.
Installed packages are: (viewable by: rpm -qa | grep php)
php4-devel-4.4.0-6
apache2-mod_php4-4.4.0-6.6
php4-mysql-4.4.0-6
php4-session-4.4.0-6
php4-4.4.0-6.6
Test php by placing test.php in the root which contains:
<?php; echo "This is a test"; ?>
and browse to it with your browser to see if php works.
errors and such:
Check your permissions and users:groups if you cannot browse to php files.
Clear your browser cache since this creates false files not updated by the system if you get errors.
change some comments in your /etc/php.ini so it says this:
session.save_path = /tmp
;session.save_path = /var/lib/php
"Fatal error: Call to undefined function: session_start()"
means you are missing: php4-session-4.4.0-6
enabling browscap:
Save a browscap from somewhere such as: http://browsers.garykeith.com/downloads.asp
Store browscap.ini in this location: /usr/local/lib/browscap.ini
Adjust your php.ini so it knows where browscap is: in php.ini add this line (or uncomment):
browscap = /usr/local/lib/browscap.ini
//This doesn't really work for me yet ....
suse specific - php - running mod_rewrite{
You might need this for running gallery (http://gallery.sourceforge.net/).
To enable it go to: start -> system -> yast -> system -> etc/sysconfig editor -> network -> www -> apache2 -> APACHE_MODULES
in the setting box (maybe after php4) put in (first a space after php4): rewrite and click finish.
So now it looks like:
access actions alias auth autoindex cgi dir include log_config mime negotiation setenvif status userdir asis imap php4 rewrite
< index >
suse specific - mysql
http://togaware.com/linux/survivor/MySQL_Root.shtml
http://www.topology.org/linux/mysql.html
Installing mysql:
Go to yast and search for mysql and install (them all?)
Typing man mysqld should give you some sort of manual too and show you that mysql is installed.
rpm -qa | grep mysql
Gives you the packages i installed:
mysql-shared-4.1.13-3
perl-DBD-mysql-3.0002-2
mysql-4.1.13-3
mysql-client-4.1.13-3
php4-mysql-4.4.0-6
Running mysql:
goto start -> yast -> system -> System Services (runlevel) -> mysql -> enable -> ok -> finish.
Or in:/etc/init.d you can type: mysql start and mysql stop so:
linux:/home/user # /etc/init.d/mysql stop
Shutting down service MySQL done
linux:/home/user # /etc/init.d/mysql start
Starting service MySQL
Is mysql running :?
Type this to make sure: netstat -vat |grep sql
The reply should be: tcp 0 0 *:mysql *:* LISTEN
Or mysqlshow: shows the databases currently in use, probably the only you have now is test.
ps -ef | grep apache
Which user is running apache?
Set the root password:
To do so, (start the server), then issue the following commands (as root):
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h linux.site password 'new-password'
So for instance if we want to use the password driebloteapen.
/usr/bin/mysqladmin -u root password driebloteapen
/usr/bin/mysqladmin -u root -h linux.site password driebloteapen
To connect to the MySQL database, do something like this: mysql -p
Exit to exit.
Commands end with ;
mysql -p < 060422backup.sql
Import a mysql-backup into your mysql.
mysqldump -p <databasename> > <outputfile>
Export a database from mysql to a local file (textfile).
mysql -p -e"SHOW DATABASES;"
Run a mysql query from the commandline.
Benchmarking your mysql:
http://www.novell.com/coolsolutions/tip/16076.html
mysql-bench-4.1.13-3.i586.rpm
This yast package is not on the DVD so we get it from http://mirrors.kernel.org/opensuse/distribution/SL-10.0-OSS/inst-source/suse/
Go to start -> Yast -> software -> installation sources; and add the following by 'add', HTTP and as server name use
mirrors.kernel.org/opensuse/distribution/SL-10.0-OSS/inst-source
Turn Refresh and enable on for this source and goto start -> YasT -> software management to find mysql-bench
I could not test the MySQL daemon with the benchmarks in the 'sql-bench' directory:
cd /usr/share/sql-bench ; perl run-all-tests.
Instead: su to root, and type: perl run-all-tests --server=mysql --cmp=mysql,pg,solid --user=root --password=<yourownpassword> --log
cd ~mysql
Change directory to your mysql directory (/var/lib/mysql)
cat /etc/my.cnf
The configuration file for mysql.
< index >
suse specific - skype
Get skype from here: http://www.skype.com
rpm -Uvh skype-<NUMBER>-suse.i586.rpm
SuSEconfig
ldconfig
(as root) This installs skype. Execute these commands in the dir where you saved the rpm.
< index >
other good links to explore:
http://www.unixguide.net/linux/linuxshortcuts.shtml
Linux Newbie Administrator Guide
by Stan and Peter Klimas
http://rute.2038bug.com/index.html.gz
Rute User's Tutorial and Exposition (Version 1.0.0)
by Paul Sheer
----------------------------------------------------------------------
'Just because you're paranoid, doesn't mean they're not after you!' ;)
----------------------------------------------------------------------