Useful Linux commands

General

ssh into server

 ssh username@host

display manual for myprogram

 man myprogram

display help file for myprogram

 myprogram --help

go to home directory

 cd

navigate to some_path under your home directory

 cd ~/some_path

show current directory path

 pwd

print out permissions octal notation for myfile.txt

 stat -c '%n %a %A' myfile.txt

change permissions on myfile.txt

 chmod 777 myfile.txt

change group of myfile.txt to group

 chgrp group myfile.txt

change owner and group of myfile.txt

 chown user:group myfile.txt

list files, display human readable and sort by time

 ls -hlt

calculate size of folder foo and its subfolders

du -sh foo

search for the word foo in the output of ls

 ls | grep "foo"

display number of matches to the word foo

 ls | grep "foo" | wc -l

find files matching a string

 find ./* -name '*.png'

search recursively in files for "my phrase" (case insensitive) where file names match "*.txt" and list matching file name and line number of each result

 grep -ir "my phrase" *.txt

copy myfile.txt

 cp /home/user/myfile.txt /usr/local/

rename myfile.txt to myfile2.txt

 mv myfile.txt myfile2.txt

move myfile.txt one directory below and rename it

 mv myfile.txt ../myfile2.txt

forcibly remove all files and directories recursively starting at current location

 rm -f -R ./*

search for "my phrase" in all files starting at current location

 grep "my phrase" ./*

search for "my phrase" in all files and subdirectories starting at the current location

 grep -r "my phrase" ./*

list all files contained in mydir starting with the word foo

 ls -R mydir/foo*

show all running processes with java somewhere in the process string

 ps ax | grep java

kill running process 1234

 kill 1234

show where apache is installed

 whereis apache

show version number of apache

 which apache

display location of files related to apache

 locate apache

display network settings

 ifconfig

transfer a local file to another machine

 scp my-local-file.txt user@host:/path/on/server/

transfer a file from another machine to my machine

 scp user@host:/path/on/server/myfile.txt /local/path/

decompress a tar file that was compressed with gzip

 tar xvzf myfile.tar.gz

download myfile.txt from foo.com

 wget http://foo.com/myfile.txt

run myprogram

 ./myprogram

vim

create a new file or open an existing called myfile.txt

 vim myfile.txt

search for "foo"

 /foo

show next result

 n

move to bottom of document

 shift+g

enter insert/edit mode

 i

return to default view mode

 esc

quit

 :q

quit and don't save

 :q!

write and quit

 :wq

sql

login to sql using username "root" and password "root123"

 sql -uroot -proot123

list available databases

 show databases;

load database "foo"

 use foo;

list tables in database

 show tables;

show structure of table "bar"

 describe bar;

display number of records in "bar"

 select count(*) from bar;