Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Linux Ask!

Jan 012010
 

How to perform checksum on a file?

Answer:

You can use the md5sum or shasum commands.

Example

#md5sum test.txt

f1d41356dcabb8d8acefed069b22b0da  test.txt
#shasum test.txt

809970aec0109c5464dd36a3a9a5a9e79f838313  test.txt

md5sum is faster if you perform the command on a very large file, but shasum is more secure since the chance of two different files have the same checksum is smaller.

References:

Jan 012010
 

How to print out a newline character using the echo command?

Answer:

If you try

echo "foo\nbar"

It will print out

foo\nbar

But if you want the two words separate by an actual newline, you can use

echo -e "foo\nbar"

Output would be

foo
bar

You can combine the "-e" flag with other control characters such as "\t", "\r", "\v"... as well.

Jan 012010
 

How to reverse print a file?

Answer:

Normally, you can print a file to standard out by using

cat test.txt

It will show something like:

a
b
c

However, if you want to start from the last line, you can use tac (actually it is reverse of the cat command)

tac test.txt

It will give:

c
b
a

Dec 312009
 

Run tasks in parallel with xargs

Answer:

xargs provided a very simple to parallel tasks, e.g. setting P=3 means run 3 processes in parallel

seq 1 10 | xargs -n 1 -P 3 echo

Result:

1
2
3
4
5
7
6
9
8
10

Try to run the command several times, you might notice that number's order might be changed.

Why? Since they are running in parallel and some numbers finished first and some later!