COLLECTION OF ALL LINUX COMMANDS WITH EXAMPLES
HARDWARE
# list all hardware details
inxi -F
 
# list all hardware data from BIOS
dmidecode
dmidecode -t system
dmidecode -t memory
dmidecode -t processor
dmidecode -t bios
dmidecode -t baseboard
 
# hardware information
hwinfo
hwinfo --short
 
# list all hardware
lshw
lshw -short
lshw -html > info.html
 
# list all PCI devices
lspci
lspci -t
# produces output in tree format
lspci -v
# produces detailed information about each connected device
 
# list block devices
blkid
blkid -a
 
# reports various partitions, their mount points and the used and available space on each
df -H
 
# check the used, free and total amount of RAM
free -m
 
# memory information
cat /proc/meminfo
 
# cpu information - number of CPU’s, cores, CPU family model, CPU caches, threads, etc.
lscpu
cat /proc/cpuinfo
 
# linux/kernel information
cat /proc/version
uname -a
 
# partitions information
cat /proc/partitions
 
# show CPU temperature
cat /proc/acpi/thermal_zone/CPUZ/temperature
 
# CPU power
cpupower frequency-info
cpupower -c all frequency-set -g ondemand
cpupower set -c all frequency-set --min 800MHz --max 2.30GHz
 
# gets information about sata devices like hard disks
hdparm -i /dev/sda
 
# List USB devices
lsusb
lsusb -v
 
HDD DIAGNOSTICS
# check for bad blocks
badblocks -v /dev/sdc1
 
# repair partition
fsck -t ext4 /dev/sdc1
 
# repair partition including ntfs
e2fsck -c /dev/sdc1
 
# repair 'ntfs' partition
ntfsfix -b /dev/sdc1
 
# monitoring and analysis tool for hard drives
smartctl -i /dev/sdc
# ensure the hard disk supports SMART and is enabled
smartctl -c /dev/sdc
# gives approximate time duration before performing a test
smartctl -t (short|long|conveyance|select) /dev/sdc
# starts desired test
smartctl -t (short|long|conveyance|select) -C /dev/sdc
# starts desired test in foreground mode
smartctl -a /dev/sdc
# view the test results
smartctl -d ata -H /dev/sdc1
# check hard drive status
 
# repair many types of partitions and copy data from broken drives
testdisk
 
# create user: '-d' sets home folder, '-g' sets the group, '-p' sets password
useradd -m -d /home/[user_name] -g [group_name] -c "Full name" -p "$(openssl passwd -1 [password])" [user_name]
 
# create user without shell
adduser --shell /bin/false --no-create-home [user_name]
useradd --shell /bin/false --no-create-home [user_name]
 
# move user's home folder to a new location
usermod -m -d /userspace/[user_name] [user_name]
 
# add user only to groups group1, group2 and group3
usermod -G group1,group2,group3 [user_name]
 
# change the UID of a user
usermod -u 1000 [user_name]
 
# remove user from the system, but keep his home folder
userdel [user_name]
 
# remove user completely from the system, delete also his home folder
userdel -r [user_name]
 
# add user to specific group
gpasswd [group_name] -a [user_name]
 
# remove user from specific group
gpasswd [group_name] -r [user_name]
 
# change the GID of a group
groupmod -g 1000 [group_name]
 
# check in which groups specific user belongs
groups [user_name]
id -nG [user_name]
 
# change password for specific user
passwd [user_name]
 
# force logout for a logged user
pkill -KILL -u [user_name]
 
# list all partitions
fdisk -l /dev/sda
 
# remount all partitions after changes in /etc/fstab
mount -o remount /
 
# mount USB
mount -t vfat -o umask=0022,gid=1000,uid=1000 /dev/sdd1 /mnt/usb
 
( EXT4 )
# show partition label
e2label /dev/sda1
 
# format "EXT4" partition
mkfs.ext4 /dev/sdc1
mke2fs -t ext4 -L "LABEL_123" /dev/sdc1
 
# after format you can tune the "EXT4" partition, '-c' means to run fsck every 200 times, '-m' means
# to set 1% for super user instead of 5% which comes by default
tune2fs -c 200 -m 1 /dev/sdc1
 
# set the label of "EXT4" partition
e2label /dev/sdc1 "LABEL_123"
 
