Linux cheat sheet for command line tools

Welcome to my personal copy/paste crib sheet for everything I use on Linux. I am far from a guru and it is not my full time job, so sometimes I can’t remember the command and its switches exactly. This is primarily CentOS 6.x. Your mileage may vary.

See anything I should add? Comment below!

Add user and add them to sudoers CentOS as root

adduser username
passwd username
usermod -aG wheel username (case matters)

Full time sudo (you will need to enter the PW again). Why this is isn’t used more often is a mystery.

sudo -i

OS Version (centOS)

cat /etc/centos-release

Get a file (to current dir)

wget http://www.somesite.com/image.jpg
wget http://www.someothersite.com/zipfile.zip

TAR a directory

tar -zcvf [/dir]/[filename].tar.gz [/dir]/[folder-name]

Exclude directory/ies example

tar -zcvf name-of-tar.tar.gz /var/www/dir1 --exclude=/var/www/html/dir1/subdir1 --exclude=--exclude=/var/www/html/dir1/subdir2 etc.

Extract to a directory

tar -zxvf  [/dir]/[filename].tar.gz -C [/dir]

Unzip (ubuntu)

sudo apt-get install unzip
unzip file.zip -d destination_folder

Copy directory

cp -ar [/origin-dir] [/destination-dir]
(add -p to preserve dates/auth on files/folders)
cp -apr [/origin-dir/] [/destination-dir/]

Remove directory and files, no prompt

rm -rf mydir
delete the folder, then recreate it
rm -rf dir_name && mkdir dir_name

Move a file/dir

mv -i [/dir/][file] [/dir/][file] 
mv -i [file] [file-newname]
mv -f [file] [file-newname] No confirmation prompt

Files (like DIR in Windows)

ls -lh [basic DIR command]
add -p to add a / to the end of directories - very useful
ls -lh -p
SORT:
ls --sort=extension
ls --sort=size
ls --sort=time
ls --sort=version
ls --sort=none [default]
ls -lth | head -5 (find 5 latest files in dir, replace head with tail to see earliest)

FIND STRING in Files
grep -rnw '/path/to/somewhere/' -e 'pattern'
-r or -R = recursive
-n = display line number
-w = match whole word
-e = use pattern
-l (lower-case L) display just filename instead of pattern
Also:
--exclude, --include, --exclude-dir or --include-dir
Example:
grep --exclude *.php will exclude files ending in .php
grep --exclude *.{php,doc} will exclude files ending in .php and .doc

Cron (CentOS)

crontab -e
less /etc/crontab
ls -la /etc/cron*
less /etc/cron.daily/filename

Viewing log content (adjust to your needs)

cat access.log | awk '{if ($4 ~ /2017:14/) print $1 }' | sort | uniq -c | sort -nr | head -20

See HDD size, space, and partitions

df -h
du -sh [folder] shows disk usage of that folder

Total HDD size

fdisk -l | grep Disk

View services, Service Stop/Start service, etc, e.g. Apache

chkconfig --list
service --status-all
or
chkconfig --list
service [name] start/restart/stop
service httpd restart
service [service] status
service httpd status
service --status all

Versions of stuff

php -v
php -i [verbose output]

Ouput into a text file example (can be use for many commands)

php -i > [filename].txt
echo "<?php phpinfo(); ?>" | php > phpinfo.txt

Reboot immediately (kill everything)

shutdown -r now
shutdown -h now (shut down the machine and turn it off - DANGEROUS!)

Basic system info

free [RAM info]
free -g [Shows in GB]
top
top -u [username]
top -u apache [not the service name: httpd]

More RAM/Memory commands/tools

Install htop, a better top
yum -y install htop

free -m
vmstat
cat /proc/meminfo

Network

ifconfig -a ifconfig eth0 [up][down] start/stop an interface - remember: you could kill your SSH connection

Get your external IP address from command line

dig +short myip.opendns.com @resolver1.opendns.com

Viewing large file contents

less [filename]
once viewing the file:
CTRL+F – forward one window
CTRL+B – backward one window

Connect to MySQL

mysql -u root -p -h [IP address if remote]

Dump & Restore MySQL DBs

mysqldump –all-databases > [dbname].sql mysqldump [dbname] > [dbname].sql Restore: mysql -u [uname] -p[pass] [db_to_restore] < [backupfile.sql]

Dump all DBS

Use bash for loop script to loop over all the dbs. Don’t forget the -p needs to be immediately followed by the password – no space.  Also, this means you’re going to add your pw to your bash history. You can just put in “-p” but you will then have to enter the password each time mysqldump gets to each db. Make sure the below is a single line when pasting it.

#for DB in $(mysql -u [user] -p[password] -e 'show databases' -s --skip-column-names); do mysqldump -u [user] -p[password] $DB > "$
DB.sql";done

Handy MySQL command list

http://g2pc1.bu.edu/~qzpeng/manual/MySQL%20Commands.htm

MySQLDump no create database

You were looking for this because you forgot to remove the create db statement and overwrote a live db, didn’t you? We’ve all been there.
https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_no-create-db

mysqldump --no-create-db [soucedb] > [backupfile].sql
or
mysqldump --n [soucedb] > [backupfile].sql
check your work:

head -20  [dumpdb].sql (you might need to increase the number of lines)

Finding files with certain permissions (in this case 0777)

find / -name [filename] -or- find /-name *php* (wildcard)
find / -type f -perm 0777 [f=files]
find / -type d -perm 0777 [d=directories]
find /var/www/html -type f -perm 0777 > find777files.txt [make a txt file with file list]
find /var/www/html -type d -perm 0777 > find777dirs.txt [make a txt file with dir list]

Update permissions with find

[For Dirs]
find /[path]/ -type d -exec chmod 755 {} \;
[For Files]
find /[path]/ -type f -exec chmod 644 {} \;

Yum

yum history [outputs list of updates with IDs]
yum history undo 8 [this will undo ID 8's updates]
yum -x 'kernel*' update (excludes doing a kernel update)

head

View top n lines of a file. Change “-10” to “-15” or however many lines you want to read

head -10 bar.txt

WINDOWS: 
cmd > more +10 bar.txt (then use Enter for line by line, space bar page by page) CTRL-C to exit

View end of log with tail

tail [logfile]

tail -f [logfile] (view the logfile in real time. ctrl+c to exit)

Create symlink

 ln -s source_file myfile

Apache Vhosts (view configured vhosts and their files)

CentOS/RHEL

httpd -S
apachectl -S

Ubuntu/Debian/Mint/etc

apache2 -S
apache2ctl -S

Users

adduser username
passwd username
usermod -aG wheel username

Require password change on logon:

chage -d 0 username

SCP

Transfer Directory Local to Remote Server

scp -r /opt/mydir root@example.com:/opt/

Transfer Directory Remote Server to Local

scp -r root@example.com:/opt/mydir /opt/

Permissions management

Numeric permissions table.

Linux Umask explained

List users

cat /etc/passwd

Show numerical permissions instead of letters

stat -c ‘%a – %n’ *

Or make an alias.
alias perms=”stat -c ‘%a – %n'”

Example: perms * will show everything in that folder

List php modules

php -m

php -m > phpmodules.txt

php -m | grep -i reflection

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.