Favorite Linux Commands
Post # 25 permalink Topic #25 by mreschke on 2008-02-14 10:27:27 (viewed 1,521 times)

A list of my favorite or often used (and forgotten) linux commands (favcom).

Most of the favorite one liners below come from http://commandlinefu.com These are only the ones that I have found to be useful for me. Browse and search commandlinefu for yourself to find thousands of incredible linux commands. Also check out the references links, a lot of them simply goto commandlinefu with custom search queries.

Fixme
Add later, how to simply resize (re incode with different settings) a mpg

ffmpeg -i orig.mpg -s 1280x720 -vcodec mpeg2video -b 1400 -acodec libmp3lame -ac 2 -threads 4 test01.mpg

References[-][- -][++]

Favorite Commands[-][- -][++]

Note, this list of commands is meant to be installed on your local machine, see Public Curl Installers for more information

favcom.txt
################################################################
### mReschke Favorite Command Quick Reference Cheat Sheet!   ###
###----------------------------------------------------------###
### http://mreschke.com/mrcore/mrticles/FavoriteCommands     ###
### http://www.commandlinefu.com/commands/browse             ###
################################################################

#Wget to command line output instead of a file
  $ wget -qO- <url>

#Curl to command line output
  $ curl -s <url>

#Resize Terminal Window
  $ printf "\e[8;70;180;t"

#Renames/replaces spaces to nothing in *.jpg
  $ rename "s/ *//g" *.jpg

#Renames/replaces all lowercase first characters to uppercase
  $ rename -n "s/( |^)\w/\U$&/g" *.jpg

#Renames/replaces files example
##If files was 'Bones (S01E01 Title Here.avi' and you wanted a ) after the episode number
  $ rename -n "s/E[0-9][0-9]/$&)/g" *

#Renames/replaces all periods to spaces (spaces are tricky, use this instead of a find like .JPG example below
  $ for file in *; do mv "$file" "${file//./ }"; done

#Rename/replaces .JPG to .jpg recursively
  $ find /path/to/images -name '*.JPG' -exec rename "s/.JPG/.jpg/g" {} \;

#Rename files by inode number instead of filename
  $ dir -i
  $ find . -inum 208946 -exec mv {} newfilenamehere \;

#Delete all file patterns recursively
  $ find ./ -iname 'Somename*' -exec rm {} \;

#Count/cat all files lines returned by find command (add |wc to find word counts of find files)
  $ find ./ -iname "*.php" -exec cat{} \;|wc

#Count All files lines by grep
  $ grep "" ./ -R -h|wc