# mount linux ext4 partition
mount -t ext4 -o /dev/sda1 /mnt
mount -t ext4 -o acl /dev/sda1 /mnt
 
( FAT 32 )
# show partition label
fatlabel /dev/sda1
 
# format "FAT 32" partition
mkfs.vfat /dev/sdc1
mkfs -t vfat /dev/sdc1
mke2fs -t vfat -L "LABEL_123" /dev/sdc1
 
# set the label of "FAT 32" partition
fatlabel /dev/sdc1 "LABEL_123"
 
( NTFS )
# format "NTFS" partition
mkntfs -L "LABEL_123" /dev/sdc1
 
# set the label of "NTFS" partition
ntfslabel -f /dev/sdc1 "LABEL_123"
 
( DVD )
# mount CD/DVD. If /mnt/dvd doesn't exist create it with mkdir /mnt/dvd
mount -t iso9660 -o ro /dev/dvd /mnt/dvd
 
( MANAGE FILES )
# get the file permissions as three-digit number, e.x. 755
stat -c "%a %n" file1.txt
 
# set file permissions
chmod 755 file1.txt
chmod ugo+rwx file1.txt
# (u)ser, (g)roup, (o)thers + (r)ead, (w)rite, e(x)ecute
chmod o+r file1.txt
chmod go-w file1.txt
 
# hibernate
dbus-send --system --print-reply --dest=org.freedesktop.UPower /org/freedesktop/UPower org.freedesktop.UPower.Hibernate
 
# reboot
dbus-send --system --print-reply --dest=org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.Restart
 
# shutdown
dbus-send --system --print-reply --dest=org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.Stop
 
# shutdown the system and turn the power off immediately
shutdown -h now
 
# shutdown the system after 10 minutes
shutdown -h +10
 
# reboot the system immediately
shutdown -r now
 
# force the filesystem check during reboot
shutdown -Fr now
 
# view crontab entries for user 'john'
crontab -u john -l
 
# list all processes using tree view
ps -efH
 
# create, add and enable 4GB swap file
dd if=/dev/zero of=/var/swap.tmp bs=1024 count=$((3*1024*1024))
mkswap -f /var/swap.tmp
swapon /var/swap.tmp
 
# list currently loaded modules in the kernel
lsmod
cat /proc/modules
 
# show module info
modinfo moduleX
 
# install new module in the kernel
modprobe moduleX
 
# install/insert module using file
insmod /lib/modules/4.14.3.el7.x86_64/kernel/drivers/cdrom/cdrom.ko
 
# install new module in the kernel under different name
modprobe moduleX -o moduleY
 
# remove loaded kernel module
modprobe -r moduleX
 
( YUM )
# YUM is the default package management tool for Red Hat / CentOS

# list enabled Yum repositories
yum repolist
 
# list all enabled and disabled Yum repositories in your system
yum repolist all
 
# list all available packages in the Yum database
yum list
 
# list all available Group Packages
yum grouplist
 
# list all installed packages
yum list installed
 
# search for package
yum search firefox
 
# install package
yum install firefox
yum install firefox -y
# -y means to suppress asking you whether you want to install it
yum --enablerepo=epel install firefox
# install package from specific repository
yum -ivh firefox-49.5.3-37.el7.x86_64.rpm
# install package using RPM file
 
# install Group Package
yum groupinstall 'MySQL Database'
 
# update package
yum update firefox
 
# update Group Package
yum groupupdate 'MySQL Database'
 
# remove installed package
yum remove firefox
 
# remove Group Package
yum groupremove 'MySQL Database'
 
# find which package a specific file belongs to
yum provides /usr/bin/nautilus
 
# find how many of the installed packages on your system have updates available
yum check-update
 
# keep your system up-to-date with all security and binary package updates
yum update
 
# clean Yum cache
yum clean all
 
# view all the past transactions of yum command
yum history
 
( DPKG )
# DPKG is the default package management tool for Debian

# list all installed packages
dpkg -l
 
# install package
dpkg -i firefox.deb
 
# remove package, but the configuration files are not removed
dpkg -r firefox
 
# purge the package completely
dpkg -P firefox
 
# view content of a package
dpkg -c firefox.deb
 
# check if package is installed or not
dpkg -s firefox
 
