Skip to main content

Useful Linux Commands

I find myself searching for these commands more often than I should. So, until my mush brain remembers these I'll write them down here.

Archive a directory

tar -zcvf <archive_filename>.tar.gz /DIR/You/WANT/TO/ARCHIVE </archive_filename>

Extract .tar.gz file

tar -xvzf {filename.tar.gz}

MySQL

Backup

mysqldump -u {USER} -p {DATABASE_NAME} &gt; {FILENAME.sql}

Restore

 mysql -u {USER} -p {DATABASE_NAME} &lt; {FILENAME.sql}

SCP

Copy local file to destination  scp {LOCAL.file} {USER}@{SERVER.COM}:{~/path/to/destination} Copy remote file to local with .pem file  scp -i {/path/to/pem.file} {USER}@{SERVER.COM}:{~/path/to/file} {/path/to/local/file} ** Note: don't include the braces in these commands.

Compile all access statistics from Jul 4-6 2013

find ./*/statistics/logs/access_log* -mtime -5 -exec zgrep -E "0[4-6]/Jul/2013:" {} \; &gt; /root/Jul_04-06_logs.txt

Find files modified within the last 7 days

find /var/www/vhosts/*/httpdocs -type f -mtime -7 find /var/www/vhosts/*/httpdocs -type f -mtime -7 | xargs ls -lct &gt; /root/recent_modified.log

Find files modified within the last 5 days sorted by newest first

find /var/www/vhosts/*/httpdocs/ -mtime -5 -type f -print0 | xargs -0 ls -al | sort -r

Count IP addresses in Apache log files

cat access.log | cut -d ' ' -f 1 | sort | uniq -c | sort -n

Compile a specific VHOST from the Apache 'other vhosts' Log files

find . -type f -exec grep 'VIRTUAL_HOST' '{}' \; &gt; VIRTUAL_HOST.txt
Resources: - http://askubuntu.com/a/25348

Sum the file size from the ls command

ls -l | grep -e 'filename' | awk '{COUNT+=$5} END {print COUNT}'

Find the port number that a process is using

netstat -ltnp | grep -w ':80'
netstat -ltnp | grep '20000'


Comments