Mai 10
Situation
You want to operate on a linux, unix or mac system some files in a bash script and do some file operations like modifying the text content or moving the files to another location depending on the filename, content or another mathinc pattern.
Problem
Wondered why you have problems with windows files in a network when looping over them? Linux takes whitespaces in file name as line separator so that echoing the file will cause a separate line and so you cannot operate on this file.
Solution
I guess it is called Instruction Field Separator or something. You have to set this only to line break and not to whitespaces. First save the old variable content, then set the new content so that the bash do not takes whitespaces as line separators
1
2
3
4
5
6
7
8
9
| OLDIFS=$IFS
IFS=$(echo -en "\n\b")
for FILE in $(find . -name '*.txt)
do
echo $FILE
done
$IFS = $OLDIFS |
Tagged with: loop filenames • shellscript • spaces in linux filenames
Mrz 25
Sometimes you have to revert changes in svn from lets say a commit you made some days ago. Since then more commits where done and this can be a hard job to undo your changes. So how to undo the changes e.g. from your change in revision 120 when the actual revision is 140?
Go to your trunk workdir and use the following:
1
| mvn merge -r 120:119 http://my.svn.repo/trunk/ |
The changes you made will be reverted. If there are some conflicts they will be shown.
Maybe it is a good idea to make a dry run via:
1
| mvn merge --dry-run -r 120:119 http://my.svn.repo/trunk/ |
So no changes are really made but only shows them.
Of course you can use this for reverting merges.
Tagged with: merge • reverse change • svn • undo merge
Feb 08
Imagine you have a pdf-file you want to make ocr-recognition. Take a scenario where you want to automatically let your linux pc do the job, e.g. in a folder.
I choose tesseract-ocr as ocr-programm. Easy to install and use and ok for my use. Unfortunately it takes only tif as input file type so that we have to convert the pdf to tif first.
To create a tif file with Ghostscript from pdf:
1
| gs -q -r300 -dBATCH -dNOPAUSE -sDEVICE=tiff24nc -sOutputFile=Dokument2.tif Dokument1.pdf |
Now start OCR-recognition with tesseract-ocr (maybe you have to install it).
1
| tesseract Dokument2.tif doc.txt -l deu |
-l deu means “Deutsch” for German language recognition.
-r300 means 300 DPI
Now it is easy to create a script which will automatically check a folder for new files and start ocr etc.
E.g. you can create a shell-script and start this shell script via cronjob. Maybe I will write an example here later.
Tagged with: Linux • ocr • pdf • tif
Jan 16
If you want to printout or save info from manpages simply type:
1
| man -t man | ps2pdf - > man.pdf |
Tagged with: Linux • man • pdf • Shell
Dez 20
If you want to download a file with curl type the following code into bash:
1
| curl -O http://www.url.de/file.html |
-O means to output to a file and not to stdout, normally the screen.
What if you need to resume a download of a big file?
1
| curl -C - -O http://www.url.de/file.html |
With -C – you continue downloading the current file. If is important to write -C -. Don’t forget the -
You can also set a referrer and the user agent.
1
| curl -C - -O -A http://www.url.de -e "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; de; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6" http://www.url.de/file.html |
With -A you can set the referrer and -e sets the user agent.
Pretty helpful.
Tagged with: Bash • curl • download • Linux • mac
Dez 14
After unpacking the Kernel Archive to “/usr/src” use the following:
1
2
3
4
| make dep
make clean
make bzImagemake modules
make modules_install |
Tagged with: compile • kernel compilation • Linux
Dez 08
To detach from an actual session press
Reattach with
To list the actual detached sessions:
You will see something like:
1
2
| 4840.pts-3.localhost (Detached)
4628.pts-1.localhost (Detached) |
Reattach it with:
1
| screen -r 4840.pts-3.localhost |
To share a session with another user you have to ensure some steps.
Set the screen binary setuid root:
1
| chmod +s /usr/bin/screen |
Login as root and start a screen session and name it:
Enable multi-user access function by pressing Ctrl+a then :multiuser on (to type “:”, press “shift+;”)
Grant permissions to the second user by pressing Ctrl+a: acladd username, where username is the Unix user for the second user.
1
| screen -x root/sessionName |
Activate logging with:
A file in your home directory will be created. Something like $HOME/screenlog.1
Type exit to exit screen.
Dez 02
How to send an email with telnet via smtp for testing purposes? Here is an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| telnet smtp.me.com 25
220 smtp.web.de ESMTP Web.de V4.91#2 Sun, 24 Nov 2009 16:51:20 +0100
helo yourcomputername
250 smtp.web.de Hello yourcomputername [219.99.999.999]
mail from: account@web.de
250 account@web.de is syntactically correct
rcpt to: empfaenger@provider.tld
250 empfaenger@provider.tld verified
data
354 Enter message, ending with . on a line by itself
To: anton@tirol.at;
From: hugo@schlaumeier.de
Subject: Jodelei
Servus!
Greetz,
Peterli
.
250 OK id=18Z6Uc-0001wm-00
Quit
221 smtp.web.de closing connection |
To send mail with auth:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
| telnet smtp.me.com 25
220 smtp.web.de ESMTP Web.de V4.91#2 Sun, 24 Nov 2009 16:51:20 +0100
helo yourcomputername
250 smtp.web.de Hello yourcomputername [219.99.999.999]
auth login
334 VXNlcm5hbWU6
<base64 encoded username>
<repeated base64 encoded username>
<base64 encoded password>
<repeated base64 encoded password>
235 Authentication successful
mail from: account@web.de
250 account@web.de is syntactically correct
rcpt to: empfaenger@provider.tld
250 empfaenger@provider.tld verified
data
354 Enter message, ending with . on a line by itself
Subject: Jodelei
Servus!
Greetz,
Peterli
.
250 OK id=18Z6Uc-0001wm-00
Quit
221 smtp.web.de closing connection |
To base64 encode your username or pass, use another linux terminal and type:
1
2
| printf username | base64
printf password | base64 |
If you don’t have base64 on your system try mimencode instead.
Tagged with: email • Linux • send email • send mail via telnet • smtp • telnet
Dez 02
To scroll in screen-mode of linux type the following:
Strg+a then AltGr+8
Now you can scroll up and down. This is called the copy-mode.
Source: http://blog.pregos.info/2009/02/26/scrollen-im-screen/
Tagged with: Bash • Linux • screen • scroll
Nov 30
Some useful linux find-commands:
Find files with file size between 10 and 20 MB
1
| find -size +10M -size -20M |
Find files not belonging to user root
Find files greater than 10 MB and list them with ls-lh.
1
| find -size +10M -exec ls -lh {} \; |
Find files with .jpg ending not case sensitive and create a tar-ball.
1
| find -iname '*.jpg' -exec tar -rf bilder.tar {} \; |
Find files with .mp3 ending not case sensitive and create a playlist.m3u file
1
| find -iname '*.mp3' -fprint playlist.m3u |
Find files with SUID bit set.
1
| find / -type f -perm +6000 -exec ls -l {} \; > suid.list |