Linux most used commands



Fetch priority and nice values of a process:
cat /proc/PID/stat | awk '{print "priority " $18 " nice " $19}'

How to find the size of sub-directories in current working directories
du -h --max-depth=1

Find the files whose size if greater than 100MB
find . -type f -size +100M

How do I find the kernel version ?
uname -a for all information regarding the kernel version,
uname -r for the exact kernel version

How to run cscope in linux ?
find . -name "*.c" -o -name "*.cpp" -o -name "*.h" -o -name "*.hpp" > cscope.files
cscope -q -R -b -i cscope.files
cscope -d

How to view max pid's supported in linux os ?
 cat /proc/sys/kernel/pid_max
  
Replace a String in Multiple Files in Linux Using Grep and Sed
grep -rl <matchstring> somedir/ | xargs sed -i 's/string1/string2/g'

tcpdump commands

1. read pcap format
tcpdump -r 0001.pcap

2. Capture packet on the interface and redirect them to.pcap file.
sudo tcpdump -w 0001.pcap -i lo

3. Capture packet on the interface.
sudo tcpdump -i lo -v

How to set password in linux:
 passwd

How to delete password in linux:
 passwd -d <username>

To be added to vimrc
set cindent
set expandtab
set shiftwidth=4
set tabstop=8
set hlsearch

set expandtab 

Command for highlight trailing whitespaces  :
<esc>:/\s\+$
 
Command for removing whitespaces : 
sed -i 's/\s*$//'  <filename>
<esc>: set list

<esc>: set no list

Linux Vimdiff important keyboard shortcuts: 
1. Shift to next windows from current window
press 'ctrl + w + w' [press ctrl button and double press 'w']

2. diffput: puts changes under the cursor into the other file
                        making them identical (thus removing the diff).
press 'd +p' 
3. diffget: (o => obtain). The change under the cursor is replaced
                        by the content of the other file making them identical.
press 'd + o' 


Edit .bashrc
 PS1='\e[1;36mNeelkanth$\e[m'

red=`tput setaf 1`
green=`tput setaf 3`

echo "${green}                    ────────────────────────────────"
echo "                    ───────────────██████████───────"
echo "                    ──────────────████████████──────"
echo "                    ──────────────██────────██──────"
echo "                    ──────────────██▄▄▄▄▄▄▄▄▄█──────"
echo "                    ──────────────██▀███─███▀█──────"
echo "                    █─────────────▀█────────█▀──────"
echo "                    ██──────────────────█───────────"
echo "${red}  NEELKANTH${green}         ─█──────────────██──────────────"
echo "                    █▄────────────████─██──████"
echo "                    ─▄███████████████──██──██████ ──"
echo "                    ────█████████████──██──█████████"
echo "                    ─────────────████──██─█████──███"
echo "                    ──────────────███──██─█████──███"
echo "                    ──────────────███─────█████████"
echo "                    ──────────────██─────████████▀"
echo "                    ────────────────██████████"
echo "                    ────────────────██████████"
echo "                    ─────────────────████████"
echo "                    ──────────────────██████████▄▄"
echo "                    ────────────────────█████████▀"
echo "                    ─────────────────────████──███"
echo "                    ────────────────────▄████▄──██"
echo "                    ────────────────────██████───▀"

echo "                    ────────────────────▀▄▄▄▄▀"


*********************************************************
Date commands
*********************************************************
Set date from the command line
date +%Y%m%d -s "20120418"

Set time from the command line
date +%T -s "11:14:00"

Set time and date from the command line
date -s "19 APR 2012 11:14:00"

Date command with nanosecond, microsecond and millisecond precision
date +%x_%H:%M:%S:%N
Tuesday 27 February 2018_17:48:09:890111701

date +%x_%H:%M:%S:%6N
Tuesday 27 February 2018_17:48:13:809079

date +%x_%H:%M:%S:%3N


Tuesday 27 February 2018_17:48:16:401

Command to use python 3.5 instead of default python2.7
python3 -V
python3 test.py

Print lines of text before and after "grep" for a string
[root@localhost ~]# grep "neelkanth" -A 2 test
neelkanth
f
asdf
[root@localhost ~]# grep "neelkanth" -B 2 test
asdf
asd
neelkanth 