# recursively, install all "*.deb" files found in "debpackages/" and its subdirectories
dpkg -R --install debpackages/
 
# unpack the package, but don't install it
dpkg --unpack firefox.deb
 
( APT )
# APT is the default package management tool for Ubuntu

# list all installed packages
apt-cache pkgnames
 
# search for package
apt-cache search firefox
 
# view info/description about a package
apt-cache show firefox
 
# view package dependencies
apt-cache showpkg firefox
 
# view overall cache statistics
apt-cache stats
 
# update all system packages
apt-get update
 
# upgrade all software packages
apt-get upgrade
 
# install package
apt-get install firefox
apt-get install firefox --no-upgrade
# prevent already installed packages from upgrading
apt-get install firefox --only-upgrade
# will only upgrade the package
 
# remove package, but keep it's configuration files
apt-get remove firefox
 
# completely remove package, including it's configuration files
apt-get purge firefox
 
# remove packages that were installed to satisfy dependencies for other packages, but now are not required
apt-get autoremove firefox
 
# free up the disk space by cleaning the downloaded .DEB files from the local repository
apt-get clean
 
# deletes all '.DEB' files from /var/cache/apt/archives
apt-get autoclean
 
# download the source code for a package
apt-get --download-only source [package]
 
# download and unpack source code of a package
apt-get source [package]
 
# download, unpack and compile the source code at the same time
apt-get --compile source [package]
 
# check for broken dependencies
apt-get check
 
( PACMAN )
# PACMAN is the default package management tool for Arch Linux

# sync the database
pacman -Syy
 
# update all packages
pacman -Syu
 
# install a single package or list of packages (including dependencies)
pacman -S [package]
pacman -S [package1] [package2]
 
# see what packages belong to the gnome group
pacman -Sg gnome
 
# remove a package if nothing is depending on it, leave it's dependencies installed
pacman -R [package]
 
# remove a package if nothing is depending on it, remove it's dependencies that aren't required by any other installed package
pacman -Rs [package]
 
# cascade removal of a package + all of it's dependencies
pacman -Rc [package]
 
# force removal of a package, skip dependency version check
pacman -Rdd [package]
# these two commands do the same thing
pacman -R --nodeps --nodeps [package]
# these two commands do the same thing
 
# search for package
pacman -Ss [package]
 
# search for exact package
pacman -Ss '^thunar-'
 
# search for already installed package
pacman -Qs [package]
 
# search for a package containing a file
pacman -Fy
# sync the files database
pacman -Fs file_name
 
# display extensive information about a given package
pacman -Si [package]
 
# for locally installed packages
pacman -Qi [package]
 
# retrieve a list of the files installed by a package
pacman -Ql [package]
 
# retrieve a list of the files installed by a remote package
pacman -Fl [package]
 
# query the database to know which package a file in the file system belongs to
pacman -Qo /path/to/file_name
 
# query the database to know which remote package a file belongs to
pacman -Fo /path/to/file_name
 
# list a dependency tree of a package
pactree [package]
 
# list all the packages recursively depending on an installed package, use whoneeds from pkgtools
whoneeds [package]
 
# pacman stores its downloaded packages in /var/cache/pacman/pkg/ and does not remove the old
# or uninstalled versions automatically, therefore it is necessary to deliberately clean up
# that folder periodically to prevent such folder to grow indefinitely in size.
# The built-in option to remove all the cached packages that are not currently installed is:
pacman -Sc
 
# download a package without installing it
pacman -Sw [package]
 
# install a 'local' package that is not from a remote repository (e.g. the package is from the AUR)
pacman -U /path/to/package/package_name-version.pkg.tar.xz
 
# keep a copy of the local package in pacman's cache
pacman -U file:///path/to/package/package_name-version.pkg.tar.xz
 
# download and rebuild the latest AUR packages from their source (regardless of PKGBUILD files)
yaourt -Syua --devel
 
# find file in folder
find / -name file.txt
 
# find file in folder ignring the case. It will find File.TXT as well
find / -iname file.txt
 
# list all files with extension '.txt'
find / -name *.txt
 
# list all files, which are not 'mp3'
find / -not -name "*.mp3"
 
# list all files starting with 'abc' or ending with '.php'
find / -name 'abc*' ! -name '*.php'
 