#Replaces spaces in filenames (bash example)
  $ for i in *\ *; do if [ -f "$i" ]; then mv "$i" ${i// /_}; fi; done

#Convert all .png to .jpg using Image Magic, and delete old .png
  $ for i in *.png; do convert "$i" "${i%.png}.jpg" && rm "$i" && echo "$i is converted."; done

#Convert PDF to JPG using ghostscript, set -r for resolution (default -r144 is 72dpi)
  $ gs -dNOPAUSE -sDEVICE=jpeg -r144 -sOutputFile=p%03d.jpg file.pdf

#Replace text (replace war with peace) in all files
  $ sed -i -e 's/war/peace/g' *

#Press anykey to continue
  $ read enterKey

#Empty a file
  $ > filename.txt
  $ truncate -s0 filename.txt

#Get your public IP (the curl one works, the other returns eth0 on qserver)
  $ curl -s http://icanhazip.com/

#Calculator function
  $ calc() { awk 'BEGIN { OFMT="%f"; print '"$*"'; exit}'; }

#How much disk space all images take in a given directory
  $ find /home/bubo/ -type f \( -iname \*.jpg -print0 , -iname \*.png -print0 , -iname \*gif -print0 \) | du -cm --files0-from - | tail -1

#List folders containing only PNGs
  $ find . -name '*png' -printf '%h\0' | xargs -0 ls -l --hide=*.png | grep -ZB1 ' 0$'

#Extract tarball from internet without local zaving (saves uncompress data, but not zip!)
  $ wget -O - http://example.com/a.gz | tar xz

#Great Backup, backups your current directory, on the fly gzip "." while showing a nice progress indicator
  $ tar -cf - . | pv -s $(du -sb . | awk '{print $1}') | gzip > /tmp/home.tgz

#Scan PHP files in entire folder and compile them to check for errors
  $ find . -name "*.php" -exec php -l {} \;

#Exchange SSH keys from one server to another
  $ cat ~/.ssh/id_rsa.pub | ssh <remote_host> "xargs --null echo >> ~/.ssh/authorized_keys"

#Output stats from a running dd command to see its progress
  $ watch -n60 --kill -USR1 $(pgrep dd)

#Harder, Faster, Stronger SSH connection
  $ ssh -4 -C -c blowfish-cbc

#Show S.M.A.R.T HD info, like temperature... (Expedient hard disk temprature and load cycle stats)
  $ watch -d 'sudo smartctl -a /dev/sda | grep Load_Cycle_Count ; sudo smartctl -a /dev/sda | grep Temp'

#Blocks thousands of the works spam host websites
  $ wget -q -O - http://someonewhocares.org/hosts/ | grep ^127 >> /etc/hosts

#Show when filesystem was created
  $ dumpe2fs -h /dev/DEVICE | grep 'created'

#List all TCP opened ports on localhost in LISTEN mode
  $ sudo netstat -nptl
  $ sudo lsof -P -i -n -sTCP:LISTEN
  $ nmap -p 1-65535 --open localhost

#Analyse compressed or current Apache access logs for the most commonly requested pages
  $ zcat /var/log/apache2/access.*.gz | awk '{print $7}' | sort | uniq -c | sort -n | tail -n 20
  $ cat /var/log/apache2/access.* | awk '{print $7}' | sort | uniq -c | sort -n | tail -n 20

#Extract plain text from Microsoft DOCX word files
  $ unzip -p some.docx word/document.xml | sed -e 's/<[^>]\{1,\}>//g; s/[^[:print:]]\{1,\}//g'

#Find all .jpg files and copy them to a certial location
  $ find . -iname "*.jpg" -print0 | tr '[A-Z]' '[a-z]' | xargs -0 cp --backup=numbered -dp -u --target-directory {location} &

#Convert WMV video to XVID video (avi)
  $ mencoder -ovc xvid -oac mp3lame -srate 44100 -af lavcresample=44100 -xvidencopts fixed_quant=4 Foo.wmv -o Bar.avi
  $ mencoder movie.wmv -ofps 23.976 -ovc xvid -oac mp3lame -lameopts abr:br=128 -xvidencopts pass=2:bitrate=700 -o movie.avi

#Monitor all information on a port
  $ tcpdump tcp port 80

#Restart apache
  $ httpd -k restart
  $ sudo /etc/init.d/apache2 restart

#Restart, Stop, Start Daemon (Services) (sometimes its /etc/rc.d/servicename)
  $ sudo /etc/init.d/servicename restart
  $ sudo /etc/init.d/servicename start
  $ sudo /etc/init.d/servicename stop

#Goto run level x
  $ telinit x
  $ init x

#Update yum and rpm programs
  $ yum update rpm* yum*

#Get hardware Information
  $ getconf -a
  $ dmesg |grep xxx
  $ lspci
  $ lsusb

#Get BIOS info
  $ dmidecode --type 0

#Get CPU Info
  $ dmidecode --type processor

#Get PCI/PCIe Info
  $ dmidecode --type 9

#Load kernel module
  $ modprobe modulename

#Remove Kernel Module
  $ modprobe -r modulename

#List Loaded Kernel Modules
  $ lsmod

#List All Available Kernel Modules
  $ modprobe -l

#Get Kernel Version Info
  $ uname -a

#Get Distro Version (Ubuntu)
  $ lsb_release -a

#Get Mounted Hard Drive Free/Used Space Info
  $ df -h

#Show All Hard Drives on the system
  $ sudo fdisk -l

#Get Memory Information
  $ free

#Get Running Processes (Applications) Information
  $ ps -A
  $ ps -Al
  $ ps -AlF
  $ ps -AlFH
  $ ps aux (my fav)
  $ ps faux (my fav)
  $ ps aux -H
  $ pstree
  $ ps -U vivek -u vivek u (procs by user)
  $ ps -C apache2 u (procs by name)
  $ ps aux|grep apache2 (procs by name)
  $ pgrep apache2 (show all PIDS of apache2)
  $ ps -C apache2 -o pid= (show all PIDS of apache2)
  $ ps -p 12323 -o comm= (show name of PID)
  $ ps -auxf | sort -nr -k 3 | head -10 (top 10 processor hogs)
  $ top

#Kill running process (xxx is process ID, the -9 means kill it no matter what)
  $ kill xxx
  $ ps xxx kill
  $ killall processname
  $ kill -9 xxxx
  $ kill `pgrep apache2`

#Output to file (> write, >> append)
  $ ls > /root/Desktop/test.txt

#List all files in dir and sort by size DESC
  $ du -s /home | sort -nr

#Burn CD/DVD ISO (or other) Image
  $ cdrecord -v -dao speed=4 dev=/dev/dvd /path/to/someimage.iso

#Erase CD-RW with cdrecord
  $ cdrecord blank=fast

#Install RPM package
  $ rpm -Uvh filename.rpm

#Extract .tar or .tar.gz
  $ tar -xzvf filename.tar.gz

#Extract .tar or .tar.gz to different location
  $ tar -xzvf filename.tar.gz -C /home/myuser/extracthere/

#Extract tar.bz2
  $ tar xjvf filename.tar.bz2

#Get absolute path to the current shell dir
  $ pwd

#Get uptime of the system (how long computer has been on)
  $ uptime

#Get num of lines, words, characters in file
  $ wc
  $ wc -l

#Get RPC information for computer
  $ rpcinfo -p
  $ rpcinfo -p compname

#Reboot machine when everything is hanging. 
##<alt> + <print screen/sys rq> + <R> - <S> - <E> - <I> - <U> - <B>
##If the machine is hanging and the only help
##would be the power button, this key-combination will help to reboot your machine (more or less) gracefully.
##R - gives back control of the keyboard
##S - issues a sync
##E - sends all processes but init the term singal
##I - sends all processes but init the kill signal
##U - mounts all filesystem ro to prevent a fsck at reboot
##B - reboots the system

#Create an .iso image of disk (like dd but better, has error checking)
  $ readom dev=/dev/scd0 f=/path/to/image.iso

#Search and replace command output
  $ cat /var/log/messages | sed 's/This/ToThis/g'

#Search and replace in a file and saves back to the file
 Uses the -i of sed, which is the inline parameter
 Remember with sed search and replace you can use any character as the # char
  $ sed -i "s#HOSTNAME=\"myhost\"#HOSTNAME=\"newhostname\"#g" /etc/rc.conf

#rsync one file or folder from remote server via SSH to local server
##Notice single and double quotes to prevent spaces from erroring
  $ rsync -vrLtz --partial --progress -e 'ssh -p 12222' remoteuser@remotehost.com:'"/mnt/harddrive/some file to copy.avi"' "/mnt/yourhard/drive location/movies/"

#Read DVD information to command line output (name, tracks, etc...)
  $ lsdvd

#Backup mysql database to file
  $ mysqldump -u username -h localhost -p database_name | gzip -9 > backup_db.sql.gz
  $ mysqldump -h localhost -u username -p database_name > backup_db.sql

#Reorder file columns with awk (Here I set the delimiter to the tab character)
  $ awk -F '\t' -v 'OFS=\t' '{print $4, $3, $5, $2, $1}' data.txt

#Cut out certain columns from output or file (the -d sets your delimiter. -f shows column 1)
##NOTE, you cannot re order output here, if you did -f1,3,2 it would still look like -f,1,2,3
##USE awk if you want to re-order
  $ cat somefile.txt | cut -d "," -f1

#Convert WAV to MP3 Using Lame
  $ for f in *.wav ; do lame -b 192 -h -V 0 --ta 'Ralph Myerz & The Jack Herren Band' --tl 'Special Album' "$f" ; done
      for x in *.mp3 ; do lame -a -b 64 "$x" mono64-"${x}"; done

#Monitor Disk IO
  $ watch iostat
  $ iostat -xtc 5 3

#Monitor CPUs
  $ watch mpstat
  $ watch mpstat -P ALL
  $ sar -P All 1

#Get CPU percent utilization (not the same a load)
  $ sar -P ALL 1 2 | grep 'Average.*all' | awk -F" " '{ print 100.0-$NF }'

#Monitor System Activity, Hardware and System Information
  $ vmstat 1

#Network, find established connections
  $ netstat -unat|grep ESTABLISHED
  $ watch 'netstat -unat|grep ESTABLISHED'

#Network, find DoS attacks
  $ netstat -atun | awk '{print $5}' | cut -d: -f1 | sed -e '/^$/d' |sort | uniq -c | sort -n

#Network, check IP activity
  $ netstat -nat | awk '{print $6}' | sort | uniq -c | sort -n

#Zip up a folder and its subfolders
  $ zip /home/backups/myconfigs.zip /etc/ -r

#Count size in kilobytes of command line output using the bc command
  #The wc command can count bytes
  $ cat /etc/hosts | wc --c
  #But I want to divide by 1000 or 1024 to get kilobytes
  $ echo "scale=4;`cat /etc/hosts | wc --c` / 1000" | bc
  $ echo "scale=4;`curl google.com | wc --c` / 1000" | bc
  #Example bc command (the scale shows x number of decimal places)
  $ echo '5+5' | bc
  $ echo 'scale=4;1232/3343' | bc

#Count size of curl output
  #If your curl does not display the output to stdout, it will show a size and kbps chart
  $ curl google.com > /dev/null

#Replace newline (carriage return) of output to <br> using sed or perl
 #Wow very hard to find with sed, and complicated too
 $ ls -hal | sed ':a;N;$!ba;s/\n/<br>/g'
 #Until I found this with sed I used perl with
 $ ls -al | perl -pi -e "s/\n/<br>/g"
 

Favorite Help Info[-][- -][++]

autostart application
* put it in /home/user/.kde/Autostart
* or /home/user/.config/autostart