Linux commandline tips

From PvdMWiki
Jump to: navigation, search

Bash tips

^r      Reverse search the command history
^a	Return to the start of the command you're typing
^e	Go to the end of the command you're typing
^u	Cut everything before the cursor to a special clipboard
^k	Cut everything after the cursor to a special clipboard
^y	Paste from the special clipboard that Ctrl + u and Ctrl + k save their data to
^t	Swap the two characters before the cursor (you can actually use this to transport a character from the left to the right, try it!)
^w	Delete the word / argument left of the cursor
^l	Clear the screen

history    lists command history
!61        executes command on line 61

ls -l /etc/passwd
stat !$             no need to repeat argument on second line

man -k <keyword>     searches in all man pages for <keyword>

ls *.mp3 | xargs rm
ls *.mp3 | xargs -i cp {} /home/bla
ls *.mp3 -d  -> stays in current dir and doesn't enter mp3/
  • start a restricted shell (no cd, etc) to test programs
# bash -r
  • start shell with options
# bash -O [modification]
autocd -> use the argument as cd command
cd-spell -> corrects dir names
dirspell -> corrects dir names in file completion
no-caseglob -> case insensitive

to run commands:

# command1 && command2

logical AND :: run command1, if ok then run command2

# command1 || command2

logical OR :: run command2 only if command1 fails

cp

cp -rv 

gives feedback

cp -p

preserves original date/time

dd

use blocks of 64k and report every 10Mb:

dd if=/dev/hda | buffer -s 64k -S 10m > image

or, from other console:

kill -SIGUSR1 $(pidof dd)

df

df -type=ext3,ReiserFS
df -H human readable

find

find all files in your homedir modified or created today

find ~ -type f -mtime 0

case insentive search

find . -iname "*mp3"

find files and directories in yor homedir not created by you

find ~ ! -user ${USER}

and fix this

find ~ ! -user ${USER} -exec sudo chown ${USER}:"{}" \;

free

free -m -> megabytes
free -t -> totals
free -s[seconds] -> update interval

watch the line with +/- buffers: free indicates memory that can be freed instantaniously used is all memory being used

grep

grep -A2 bla greps 2 lines after
grep -B3 bla greps 3 lines before

history

# history -d [postition] -> remove specific command
# history -c -> clear entire history

iotop

monitors io proces

iotop -o makes output more readable by showing only processes causing io right now 
iotop -d10 refresh interval
iotop -o -b -d10 -n30 > io.txt batch mode: write io status 30 times to file at 10s interval

iperf

iperf -s -w128k run server for performance benchmark
iperf -c <serveraddress> -w128k -t30 -r run client
iperf -c <serveraddress> -w128k -t30 -d run client in two directions, full duplex

kill

kill -l shows instructions

 1  SIGHUP   restart immediately after terminating, or triggers reconfiguration of background service
 2  SIGINT   Ctrl+c from the keyboard, terminate!
 3  SIGQUIT  Ctrl+\ from the keyboard, terminate with coredump
 9  SIGKILL  force terminate, extreme signal that can't ignored
11  SIGSEGV  program attempted an invalid memory reference, terminate with coredump
15  SIGTERM  request to terminate and cleanup
19  SIGSTOP  interrupts the process until you enter SIGCONT to continue
kill -19 9102 10234  stops several processes
killall -19 ssh      sends all ssh connections to sleep
killall -19 -i ssh   interactive mode (recommended)

lsof

lsof list open files
lsof -i :22 list all ssh connections (same :ssh)
lsof -i@10.0.0.1 list all connections from 10.0.0.1
lsof -i@amsterdam.nl list all connections from domain
lsof -u username list all open files from username
lsof -c bash list open files related to bash
lsof -c /log/ list all open files from all processes containing log

lspci

lspci -v 
lspci -vv
lspci -vvv
lspci -nn -> show names & numerical 
lspci -k -> show kernel modules associated with the device

more or less

+<number> number of line from which to start displaying
z <number>  number of lines to jump forward
y <number> number of lines to jump back
-I ignore case when searching
-V underline tabs, line endings
/<ctrl-k> highlights all ocurrences of search

pgrep

pgrep ssh lists all ssh processes
pgrep -l ssh shows names
pgrep -f  shows full commandline
pgrep -u show user

ping

ping 0 -> pings localhost
ping c 5 nu.nl -> pings 5 times
ping -f host -> floods the host
ping -a IP -> gives audible beep Note: It can give beep only from terminal number 1 through 7 and gnome-terminal ( It will not work in console ).
ping -q ip -> show only summary
ping -s 100 -> change packet size
Pressing CTRL+| (Control key followed by pipe symbol) for the shows the summary in between, and continues with it packet sending and receiving process.
ping hop1 hop2 hop3 .. hopN destination
ping -R 192.168.1.63 -> Record and print route of how ECHO_REQUEST sent and ECHO_REPLY received

pkill

understands the same options as pgrep

pkill -19 ssh
pkill -19 -u joop stop joop's processes

ps

show how long a process is running

ps -o pid,etime,cmd `pidof amarok`

show memory usage per process

ps -e -orss=,args= | sort -b -k1,1n | pr -TW$COLUMNS

pstree

pstree -a shows processes tree structure with program's parameters
pstree -h highlights own process 
pstree -H <id> highlights process id
pstree -u shows user

stat

shows status of file

stat /etc/passwd   
stat -c "%x %n" /etc/passwd      formatted to show time

swapon

swapon -s see size and usage

tail or head

--retry keep monitoring the file 
-s seconds how often monitoring retry
-pid <pid> stops tail from running if PID stops running

tar

tar zxf <file> -> extracts file

top

PID     programma id
USER    gebruiker
PR      prioriteit van de taak
NI      nice value
VIRT    virtuele geheugen gebruik
RES     fysieke geheugen gebruik
SHR     deel v/h gebeugen dat mogelijk met andere taken wordt gedeeld
%CPU    percentage processorgebruik
%MEM    percentage geheugengebruik
TIME+   de tijd dat de taak op de cpu heeft gedraaid
COMMAND de naam van de taak

keys: z: enable color mode. x: show sorted column in different color < >: change sorted column u [user]: show only users processes d: delay in sec

show only user user

#top -u user

zombies

ps aux | grep defunct

AutoYast

create autoyast file through icon in Yast2->autoyast

www.suse.de/~ug (Uwe Gansert)

Midnight Commander (MC)

The standard colorpallette is incredibly hard to read in certain terminals, like yakuake.

For readable colors (mostly black and green), enter in your ~/.mc/ini file the following line:

[Colors]
base_color=lightgray,green:normal=green,default:selected=white,gray:marked=yellow,default:markselect=yellow,gray:directory=blue,default:executable=brightgreen,default:link=cyan,default:device=brightmagenta,default:special=lightgray,default:errors=red,default:reverse=green,default:gauge=green,default:input=white,gray:dnormal=green,gray:dfocus=brightgreen,gray:dhotnormal=cyan,gray:dhotfocus=brightcyan,gray:menu=green,default:menuhot=cyan,default:menusel=green,gray:menuhotsel=cyan,default:helpnormal=cyan,default:editnormal=green,default:editbold=blue,default:editmarked=gray,blue:stalelink=red,default

tune kernelparameters

stored in /proc/sys/ gone after reboot

sysctl is a service that reads sysctl.conf during boot and read parameters from there -> makes it permanent

# ulimit -a

givesinfo about system resources limitations