# list all '.php' or '.txt' files
find / -name '*.php' -o -name '*.txt'
 
# list all files updated in the last 24 hours
find / -mtime -1 -print
 
# list all files bigger than 200kb
find / -type f -size +200k -exec ls -lh {} \
 
# find all 'sticky bits' files
find / -perm +1000
 
# find all '.php' files with folder max depth = 2
find / -maxdepth 2 -name "*.php"
 
# find all hidden files
find / -type f -name ".*"
 
# find all directories with name 'images'
find / -type d -name images
 
# find all files with name 'images'
find / -type f -name images
 
# find all '.png' files in direcory
find / -type f -name "*.png"
 
# find all files with permission 777
find / -type f -perm 0777 -print
 
# find all files with permission different than 777
find / -type f ! -perm 777
 
# find all empty files
find / -type f -empty
 
# find all empty directories
find / -type d -empty
 
# find all files which owner is user 'john'
find / -user john
 
# find all files with name 'file.txt' which owner is root
find / -user root -name file.txt
 
# find all files which have been mofified 3 days ago
find / -mtime 3
 
# find all files which have been accessed 3 days ago
find / -atime 3
 
# find all files which have been mofified 3-5 days ago
find / -mtime +3 -mtime -5
 
# find all files which have been created in the last 1 hour
find / -cmin -60
 
# find all files which have been modified in the last 1 hour
find / -mmin -60
 
# find all files which have been accessed in the last 1 hour
find / -amin -60
 
# find all files, which have been modified after file.txt has been modified
find / -newer file.txt
 
# find all files, which are bigger than 50MB and smaller than 100MB
find / -size +50M -size -100M
 
# find all files, which are bigger than 100MB and delete them
find / -size +100M -exec rm -rf {} \;
 
# find SUID files
find / -perm /u=s
 
# find SGID files
find / -perm /g+s
 
# find all READ-ONLY files
find / -perm /u=r
 
# find all EXECUTABLE files
find / -perm /a=x
 
# find all files, which have group read permission
find / -perm -g=r -type f -exec ls -l {} \;
 
# find all files, which only have group read permission
find / -perm g=r -type f -exec ls -l {} \;
 
# find all directories with permission=777 and use chmod command to set permission to 755
find / -type d -perm 777 -print -exec chmod 755 {} \;
 
# delete all '.mp3' files
find / -type f -name "*.mp3" -exec rm -f {} \;
 
# find the largest 5 files
find / -type f -exec ls -s {} \; | sort -n -r | head -5
 
# find file anywhere starting with '/'
locate file.txt
 
( TGZ )
# create 'tar.gz' archive
tar czf archive.tar.gz folder
tar czf archive.tar.gz file1.txt file2.txt
 
# extract files from 'tar.gz' archive
tar zxf archive.tar.gz
 
( GZ )
# create 'gz' archive
gzip file1.txt file2.txt
# it will replace the 2 files with file1.txt.gz and file2.txt.gz
gzip -k file1.txt file2.txt
# to avoid deleting the original files
gzip -cd old.gz | gzip > new.gz
# recompress archive to get better compression
gunzip file1.txt.gz file2.txt.gz
# it will replace the 2 files with file1.txt and file2.txt
gunzip -c file1.txt.gz
# if file1.txt is text file then view it without decompressing it
 
( BZ2 )
# create 'tar.bz2' archive
tar cjf archive.tar.bz2 folder
tar cjf archive.tar.bz2 file1.txt file2.txt
 
# extract files from 'tar.bz2' archive
tar jxf archive.tar.gz
 
( BZIP2 )
# create 'bz2' archive
bzip2 file1.txt file2.txt
# it will replace the 2 files with file1.txt.bz2 and file2.txt.bz2
bzip2 -k file1.txt file2.txt
# to avoid deleting the original files
bzip2 -d file1.txt.bz2 file2.txt.bz2
# decompress, it will replace the 2 files with file1.txt and file2.txt
bunzip2 file1.txt.bz2 file2.txt.bz2
# decompress, it will replace the 2 files with file1.txt and file2.txt
bunzip2 -c file1.txt.bz2
# if file1.txt is text file then view it without decompressing it
 
( TXZ )
# create 'tar.xz' archive
tar cJf archive.tar.xz folder
tar cJf archive.tar.xz file1.txt file2.txt
 