How to insert a text in the beginning of each line in VI editor
 :%s/^/foo: /

How to insert a text in the end of each line in VI editor
%s/$/  next_script_in_line/g
10,22s/$/  next_script_in_line/g




Single line bash shell command to create 100 files in 1 go.
touch TEST_CASE{1..100}.txt

Create Softlink  in Linux
Go to the directory where softlink needs to be created.
ln -s   <actual file/directory location>   <softlink file name>
##################################################################
Steps to install virtual box guest additions
##################################################################
1. sudo apt-get upgrade
2. sudo apt-get install virtualbox-guest-additions-iso
3. Go to display setting, under video capture, check enable "video capture"
4. sudo apt-get install virtualbox-guest-utils
5. restart virtual machine.
##################################################################
How to use strace tool to view flow of system calls used when glibc/libc library 
functions are executed.
##################################################################
Neelkanth_221$ cat wrapper.c
c program:
#include <stdio.h>
#include <unistd.h>
int main(){
    write(0, "Hello, Kernel!\n", 15);
    printf("Hello, World!\n");
    return 0;
}
command: strace ./a.out
system calls trace
alloc_stress.c  call_back_function.c  crypt.c          linked_list.c        save_pointer_address.c  test123.c  val               wrapper.c
a.out           crypt                 data_structures  malloc_stats_expt.c  socket_vs_queue_expts   test.c     validate_crypt.c
Neelkanth_221$ vi wrapper.c
Neelkanth_221$ strace ./a.out
execve("./a.out", ["./a.out"], [/* 23 vars */]) = 0
brk(0)                                  = 0xdeb000
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa1f40f0000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=91462, ...}) = 0
mmap(NULL, 91462, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fa1f40d9000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P \2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1853216, ...}) = 0
mmap(NULL, 3961536, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fa1f3b08000
mprotect(0x7fa1f3cc6000, 2093056, PROT_NONE) = 0
mmap(0x7fa1f3ec5000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1bd000) = 0x7fa1f3ec5000
mmap(0x7fa1f3ecb000, 17088, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fa1f3ecb000
close(3)                                = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa1f40d8000
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa1f40d6000
arch_prctl(ARCH_SET_FS, 0x7fa1f40d6740) = 0
mprotect(0x7fa1f3ec5000, 16384, PROT_READ) = 0
mprotect(0x600000, 4096, PROT_READ)     = 0
mprotect(0x7fa1f40f2000, 4096, PROT_READ) = 0
munmap(0x7fa1f40d9000, 91462)           = 0
write(0, "Hello, Kernel!\n", 15Hello, Kernel!
)        = 15
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 3), ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa1f40ef000
write(1, "Hello, World!\n", 14Hello, World!
)         = 14
exit_group(0)                           = ?
+++ exited with 0 +++

#################################################################very important debugging approach in Linux for tracking library functions used inside another library function or in an applicationprogram
#################################################################for e.g. fopen and strdup uses malloc inside itself. How do we find that in debugging ??
1. in linux shell,  set 
    LD_DEBUG=all
2. Now, run the binary. we will get a debug print with symbols corresponding to library          functions. 
3. get the flow of symbols.
4. Now, reset the environment variable.
LD_DEBUG=

#################################################################
'vimrc' mapping between Keyboard Keys and Functions 
#################################################################
function FileHeading()
let s:line=line(".")
call setline(s:line,"/*********************************************")
call append(s:line,"* Description - ")
call append(s:line+1,"* Author - Neelkanth Reddy")
call append(s:line+2,"* Date - ".strftime("%b %d %Y"))
call append(s:line+3,"* *******************************************/")
unlet s:line
endfunction

""
" in text file give command :call FileHanding
""
" autocmd BufReadPost * call FileHeading()

""
"" press <F3> to print system time in text file.
""
nmap <F3> "=strftime('%c')<C-M>p

"" 
"" Please ESC and <F4> for executing the function FileHeading
"" This would print the file Header in the file.
""
nmap <F4> <Esc>mz:execute FileHeading()<CR>`zjA

How to use Ctags:

go to a folder containing c files.
give --> ctags -R *

1. do 'ctrl + ]' on the function to go to the function definition, which could be in the same file or another file. 
2. do 'ctrl+t' to come back to the original file.