Referencia: Commandlinefu
head -$(($RANDOM % $(wc -l < file.txt) +1 )) file.txt | tail -1
tr -dc a-z1-4 </dev/urandom|tr 1-2 ' \n'|\ awk 'length==0 || length>50'|tr 3-4 ' '|sed 's/^ *//'| cat -s | fmt|head -40
mplayer "$(find . -maxdepth 2 -mindepth 2 -type d | grep -v '^.$' | sort -R | head -n1)"/*
printf "\U$(printf '%x' $((RANDOM%79+128512)) )"
feh –bg-center $(ls | shuf -n 1)
feh –bg-center $(ls -U1 |sort -R |head -1)
for USER in $(cut -d ":" -f1 </etc/passwd); do crontab -u ${USER} -l 1>/dev/null 2>&1; if [ ! ${?} -ne 0 ]; then echo -en "--- crontab for ${USER} ---\n$(crontab -u ${USER} -l)\n"; fi; done for USER in $(ls /var/spool/cron); do echo "=== crontab for $USER ==="; echo $USER; done
for USER in /var/spool/cron/\*; do echo “— crontab for $USER ---" cat "$USER” done
history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
for j in $(for i in *; do grep -A1 . $i|grep -q "^--$" && echo $i;done); do cp $j $j.bak -v;cat -s $j.bak > $j; done
cat -n FILE grep -n '^' FILE
for i in *; do
grep -A1 . $i|grep -q "^--$" && echo $i;
done
Does also work on part archives.
unrar p -inul foo.rar|mplayer -
while true; do eject && sleep $(expr $RANDOM % 5) && eject -t; done;
find /path/to/dir -type f -print0 | xargs -0 rm
season=1
for file in $(ls) ; do
dir=$(echo $file | sed 's/.*S0$season\(E[0-9]\{2\}\).*/\1/')
mkdir $dir
mv $file $dir
done
echo 'foo' | tee >(wc -c) >(grep o) >(grep f)
centralized(){
L=$(echo -n $*|wc -c)
echo -e "\x1b[$[ ($COLUMNS / 2) - ($L / 2) ]C$*"
}
psu(){ command ps -Hcl -F S f -u ${1:-$USER}; }
while [ 1 ]; do echo -n "$(date +%F_%T)" vmstat 1 2 | tail -1 sleep 4 done
zeros=3; from=1; to=15;
for foo in $(seq $from $to); do
echo mv "front${foo}back" "front$(printf "%0${zeros}d\n" $foo)back"
done
parse_rpm() { RPM=$1 B=${RPM##*/} B=${B%.rpm} A=${B##*.} B=${B%.*} R=${B##*-} B=${B%-*} V=${B##*-} B=${B%-*} N=$B echo "$N $V $R $A" }
About all structures (or entry points) it knows of.
biosdecode
rp() { local p eval p=":\$$1:" export $1=${p//:$2:/:} } ap() { rp "$1" "$2" eval export $1=\$$1$2 } pp() { rp "$1" "$2" eval export $1=$2:\$$1 }
grep VmHWM /proc/$(pgrep -d '/status /proc/' FOO)/status
cat /var/log/nginx/access.log | grep -oe '^[0-9.]\+' | \ perl -ne 'system("geoiplookup $_")' | \ grep -v found | \ grep -oe ', [A-Za-z ]\+$' | sort | uniq -c | sort -n
true && { echo success;} || { echo failed; }
for (( i = 0; i < 100; i++ )); do echo "$i"; done
[ ! -d /squashed/usr ] && mkdir -p /squashed/usr/{ro,rw}
mksquashfs /usr /squashed/usr/usr.sfs.new -b 65536
mv /squashed/usr/usr.sfs.new /squashed/usr/usr.sfs
reboot
watch -n 10 -d eval "sensors | grep RPM | sed -e 's/.*: *//;s/ RPM.*//'"
find /path/to/dir -type f -mtime +[#] -exec rm -f {} \;
find_alternatives(){
for i;do
which "$i" >/dev/null && { echo "$i"; return 0;}
done
return 1
}
echo 'Desktop SPAM!!!' | while read SPAM_OUT; do notify-send "$SPAM_OUT"; done
some_command > >(/bin/cmd_for_stdout) 2> >(/bin/cmd_for_stderr)
tail -f /var/log/messages.log | \
while read line ; do
echo $line | cut -d \ -f5- | sed s/\\[[0-9]*\\]// | espeak
done
p() {
l=$LINES
case $1 in
do) shift; IFS=$'\n' _pg=( $("$@") ) && _pgn=0 && p r;;
r) echo "${_pg[*]:_pgn:$((l-4))}";;
d) (( _pgn+=l-4 )); (( _pgn=_pgn>=${#_pg[@]}?${#_pg[@]}-l+4:_pgn )); p r;;
u) (( _pgn=_pgn<=l-4?0:_pgn-$l-4 )); p r;;
esac;
}
!$
Including files in sub-directories, and have keyboard shortcuts work
mplayer -playlist <(find $PWD -type f)
With buffer to seek back and forth
unrar p -inul *.rar|mplayer -cache 100000 -
mplayer -nolirc http://5253.live.streamtheworld.com/VIRGINRADIO_DUBAIAAC
read -p "Which station? "; mplayer --reallyquiet -vo none -ao sdl http://somafm.com/startstream=${REPLY}.pls
while [ 1 ]; do (ping -c 1 google.com || growlnotify -m 'ur wifiz, it has teh sad'); sleep 10; done
while : ; do if [ ! $(ls -l commander | cut -d ' ' -f5) -eq 0 ]; then notify-send "$(less commander)"; > commander; fi; done
seq 1 255 | parallel -j+0 'nc -w 1 -z -v 192.168.1.{} 80'
cd() { if [ -n "$1" ]; then [ -f "$1" ] && set -- "${1%/*}"; else [ -n "$CDDIR" ] && set -- "$CDDIR"; fi; command cd "$@"; }
enscript -E -B -3 -r -s 0 --borders -fCourier4.8 --mark-wrapped-lines=arrow
function every() { sed -n -e "${2}q" -e "0~${1}p" ${3:-/dev/stdin}; }
systemctl --failed | head -n -6 | tail -n -1
echo "string" | sed -e 's/.*match1//' -e 's/match2.*$//'
function ldd(){ objdump -p $1 | grep -i need; }
LD_TRACE_LOADED_OBJECTS=1 name_of_executable
gdb --batch --quiet -ex "thread apply all bt full" -ex "quit" ${exe} ${corefile}
echo Print text vertically|sed 's/\(.\)/\1\n/g'
find -maxdepth 1 -type d | while read dir; do echo $dir; echo cmd2; done
returning a boolean
echo 2006-10-10 | grep -c '^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$'
returning a boolean
if date -d 2006-10-10 >> /dev/null 2>&1; then echo 1; else echo 0; fi
"some line input" | sort | uniq -c | sort -nr
find $(locate hello) -type f -executable -print|grep -E "hello\$"
quickfind () { find . -maxdepth 2 -iname "*$1*" }
gnuplot -persist <(echo "plot '<(sort -n listOfNumbers.txt)' with lines")
while read line; do echo "$(date),$(hostname),$line"; done < somefile.txt
echo 1 2 3 > FILE; while read -a line; do echo ${line[2]}; done < FILE
stty cbreak -echo; KEY=$(dd bs=1 count=1 2>/dev/null); stty -cbreak echo
seq 20 | sed '5,6 { w out.txt }' #Can't print correctly. See sample output
tail -f /var/log/logfile|perl -e 'while (<>) {$l++;if (time > $e) {$e=time;print "$l\n";$l=0}}'
export PROMPT_COMMAND='( x=$? ; let x!=0 && echo shell returned $x )'
grep -a -B 25 -A 100 'some string in the file' /dev/sda1 > results.txt
multiple subdirectories into one file
find . -type f -name *.ext -exec cat {} > file.txt \;
find . -name "*.h" -o -name "*.cpp" | xargs sed -i 's/addVertexes/addVertices/g'
PATH="${PATH}:$(find ${HOME}/bin -type d | tr '\\n' ':' | sed 's/:$//’)"
find . -name “node_modules” -exec rm -rf ‘{}’ ;
find . -type f |xargs -I% sed -i ‘/group name/s/\>/ deleteMissing=“true”\>/’ %
for i in $(find . -type f); do sed -i ‘/group name/s/\>/ deleteMissing=“true”\>/’ $i; done
filename.ext
find . -type f -name filename.exe -exec sed -i “s/oldstring/oldstring/g” {} +;
find /test -type f -printf “%AY%Aj%AH%AM%AS—%h/%f” | sort -n
chown -cR –from=olduser:oldgroup newuser:newgroup \*
find . -name “.DS_Store” -print0 | xargs -0 rm -rf
to mobi using calibre's ebook-convert utility
find . -name ’\*.epub’ -exec sh -c ‘a={}; ebook-convert $a ${a%.epub}.mobi –still –more –options’ ;
find . -type f -exec sed -i s/oldstring/newstring/g {} +
grep -rl oldstring . | parallel sed -i -e ‘s/oldstring/newstring/’
Recursive search and replace old with new string, inside files
$ grep -rl oldstring . |xargs sed -i -e ‘s/oldstring/newstring/’
grep -rlZ oldstring . | xargs -0 sed -i -e ‘s/oldstring/newstring/’
TIME=$( { time YOUR_COMMAND_HERE; } 2\>&1 ) ; echo $TIME
command foo bar | sudo tee /etc/write-protected \> /dev/null
convert -quality 40% original_image reduced_image
scp -r $(ls | grep -vE “(Pattern1|Pattern2)”) user@remote_host:/location
rm-but() { ls -Q | grep -v “$1” | xargs rm -r ; }
for i in $(tar -tf FILE.TAR.GZ); do rm $i; done;
while read l; do echo -e “$l”; done \<1.txt \>2.txt
for wrd in $(cat file2) ; do sed -i .bk "/^$wrd/d" file1; done
time vmware-cmd -l | while read x; do printf “$x" vmware-cmd "$x” removesnapshots done
gmetric -n $METRIC_NAME -v foo -t string -d 10
ls -il; find \* ( -type d -prune ) -o -inum NUM -exec rm -i {} ;
grep -r “mystring” . |uniq | cut -d: -f1 | xargs sed -i “s/mystring//”
touch -t “YYYYMMDDhhmm.ss” dummy ; find . -anewer dummy
find . -size 1400c -exec rm {} ;
find . -mtime +30 -type f -exec rm -rf {} ;
zipinfo -1 aap.zip | xargs -d ‘’ rm
cat file.txt | grep -v /$ \> newfile.txt
find . -type f -name "\*.txt" | while read; do \[1\] && rm -vf "$THISFILE" done
Remove text from file1 which is in file2 and stores it in an other file
grep -Fvf file1 file2 \> file-new
echo “command lines” | rev | cut -c 2- | rev
cat -n $file | tail -n 100 && head -n number-of-lines-you-want-to-keep \> newfile
mv -b old_file_name new_and_already_existent_file_name
for i in \*ext; do mv $i ${i%.ext}; done
renice -20 -g 2874 \# (2784 found with ps -Aj)
find . -name “-help” -exec mv {} help.txt ;
echo -e ’’$_{1..80}‘’
\!\!
tr ’ ’ ‘’ \< FILENAME \> OUTPUT
for f in \*; do fn=$(echo $f | sed 's/\\(.\*\\)\\.\\(\[^.\]\*\\)$/\\1\\2/;s/./-/g;s//./g’); mv $f $fn; done
zmv ’(*.*)(.\*)’ ’${1//./_}$2’
(set -e; while true; do TEST_COMMAND; done) | tee log
/dev/sda).
partprobe
for times in $(seq 10) ; do puppet agent -t && break ; done
touch -acm yyyymmddhhMM.ss \[file\]
identify -format “%\[fx:w\]x%\[fx:h\]” logo:
ps -C program_name || { program_name & }
rhc snapshot restore -a {appName} -f {/path/to/snapshot/appName.tar.gz}
( cat badfile.log ; tac badfile.log | tac ) \> goodfile.log
scp -l10 pippo@serverciccio:/home/zutaniddu/\* .
curl $URL -s -L -o /dev/null -w ‘%{url_effective}’
shebang () { printf ‘%s’ 0a ‘\#\!’“$1” . w | ed -s “$2” ; }
\!\!; while \[ $? -ne 0 \]; do \!\!; done
title() { string=( $@ ); echo ${string\[@\]^} }
get_absolute_path() { echo $1 | \ sed "s|^\\(\[^/\].\*/.\*\\)|$(pwd)/\\1|;s|^(\[^/\]\*)$|$(which – $1)|;s|^$|$1|"; }
which any_path/a_command.sh | sed “s|^./|$(pwd)|”
find . -type d | while read dir ; do num=$(ls -l $dir | grep '^-' |\ wc -l) ; echo "$num $dir" ; done | sort -rnk1 | head
ps -o thcount -p PROCESS ID
\!\*
bash -x test.sh 2\>&1 | tee out.test
gmake runtestsingle testsingle=udtime
puppet agent –test –debug
puppet master –no-daemonize –debug
vmrun start /path/to/virtual_machine.vmx nogui
command output
num_errs=$(grep ERROR /var/log/syslog | tee \>(cat \>&2) | wc -l)
line that contains them.
zcat a_big_file.gz | sed -ne “$(zcat a_big_file.gz | tr -d "\[:print:\]" | cat -n | \\ grep *vP "^ \*\\d+\\t$” | cut -f 1 | sed -e “s/(\[0-9\]+)/\\1=;\\1p;/” | xargs)" | tr -c “\[:print:\]” “?”
even if logged out
( ( sleep 2h; your-command your-args ) & )
nohup and at
( trap ’’ 1 ( nice -n 19 sleep 2h && command rm -v -rf /garbage/ &\>/dev/null && trap 1 ) & )
psg(){ ps aux | grep -E “\[${1:0:1}\]${1:1}|^USER”; }
ps -e | grep SearchStringHere
find . -name "\*.jar" | \ while read line; do echo “\#\#\# $line” unzip -l $line done | \ grep “^\#\#\#|you-string” |less
directories where the matches occur
for i in $(locate your_search_phrase); do dirname $i; done | sort | uniq
egrep “(\[_a-zA-Z\]\[_a-zA-Z0-9\]//) //= //\\1 //\[\*/+-\] //\[0-9\]+ //;”
grep -Rl “pattern” files_or_dir
vifind() { vi $(find . -name “$1”) }
gdb command: apropos KEYWORD
find . ( -name “*cpp" -o -name "*hpp” ) \ -exec grep -Hn -E “043\[eE\]|70\[Dd\]7” {} ;
docx xlsx
find . -iname "*.???x" -type f -exec unzip -p ‘{}’ ’*’
bzgrep -i “pattern” pattern.bz2
slocate filename/dirname
netstat -an | grep -i listen
echo $(( $(ulimit -u) - $(find /proc -maxdepth 1 ( -user $USER -o -group $GROUPNAME ) -type d|wc -l) ))
watch -d ‘echo -e “Remaining: $\[2\] : $\[3\]”’
sleep 6s && notify-send -t 10000 -u critical “remember to think” &
find . -type f \-exec echo -n “touch -t $(echo” ; \-exec echo -n {} ; \-exec echo -n " | sed -E ’s/.\*([](/digit/){8})_([](/digit/){4})([](/digit/){2}).\*/\\1\\2.\\3/g’) " ; -exec echo {} ; | sh
setfacl -m u:john:r– myfile
pattern under the DIR.
find DIR -name “FILE” -exec grep -IHn STRING {} ;
an icon in notifications tray
notify-send –hint=int:transient:1 -u low -t 100 “Command” “Finished”
declare -f \[ function_name \]
netstat -pnut -W | column -t -s $’
netstat -tup -W | column -t
ps -eo pmem,comm | grep application-name
export PROMPT_COMMAND=‘echo -ne “\\033\]0;${PWD/\#$HOME/\~}\\007”;’
tail -n0 -f /var/log/messages | while read line; do notify-send “System Message” “$line”; done
echo ${PIPESTATUS\[@\]}
printf “\]’ ‘’ | grep -v”^$" | sort | uniq -c | sort -bn
for p in ${PATH//:/ }; do [-d $p && -x $p](-d%20$p%20&&%20-x%20$p) && echo $p; done
netstat -an | grep -i listen
ls /sys/bus/scsi/devices
'n' number of lines above and below it.
grep -C NO_OF_LINES STRING
curl -s http://www.drudgereport.com |\ sed -n ‘/\<\! MAIN HEADLINE\>/,/<!-- Main headlines links END --->/p’ |\ grep -oP "(?\<=\>)\[^\<\].\*\[^\>\](?=\<)"
grep -ao -HP "http://\[^/\]///" //
lsof -bw -d cwd -a -c java
OK, otherwise unmount, create dir and mount)
(mountpoint -q “/media/mpdr1” && df /media/mpdr1/\* \> /dev/null 2\>&1) || ((sudo umount “/media/mpdr1” \> /dev/null 2\>&1 || true) && (sudo mkdir “/media/mpdr1” \> /dev/null 2\>&1 || true) && sudo mount “/dev/sdd1” “/media/mpdr1”)
iozone -s 2g -r 64 -i 0 -i 1 -t 1
find . -type d -maxdepth 1 | xargs du -sh
find . \! -name “$(printf ‘*\[\\001-\\037\\177\]*’)”
seq 1 12 | sed 1,5d ; seq 1 12 | head –lines=-5
cat skype_log | sed -s ’s/(\[.*\]) (.*): (.\*)/\<\\2\> \\3/’
echo $numbers | sed "s/\\( \\|$)//g" | sort -nu | tr “” " " | sed -e "s/^ \*//" -e “s/ $//”
tr -d \# GNU tr version 1.22 or higher \`\`\`