The contents of this file are released under the GNU General Public License. Feel free to reuse the contents of this work, as long as the resultant works give proper attribution and are made publicly available under the GNU General Public License.
- Reference: ** Basics: *** Getting help:
man command
command -h
apropos word
info
*** Command Line Utilities: **** Basic File and Directory Operations:
pwd
ls
Show maximum information about all files, including hidden:
ls -a
ls -R
ls -lt
Move/rename a file or directory (be careful that you don't move the source over a destination with the same name):
mv source destination
rm target
cp source destination
mount /dev/device_name /media/device_name
umount /media/device_name
Forensically clone filesystems and do other low-level operations on files. Be careful with this one. Can be destructive:
dd
parted
mkfs
**** System Administration:
Execute command as an administrator (can be destructive/insecure. Use only for system administration tasks):
sudo command
sudo -s
exit
Forgot to type sudo in front of a command and already hit enter? Repeat the last command using sudo:
sudo !!
***** Installing software from a .tgz (also known as a tarball):
cd software_directory
Always read README first if it is provided, in case there are any modifications to the procedure outlined below:
cat README
./configure
make
make install
make clean
***** Ubuntu/Debian Software repositories:
sudo apt-get update
sudo apt-get upgrade
apt-cache search keyword
apt-cache show package_name
sudo apt-get install package_name
sudo apt-get remove package_name
sudo apt-get purge package_name
sudo apt-get autoclean
sudo apt-get clean
du -sh /var/cache/apt/archives
command | less
**** Working With Files:
cat file
locate filename
which appname
grep phrase filename
command | grep phrase
**** Working With Processes:
ps -e
top
htop
renice process_name
pkill process name
command &
nohup command &
jobs
fg OR %1 OR %2
**** Compression and Encryption:
tar -cvzf backup_output.tgz target_files_or_directories
tar -xvf target.tgz
gpg -o outputfilename.gpg -c target_file
gpg -o outputfilename -d target.gpg
gpg-zip -o encrypted_filename.tgz.gpg -c -s file_to_be_encrypted
*** The Bash shell: **** File Name expansions:
~/
./
../
../../
/*
**** Output Redirects:
command_1 | command_2
command_1 | command_2 | command_3
command > file
file > file
file < file
file_or_command >> file
tee target
command > /dev/null 2>&1
**** Controlling Execution:
command_1 ; command_2
command_1 ; command_2 ; command_3
&& acts like ; but only executes command_2 if command_1 indicates that it succeeded without error by returning 0.
command_1 && command_2
command_1 || command_2
**** Bash Wildcards:
phrase*
phrase
?
[chars]
[a-z]
** Advanced: *** Command Line Utilities, Continued: **** Networking:
ifconfig
iwconfig
ssh username@ip_address
ssh -X username@ip_address
scp -r source_filename:username@ip_address target_filename:target_username@target_ip_address
Copy only changes between files or directories (super efficient way to sync directories, works either locally or with remote servers using username@ip_address:optionalport, just like ssh):
rsync source target
ping ip_address
traceroute6 ip_address
netstat
iptables -L
nc -v portquiz.net
nmap localhost
nmap -sP -T Insane 192.168.1-254
***** curl:
curl -O http://example.com/file.txt
curl -O http://example.com/file[1-33].txt
curl -o output.txt http://example.com/file.txt
curl -C - -O http://example.com/file.mp3
curl --mail-from [email protected] --mail-rcpt [email protected] smtp://mailserver.com
***** wget:
wget http://example.com/folder/file
wget -c http://example.com/folder/file
wget -b wget -c http://example.com/folder/file
wget -rkp --level=1 http://website.com
wget --ftp-user=USER --ftp-password=PASS ftp://example.com/folder/file
***** netcat:
nc -l recieving_port > file_copied
command | nc -w number_of_seconds_before_timeout target_ip target_port
Use tar to compress and output a file as a stream, pipe it to a target ip and port over the network:
sudo tar -czf - filename | nc -w number_of_seconds_before_timeout target_ip target_port
**** Users and Groups:
chown user_name:group_name directory_name
chmod
adduser
usermod
deluser
groups
groupadd
groupmod
delgroup
su username
users
talk
Interactive talk program to talk to other users from terminal (must be installed from repositories.):
ytalk
**** Working With Files, Continued:
lsof
diff file_1 file_2
head -n number_of_lines file
tail -n number_of_lines file
md5sum file
md5deep directory
sha1sum
sha1deep
watch -d -n number_of_seconds command
time command
du -a directory | sort -n -r | less
rename -n 's/[\s]/''/g' *
rename 'y/A-Z/a-z/' *
***** Environment and Hardware:
dmidecode
date
echo $HOSTNAME
lsb_release -a
more /etc/issue
uname -a
lsmod
modprobe
dpkg --get-selections
dpkg -L pkg | grep /usr/bin
printenv
lspci
lsusb
sudo dmidecode
dumpcap
dumpkeys
***** Ubuntu System Administration, Advanced (Continued):
add-apt-repository
sudo dpkg -i package.deb
**** Python:
pip install -U pip
pip search library_name
create a virtual python environment to allow install of many different versions of the same Python modules:
virtualenv dirname --no-site-packages
source dirname/bin/activate
deactivate
pip install packagename==version_number -E dirname
pip freeze -E dirname > requirements.txt
pip install -E dirname -r requirements.txt
**** git (all commands must be performed in the same directory as .git folder):
git init git config user.name "user_name" git config user.email "email"
git remote add origin https://github.com/username/project.git
git remote add origin [email protected]:dskrad/bin.git
Make a copy of a git (target can be specified either locally or remotely, via any number of protocols):
git clone target
git commit -m "message"
git status
git log git log --summary git log --oneline git log --graph git log --after= --before=
git log -p
git ls-tree
git pull [target]
git push [target]
git push -u origin master
git branch
git branch [branchname]
git checkout [branchname]
git branch -d [branchname]
git push origin :branchname git push origin --delete branchname
git merge [branchname] [branchname]
Remove the specified file from the staging area, but leave the working directory unchanged. This unstages a file without overwriting any changes.
git reset
Reset the staging area to match the most recent commit, but leave the working directory unchanged. This unstages all files without overwriting any changes, giving you the opportunity to re-build the staged snapshot from scratch.
git reset
Reset the staging area and the working directory to match the most recent commit. In addition to unstaging changes, the --hard flag tells Git to overwrite all changes in the working directory, too. Put another way: this obliterates all uncommitted changes, so make sure you really want to throw away your local developments before using it.
git reset --hard
git checkout -- file.txt
Move the current branch tip backward to , reset the staging area to match, but leave the working directory alone. All changes made since will reside in the working directory, which lets you re-commit the project history using cleaner, more atomic snapshots.
git reset
Move the current branch tip backward to and reset both the staging area and the working directory to match. This obliterates not only the uncommitted changes, but all commits after , as well.
git reset --hard
*** Virtualization:
vboxmanage clonehd virtual_machine_name.vdi --format VDI ~/target_virtual_machine_name.vdi
you need to make sure you have the right kernel modules. You can do this with modprobe, but this package works instead in a ubuntu-specific way.
sudo apt-get install virtualbox-ose-guest-utils
sudo mount -t vboxsf name_of_shared_folder_specified_in_Virtualbox path_of_mountpoint
*** mysql:
help
show databases;
use database_name_here;
show tables;
DROP DATABASE databasename;
CREATE DATABASE databasename;
CREATE USER username@localhost IDENTIFIED BY 'password';
select * from mysql.user;
delete from mysql.user WHERE User='user_name';
Give user access to all tables (make them root). the "%" means that they can sign in remotely, from any machine, not just localhost.:
grant all privileges on . to someusr@"%" identified by 'password';
grant select,insert,update,delete,create,drop on somedb.* to someusr@"%" identified by 'password';
flush privileges;
use mysql;
update user set password='password'('newpassword') where User='user_name';
mysqldump databasename > dumpfilename.txt
mysql -u username -p < dumpfilename.txt
mysqldump -u username -p --opt databasename > dumpfile.sql
mysql -u username -p --database=databasename < dumpfile.sql
*** ffmpeg or avconv
ffmpeg -i input.mp4 -acodec copy -vn output.m4a (or mp3 if that is the audio track encoding)
ffmpeg -i largefile.m4a -t 00:04:15 -acodec copy smallfile.m4a ffmpeg -i largefile.m4a -ss 00:04:15 -acodec copy newlargefile.m4a
ffmpeg -i input.flv -vcodec copy -acodec copy output.mp4