# extract files from 'tar.xz' archive
tar Jxf archive.tar.xz
 
( 7Z )
# create '7z' archive
7z a archive.7z folder
7z a archive.7z file1.txt file2.txt
 
# create password protected '7z' archive, '-mhe' enables or disables header encryption (on/off)
# you have to replace {PASSWORD} with your secret key
7z a archive.7z -p{PASSWORD} -mhe=on file1.txt file2.txt
 
# create multi volume '7z' archive
7z a archive.7z -v100m /path/to/big-size-directory
 
# create '7z' archive without compression
7z a -m0=Copy archive.7z folder
7z a -mx0 archive.7z folder
# use anything from '-mx0' to '-mx9', mx9 is ultra compression
 
# update '7z' archive
7z u archive.7z folder
 
# list '7z' archive details
7z l archive.7z
 
# display technical data of a '7z' archive - type, physical size, header size, etc.
7z l -slt archive.7z
 
# delete file from '7z' archive
7z d archive.7z file1.txt -r
 
# extract '7z' archive in the current folder without using directory names
7z e archive.7z
 
# extract '7z' archive in the current folder using full paths
7z x archive.7z
7z x -o/path/to/directory archive.7z
 
# backup folder
tar cf - folder | 7z a -si backup.tar.7z
 
# restore from backup
7z x -so backup.tar.7z | tar xf
 
( RAR )
# create 'rar' archive
rar a archive.rar folder
rar a archive.rar file1.txt file2.txt
 
# create 'rar' archive + split in multiple files each having size=1.9GB
rar a -v1900m archive.rar folder
 
# create encrypted 'rar' archive without compression. Replace {PASSWORD} with your secret key
rar a -hp{PASSWORD} -m0 archive.rar folder
 
# extract files from 'rar' archive
unrar e archive.rar
 
( XAR )
# create 'xar' archive
xar -cf archive.xar folder
xar -cf archive.xar file1.txt file2.txt
 
# list files in 'xar' archive
xar -tf archive.xar
 
# extract files from 'xar' archive
xar -xf archive.xar
 
( CPIO )
# create 'CPIO' archive
ls | cpio -ov > archive.cpio
 
# create 'CPIO' archive
find . -print -depth | cpio -ov > archive.cpio
 
# create 'CPIO' archive
ls | cpio -ov -H tar -F archive.tar
 
# extract 'CPIO' archive
cpio -idv < archive.cpio
 
# extract 'CPIO' archive
cpio -idv -F archive.cpio
 
# list files in 'CPIO' archive
cpio -it -F archive.tar
 
( PATOOL )
# PATOOL can handle any kind of archive format. It has been created to serve as wrapper for all of them.


# list all supported archive formats
patool formats
 
# extract archive
patool extract archive.zip
patool extract archive.rar
patool extract archive.txz
 
# test if archive is intact
patool test --verbose archive.txz
 
# list files inside an archive
patool list archive.rar
 
# create archive
patool create --verbose archive.zip folder
patool create --verbose archive.txz folder
patool create --verbose archive.tar.bz2 file1.txt file2.txt
 
# list differences between two archive contents
patool diff archive1.tar.gz archive2.zip
 
# search archive contents
patool search "def urlopen" archive.zip
 
# compress the archive in a different format
patool repack archive.tar.gz archive.zip
 
( PAX )
# PAX can handle any kind of archive format. It has been created to serve as wrapper for all of them.


# create archive
pax -wf archive.tar folder
pax -wf archive.zip folder
pax -wf archive.tar.bz2 folder
 
# list content of archive
pax -f archive.tar
 
# extract archive
pax -rf archive.tar
 
# extract archive using regular expression
pax -r <archive.tar 'sales/*.txt' 'products/*.png'
pax -r <archive.tar 'sales/year_200[123456].txt'
 
# copy current directory content to new path
pax -rw . /path/newdir
 
# copy only files modified today
pax -rw -T file1.txt folder /n/mybackups/$(date +%A)/
 
# extract all files from the archive archive.pax, which are owned by 'root' with group 'bin' and preserve all file permissions
pax -r -pe -U root -G bin -f archive.pax
 
