Here lies a list of useful commands I've come across. I'm sure I'll need at least some of them again in the future. This is where they'll stay for now.




No results.

sudo hdparm -Tt /dev/sda
Check hard disk performance.

df -h
Display disk space usage for all mounted drives.

uname -a
Print out Linux kernel version and architecture as well as other useful system information.

cat /proc/sys/vm/swappiness
Print systems swappiness value.

xset dpms force off
Turn off the monitor. Any keypress or mouse action turns it on again.

xev
Find out the code of a button on the keyboard. It also prints out all X events.

netstat -nr
Print the network gateway.

cat /proc/cpuinfo
Print processor information.

unzip -t <archive.zip>
Test and display zip contents without extracting any files.

tar czf <backup.tar.gz> <path_to_archive>
Create tar.gz backup archive.

tar -xzf <backup.tar.gz>
Extract a tar.gz backup archive.

sudo -i
Log in as the root user.

du -hs <path>
Print the size of a directory. Leave out <path> to use the current directory.

crontab -e
Edit tasks run by cron (task scheduling).

sshd -v
See the version number of the ssh server.

tar -cj <path> | openssl des3 -salt > <encrypted_file>
Encrypt a file or directory with bzip2 compression.

cat <encrypted_file> | openssl des3 -d -salt | tar -xvj
Decrypt a encrypted file. It will be saved in the current directory.

which <executable>
Print the path to the installed executable file.

/sbin/mdadm --detail /dev/md0
Print the current RAID status.

tar -ztvf <backup.tar.gz>
List the contents of tar.gz archive without extracting.

watch -t 0.5 <command>
Run and print <command> every 0.5 seconds.

sudo usermod -s /bin/false <user>
Deny <user> shell access by setting their shell to false.

exec bash
Reload the current bash process to load any configuration changes.

cd -
Go back to previous directory.

sudo dmidecode -t 2
Display motherboard info.

sudo dpkg -i *.deb
Install all .deb in the current directory.

scp -r <source_path> <user>@<ip>:<remote_path>
Push file/folder to a remote server.

sudo dpkg-reconfigure tzdata
Change the timezone on a deb based system.

ssh-keygen -H -F <hostname>
Search for <hostname> in the known_hosts file.

tar -czf <backup.tar.gz> --exclude=<PATTERN1> --exclude=<PATTERN2> <path>
Create a tar.gz backup, excluding <PATTERN1> and <PATTERN2>.

find . -name <pattern> -print | xargs sed -i 's/<foo>/<bar>/g'
Find <foo> and replace it with <bar> across multiple files that match the <pattern>. e.g. *.txt

lsof -n -p <pid>
See the files that process <pid> has open.

mysql -u root -p -h localhost database_name < backup.sql
Import backup.sql into database as the mysql root user.

for f in *.wav; do ffmpeg -i "$f" -ab 320k "${f%.wav}.mp3"; done
Encode all .wav files in the current directory to 320Kbps mp3s.

sudo alsactl store
Save the current driver state for the selected soundcard to the configuration file.

lsof +c 0 /dev/snd/pcm* /dev/dsp*
See which processes have taken hold of your sound devices.

date
Print the current time and date.

dd if=/dev/device_name of=image.img bs=4M
Make an exact copy of a drive, such as a hard drive or a SD card from a Raspberry Pi.

sudo !!
Run the previous command with sudo.

mogrify -format png *.bmp
Convert images from one format to another preserving file names. Requires imagemagick.

for file in *.wav; do lame -m m --preset standard "$file" "${file%.wav}.mp3"; done
Convert mono wav files, in the current directory, to mono mp3's at decent quality. and with a small file size.

mongoimport -d <database> -c <collection> --type csv --file <data.csv> --headerline
Import a csv file into mongodb.

ssh-copy-id <user>@<ip>
Copy a ssh public key to a server.

htpasswd <password_file> <username>
Append user to <password_file> for apache2.

mogrify -resize <width>x<height> *.jpg
Resize all jpg images in folder. They will be overwritten.

for f in *.flac; do ffmpeg -i "$f" -q:a 2 "${f%.flac}.mp3"; done
Convert all flac files in the current directory to mp3, with a variable bit rate of 170-210 kbps.

ln -s <source_path> <link_path>
Create a symbolic link to a file or folder.

mongodump --db <database> --collection <collection>
Export a mongodb database in a importable mongodb format.

shntool split -f album.cue album.wav
Split an album, that is contained in one wav file, into separate tracks that are described in a cue file. Requires shntool.

for f in *.png; do convert "$f" -resize <width>x<height> "${f%.png}.jpg"; done
Convert and resize all png images in the current directory to jpg. Requires imagemagick.

find . -mtime 0
Recursively find files that have been modified more recently than 24 hours ago.

dig +short myip.opendns.com @resolver1.opendns.com
Print the external/web/global IP address of the current machine.

rubberband -T 50:100 in.wav out.wav
Speed up a wav file from 50bpm to 100bpm.

for f in *.wav; do rubberband -T 85:90 "$f" "${f%.wav}_stretched.wav"; done
Stretch all wav files in the current directory from 85bpm to 90bpm.

for file in *.wav; do oggenc -q 6 "$file"; done
Convert all wav files in the current directory to Ogg Vorbis.

mongorestore -h 127.0.0.1 --collection <collection> --db <database> <collection.bson>
Restore/import a mongodb collection from a mongodump.

sudo nmap -sP <broadcast_address>/<shorthand_subnet>
Show all nodes on the network.

sudo nginx -c /etc/nginx/nginx.conf -t
Check and test current nginx configuration.

ssh -ND 9999 <user>@<host>
Set up a SSH tunnel to browse the web using <host> as a proxy server.

sudo blkid
Display the UUID and filesystem for all block devices (drives) attached to the machine.

find <path> -iname "<pattern>"
Find all the files in the path (and sub directories, recursively), whose file names match the pattern.

find <path> -iname "<pattern>" | rename 's/<find>/<replace>/'
Rename multiple files recursively, starting at <path> . If a files name contains <find>, replace it with <replace>.

sudo kpartx -a <disk.img>
Mount the partitions of a disk image.

sudo kill -USR1 $(pgrep ^dd)
See progress of a running dd command.

sudo gpasswd -a user group
Add a user to a group.