Linux notes

From raju


head tail usage

head -n -5 FILE    print everything but the last 5 lines
head -n +5 FILE    print only the first 5 lines
tail -n -5 FILE    print only the last 5 lines
tail -n +5 FILE    print from 5th line onwards

print last but one line

    cat FILE | tail -n -2 | head -n +1


redirect standard output and error

To redirect standard output and error into a file

    foo > log.txt 2>&1

useful HOWTOs and tutorials

sound related

Increase the Sound Level in a Video -

ffmpeg -i video-in.avi -vol 1536 -vcodec copy video-out.avi

Every 256 in vol increases sound by 100%. So in the above example, the sound will be increased by 600%.

wget tips

using wget to download a file via php script

wget -O NrrwRgn-0.32.vmb -q

Listing options in this order is more easy to remember (get the link, store it here and be quiet). Compare this to

wget -O csv-0.30.vmb -q

where the thought process is not as smooth.


execute external commands

To execute an external command in gdb

(gdb) help shell
Execute the rest of the line as a shell command.
With no arguments, run an inferior shell.
(gdb) shell date
Sun Jan 18 15:04:19 EST 2015

enter tab character in the shell


will insert a tab character on the command-line.

find usage articles

Articles on using find

List directories by size

To sort the directories by disk usage

    $ du -ah --max-depth=1 | sort -hr | head -n 10
    1.3G    .
    143M    ./agg_5
    143M    ./agg_4
    143M    ./agg_3
    143M    ./agg_2
    143M    ./agg_1
    122M    ./run_3
    121M    ./run_5
    121M    ./run_4
    121M    ./run_2


    rajulocal@hogwarts ~ % du -a -h --max-depth=1 | sort -h | tail -n 10
    225M    ./compile_static_gdb_debian_way
    228M    ./.local
    370M    ./songs
    501M    ./youtube_videos
    849M    ./x
    944M    ./.cache
    1.2G    ./work
    1.7G    ./.adobe
    8.0G    ./software
    15G     .

tips available on the net

add a string to the end of each line

We can use awk to add a given string to the end of each line in a file. For example,

% cat ~/x/junk21.txt
% cat ~/x/junk21.txt| awk '{print $0 " str .=$()-"}'
a str .=$()-
an str .=$()-
the str .=$()-
they str .=$()-
their str .=$()-

search the symbols

nm DebuggableExecutable -C

check if the DISPLAY variable is set correctly

Use "xset q" and check the error code. For example if it is working

% xset q
<output suppressed>
% echo $?

If it is not working

% xset q
xset:  unable to open display ""
% echo $?

What type of OS am I running?

Use OSTYPE environment variable to check the type of operating system.

% printenv | grep OS

or simply

% printenv OSTYPE   

top usage

  • Run in batch mode
 top -c -u [username] -b -n 1

-c -u [username] is optional -b is for batch mode -n 1 is to stop after one iteration

  • track the memory usage of a process over time
 top -b -p [pid] < /dev/null >& top.out &

Look at the VIRT field.

updatedb and locate

  • To generate the database
 updatedb -U <dir1> -l 0 -o <dir1.db>
 updatedb -U <dir2> -l 0 -o <dir2.db>
  • To search the database
 locate -d <dir1.db> -d <dir2.db> -e -i <expr>

Open a pdf at a specific page

To open at the 10th page of a pdf. Note that this is not same as opening it at page 10 by label.

    acroread /a page=10 file.pdf
    evince -i 10 file.pdf
    okular -p 10 file.pdf

To open at page 10 by label.

    evince -p 10 file.pdf

cat show filename

Use "tail -n +1" to show the filename when displaying its contents. For example

    tail -n +1 file1.txt file2.txt file3.txt


    ==> file1.txt <==
    <contents of file1.txt>
    ==> file2.txt <==
    <contents of file2.txt>
    ==> file3.txt <==
    <contents of file3.txt>

print the type of file system

    df -T /path/to/dir

This can tell if the directory is a local directory (ex:- ext4 filesytem) or a remote mount (ex:- nfs).

create a zip file from list of files

    zip bar.txt baz.txt

To unzip


Note that this will not delete the

external links

commonly used date time stamps

time stamp for log files

    $ foo > bar_`date +'%Y%m%d_%H%M%S'`

stores the output of foo into a file called bar_YYYYmmDDHHMMSS