# Update (and list) only those files in the destination directory '/backup', which are
# older than files with the same name found in the source file tree '/home'
pax -r -w -v -Y -Z /home /backup
 
( ATOOL )
# ATOOL can handle any kind of archive format. It has been created to serve as wrapper for all of them.


# create archive
atool -a archive.tar.gz folder
apack archive.tar.gz folder
 
# extract archive
atool -x archive.tar.gz
aunpack archive.tar.gz
 
# view file from the archive
atool -c archive.tar.gz file1.txt
acat archive.tar.gz file1.txt
 
# list files in the archive
atool -l archive.tar.gz
als archive.tar.gz
 
# repacks archive
arepack archive.tar.gz archive.7z
 
# shows the difference between 2 archives
atool -d archive1.7z archive2.7z
adiff archive1.7z archive2.7z
 
# adding '-S' to any command is for simulation only, it will show what will be done without filesystem changes
atool -x -S archive.tar.gz
 
( DTRX )
# DTRX - (d)o (t)he (r)ight e(x)traction

# extract archive
dtrx archive.tar.gz
 
# set date/time
date --set="1 JAN 2018 18:00:00"
date +%Y%m%d -s "20171128"
date +%Y-%m-%d -s "2017-11-28"
date +%T -s "10:13:13"
date -s "$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)Z"
 
# add 7 days and print in ISO format
date -d "2017-12-04 7 days" +%Y-%m-%d
 
# set hardware clock
hwclock --set --date="2017-04-19 20:45:05" --utc
 
# set current time zone
cp /usr/share/zoneinfo/Europe/Paris /etc/localtime
 
# what time is now according to "National Institute of Standards and Technology"
nc time.nist.gov 13
 
( CREATE ISO FILE )
# create ISO from directory
mkisofs -r -o data.iso folder
mkisofs -lJR -o data.iso folder
mkisofs -V Label123 -J -r -o data.iso folder
genisoimage -o data.iso folder
 
# create ISO from directory with Rock Ridge extension
genisoimage -o data.iso -R folder
 
# create ISO from directory with Rock Ridge extension owned by 'root' with read permission
genisoimage -o data.iso -r folder
 
# create ISO from list of mp3 files
mkisofs -r -o data.iso *.mp3
 
# create ISO from CD/DVD
dd if=/dev/cdrom of=data.iso
 
# create bootable ISO
mkisofs -V Label123 -J -r -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -o slackware.iso folder
 
( EXTRACT ISO FILE )
# extract ISO file
mount -t iso9660 -o loop file.iso /mnt
# then 'cd /mnt' and 'cp -R * /path/to/folder/'
 
# extract ISO file
7z x file.iso
 
( BURN ISO FILE )
# burn ISO file
growisofs -dvd-compat -Z /dev/sr0=file.iso
 
# burn ISO file
dd if=/path/image.iso of=/dev/sr0
 
# burn ISO file. Find the DVD device with: wodim --devices
wodim -eject -tao speed=0 dev=/dev/sr0 -v -data file.iso
 
# burn ISO file. Find the DVD device with: cdrecord -scanbus
cdrecord -v dev=ATA:1,0,0 driveropts=burnfree -dao file.iso
cdrecord dev=/dev/sr0 file.iso
 
( BURN DIRECTORY )
# burn directory
growisofs -Z /dev/sr0 -R -J folder
 
# burn directory - create multi session CD/DVD disk
growisofs -M /dev/sr0 -R -J folder
 
# replace newlines with space
cat file1.txt | sed ':a;N;$!ba;s/\n/ /g'
 
# remove all leading blank lines at the top of a file
cat file1.txt | sed '/./,$!d'
 
# remove all trailing blank lines at the bottom of a file
cat file1.txt | sed -e :a -e '/^\n*$/{$d;N;};/\n$/ba'
 
# remove both leading and trailing blank lines
cat file1.txt | sed -e :a -e '/./,$!d;/^\n*$/{$d;N;};/\n$/ba'
 
# delete 'start' plus the next 4 lines
cat file1.txt | sed '/start/,+4d'
 
# print file content in reverse order
tac file1.txt
cat file1.txt | sed -n '1!G;h;$p' file1.txt
 
# add line number for all non-empty-lines in a file
sed '/./=' file1.txt | sed 'N; s/\n/ /'
 
