linux

Linux Cheatsheet

Find all directories owned by user

find . -type d -user kayals

 

Rename filename with spaces to Underscore

for f in *\ *; do mv “$f” “${f// /_}”; done

 

SORT

du -h –max-depth=1 | sort -hr
du -ch Downloads/ | grep total
du -ch –exclude=’*.mp4′ | grep total
du -csh ostechnix /home/sk/
du -h directory1 directory2

 

Vi/vim search for a pattern and if occurs delete to end of line

:%s/{pattern}.*//
:%s/{pattern}/d$
:g/{pattern}/d$

 

SCREEN

ssh kayals@xx.xx.xx.xx
Screen -Dr (to log in background)
Run command
Ctl A Ctl D to exit background

 

Linux – Find total processes

ps -A –no-headers | wc -l

 

Save a single web page (with background images) with Wget

wget -E -H -k -K -p http://www.example.com/
Also in case robots.txt is disallowing you add -e robots=of

 

Apache – List all modules

apachectl -M | sort

 

MySQL Backup/Restore

mysqldump -u root -p –all-databases > alldb.sql
mysqldump -u root -p –opt –all-databases > alldb.sql
mysqldump -u root -p –all-databases –skip-lock-tables > alldb.sql
Import:
mysql -u root -p < alldb.sql

 

How to find which Process is causing High CPU usage

ps -eo pcpu,pid,user,args | sort -k1 -r | head -10

 

Change MySQL/PhpMYAdmin root password

# mysql -u root -p
# SET PASSWORD FOR root@localhost = PASSWORD(‘YOUR PASSWORD’);
Make sure to set the new password into phpmyadmin’s config.inc.php too, at line
$cfg[‘Servers’][$i][‘password’] = ‘yourpassword’;
Otherwise, phpmyadmin may not work, echoing
Access denied for user ‘user’@’localhost’ (using password: YES)

 

WordPress Files/Folder Ownership

chown -R www-data:www-data *
find . -type f -print0|xargs -0 chmod 644 && find . -type d -print0|xargs -0 chmod 755

 

Search and Replace

find /home/www -type f -print0 | xargs -0 sed -i ‘s/subdomainA\.example\.com/subdomainB.example.com/g’

 

Linux – Find file by size

find . -type f -size +50000k -exec ls -lh {} \; | awk ‘{ print $9 “: ” $5 }’
Remove Command
find . -type f -name “*.pdf” -exec rm -i {} \;

 

Count number of files in sub-directories

find . -type d | \
while IFS= read -r d; do
f=$(ls -F “$d”);
echo “$f” | egrep -q “/$” || \
echo $d : $(echo -n “$f”|wc -l) files;
done

 

SCP multiple files

scp kayals@xx.xx.xx.xx:/var/www/\{a,b,c\} .

 

REMOVE/DELETE FILES –

find . -name “*.mov” -exec rm -rf {} \;
find . -type f -name ‘*.mov’ -delete

 

NETSTAT

netstat -ntu | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n

 

List by directory

ls -l | grep ‘^d’
ls -p | grep “/”

 

Change file/folder permission

chown -R www-data:www-data * && find . -type f -print0|xargs -0 chmod 644 && find . -type d -print0|xargs -0 chmod 755
find . -type f -print0|xargs -0 chmod 644 && find . -type d -print0|xargs -0 chmod 755

 

TREE

ls -R | grep “:” | sed -e ‘s/://’ -e ‘s/[^-][^\/]*\//–/g’ -e ‘s/^/ /’ -e ‘s/-/|/’

 

Strace MySQL dump with output

strace -vv -fF mysqldump -u backup -pPASSWORD stateblog > stateblog-date

 

Remove Apache2 server signature

$ vi /etc/apache2/apache2.conf
Add the below two line
ServerSignature Off
ServerTokens Prod
/etc/init.d/apache2 restart$ vi /etc/php5/apache2/php.ini
expose_php = off
/etc/init.d/apache2 restart

 

PostgreSQL – BACKUP and Restore

1. su postgres
2. run /usr/lib/postgresql/9.4/bin/pg_dump -p 5432 --format custom -f "/var/lib/local/ca-$(date +\%Y-\%m-\%d_\%H:\%M).postgres” ca
3. Dumps are found in /var/lib/local/Step 1: Test the file.ca2:~$ file ca.postgres
ca.postgres: PostgreSQL custom database dump - v1.12-0

Step 2: To restore the file, first drop the current database (if it exists).

ca2:~$ sudo -u postgres dropdb ca

Step 3:  Create a fresh database.

ca2:~$ sudo -u postgres createdb -T template0 ca

Step 4: perform the restoration.

ca2:~$ sudo -u postgres pg_restore -d ca ca.postgres

 

VI Tricks

Join all lines in one – ggVGJ
Insert before beginning of lines – :%s/^/new/g
Insert end of lines – :%s/$/new/g

 

Helpdesk PostgreSQL Dump

pg_dump -h 10.20.10.102 -p 2093 -Fc -o -U HOST DATABASE_NAME > mydatabase.dump