# substitute /usr/bin/ to /usr/bin/local, '&' matches what is found
cat file1.txt | sed 's@/usr/bin@&/local@g'
 
# get the list of usernames
sed 's/\([^:]*\).*/\1/' /etc/passwd
 
# remove duplicate lines
cat file1.txt | awk '!($0 in array) { array[$0]; print }'
awk -i inplace '!($0 in array) { array[$0]; print }' file1.txt
# will modify file1.txt
 
# replace 'OLD' with 'NEW' only if OLD is found in the 3d column
cat file1.txt | awk '{gsub(/OLD/,"NEW",$3); print}'
awk -i inplace '{gsub(/OLD/,"NEW",$3); print}' file1.txt
# will modify file1.txt
 
# replace 'OLD' with 'NEW' only if OLD is found in the 4th line
cat file1.txt | sed '4s/OLD/NEW/g'
cat file1.txt | awk 'NR==4{gsub(/OLD/,"NEW")};1'
cat file1.txt | perl -pe 's/OLD/NEW/g if $.==4'
awk -i inplace 'NR==4{gsub(/OLD/,"NEW")};1' file1.txt
# will modify file1.txt
 
# replace multiline text, Evian + God >> Naive + Dog
sed -n '$!N;s@Evian\nGod@Naive\nDog@;P;D' file1.txt
# 2 lines
sed -n '1{$!N};$!N;s@Evian\nGod\nLive@Naive\nDog\nEvil@;P;D' file1.txt
# 3 lines
sed -n '1{$!N;$!N};$!N;s@ ... @ ... @;P;D' file1.txt
# 4 lines
 
# sort a file
sort file1.txt
 
# sort a file in descending order
sort -r file1.txt
 
# sort file by 3rd column
sort -t: -k 3n file1.txt
 
# open file with vim and goto line 113
vim +113 file1.txt
 
# open file with vim and go to the line containing "search-term"
vim +/search-term file1.txt
 
# open file in readonly mode
vim -R file1.txt
 
# print lines, where column#3 == column#4
cat file1.txt | awk -F':' '$3==$4'
 
# find "token" in file, regardless the case
grep -i "token" file1.txt
 
# find "token" in directory and all subdirectories
grep -r1 "token" ./
 
# left trim
echo " Capella" | sed 's/^ *//g'
 
# right trim
echo "Arcturus " | sed 's/ *$//g'
 
# padding left
printf "%20s" "Sirius"
# "              Sirius"
 
# padding right
printf "%-20s" "Orion"
# "Orion              "
 
# "123 | 456 | 789" => "321 | 654 | 987"
echo "123|456|789" | tr "|" "\n" | rev | paste -sd "|"
 
# parenthesize first character of each word
echo "The Planet Earth" | sed 's/\(\b[A-Z]\)/\(\1\)/g'
# (T)he (P)lanet (E)arth
 
# make big numbers more readable by adding commas: 2342355 >> 2,342,355
echo "123456789" | sed 's/\(^\|[^0-9.]\)\([0-9]\+\)\([0-9]\{3\}\)/\1\2,\3/g'
 
# create random string
tr -dc A-Za-z0-9_ < /dev/urandom | head -c 20 | xargs
 
# print color text in console. You can replace 31 with anything in range 31........47 and 1m with 1m......9m
echo -e "\033[31;1mRED COLORED TEXT\033[0m"
 
# mogrify comes with 'Image Magick'
mogrify -resize 1600 *.jpg
# resize JPG files with width=1600
mogrify -resize '1280x1024>' *.jpg
# resize only if width or height is larger than the limits
mogrify -format jpeg *.tiff
# convert all TIFF files in a direcory to JPEG format
mogrify -format gif *.jpg
# convert all JPEG files in a direcory to GIF format
mogrify -rotate 90 *.jpg
# rotate images
mogrify -monochrome *.jpg
# convert all JPEG files to black and white   
mogrify -colors 4 *.gif
# reduce colors of all GIF images to 4
 
# convert 'WAV' file to 'MP3' file
ffmpeg -i file.WAV -codec:a libmp3lame -qscale:a 2 file.MP3
# high quality
ffmpeg -i file.WAV -vn -ar 44100 -ac 2 -ab 192k -f mp3 file.MP3
lame file.WAV --cbr -b 256 file.MP3
# 256 is for high quality, 128 would be for less quality
ls *.WAV | xargs -t -I {} lame "{}" --cbr -b 256
# converts all WAV files in a directory
 
# show video file information
mplayer -frames 0 -identify file.AVI
ffmpeg -i file.VOB
# find the audio/video streams
ffmpeg -codecs
# full list codecs
 
# merges 2 video files
mencoder -ovc copy -oac copy part1.AVI part2.AVI -o full_movie.AVI
 
# convert VOB to MKV format
ffmpeg -i file.VOB -fflags +genpts -c:v copy -c:a copy file.MKV
ffmpeg -i file.VOB -c:v libx264 -an file.MP4
# good quality, very hard to detect the difference from source
ffmpeg -i file.VOB -c:v libx264 -map 0:1 -c:a libfdk_aac -b:a 192k -map 0:4 file.MP4
avconv -i file.VOB file.MKV
avconv -i file.VOB -vcodec libx264 file.MKV
avconv -i file.VOB -c:v copy -c:a copy file.MKV
 
# convert VOB to MP4 format
ffmpeg -i file.VOB -q:v 3 file.MP4
# with -q:v you can adjust the video quality
ffmpeg -i file.VOB -c:v copy -c:a copy file.MP4
ffmpeg -i file.VOB -acodec libmp3lame -ac 2 -ar 22050 file.MP4
# to copy the input audio, use '-acodec copy'
 
# convert VOB to AVI format
ffmpeg -i file.VOB -sameq file.AVI
# same quality output file
ffmpeg -i file.VOB -c:v libx264 -crf 20 -c:a libmp3lame -ac 2 -ab 192k file.AVI
# for quality -crf = 18-29
mencoder file.VOB -oac copy -ovc x264 -x264encopts bitrate=2500 -o file.AVI
mencoder file.VOB -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=1600 -force-avi-aspect 1.7778 -oac mp3lame -lameopts vbr=3 -nosub -idx -o file.AVI
 
# convert MKV to AVI format
mencoder file.MKV -ffourcc divx -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=400 -oac mp3lame -lameopts vbr=3 -o file.AVI
 
# reduce video size
ffmpeg -i file.AVI -s 320x240 -vcodec msmpeg4v2 file-divx.AVI
# compress file to divx format
mencoder file.AVI -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=1600 -force-avi-aspect 1.7778 -oac mp3lame -lameopts vbr=3 -o file-reduced.AVI
 
# extract video piece, in this case from 1:02:41 next 20 seconds; 3600*hours + 60*minutes + seconds
mencoder file.AVI -ss 3761 -endpos 20 -ovc copy -oac copy -o clip.AVI
ffmpeg -i file.VOB -ss 3761 -t 20 -c:v mpeg4 -qscale:v 2 -c:a mp2 -b:a 320k clip.AVI
 
# extract images from video
mplayer -vo jpeg -ss 1:02:41 -frames 48 file.AVI
 
# extract sound from video and save it as MP3
ffmpeg -i file.AVI -vn -ar 44100 -ac 2 -ab 192k -f mp3 sound.MP3
 
# desktop recording
recordmydesktop -x 0 -y 0 --width 200 --height 200 --on-the-fly-encoding -o desktop-200x200.ogv
mencoder -idx desktop-200x200.ogv -ovc lavc -oac mp3lame -o desktop-200x200.avi
 
# capture audio stream
mplayer http://www.dummy-streaming-location.com/path -dumpstream -dumpfile file.MP3 -vc dummy -vo null
 
Hardware: inxi, hwinfo, lshw, lscpu, blkid, dmidecode
HDD Diagnostics: e2fsck, smartctl
Users and Groups: useradd, usermod, userdel, gpasswd
File System: mke2fs, tune2fs, mkntfs, e2label
Manage System: dbus-send, ps, yum, dpkg, apt, pacman
Search Files: find, locate
Archives: tgz, bz2, txz, 7z, rar, xar, cpio, patool, pax, atool, dtrx
Date & Time: date, hwclock
ISO Images: mkisofs, genisoimage, growisofs, cdrecord, wodim
Text Files: cat, tac
String Processing: sed, awk
Multimedia: mogrify, ffmpeg, mplayer, mencoder