Linux Cheat Sheet

cpu usage

top

summarize size of folder:

du -sh /path/to/director

Find files changed in the last 7 days

find . -type f -mtime 7

Delete all files with a specified extention (or name for that matter)

find . -name "*.bak" -type f -delete

But use it with precaution. Run first:

	find . -name "*.bak" -type f

to see exactly which files you will remove.

Find a file:

find / -type f -name <filename> # / is the top dir, ./ is the local dir

find / -type f -iname <filename*>

find / -type d -name <foldername>

find . -name <filename>

skip “type” for files and directories

locate -i "<filename*>"   // example locate -i "*.txt"

might need to run updatedb

Look for a string within a file:

grep -r -i "some string" /home/yourusernamethype/foo.cpp

This will find every file with .log as extension and apply the grep command.

find . -name "*.log" | xargs grep -i 'the only string im looking for'
find . -name "*.log" | xargs grep -E 'fatal|error|critical|failure|warning|'

Convert tabs to spaces in select files

find . -name '*.cpp' ! -type d -exec bash -c 'expand -t 4 "$0" > /tmp/e && mv /tmp/e "$0"' {} \;

process info

px -aux

process info for specific process, for instance, an app named “hwServer”

ps -aux | grep ./hwServer

Is your vnc running?

ps -elf | grep vnc

tar-ing

tar -C /destFolder -xvf myfile.tar.gz

untar-ing

tar -xvf myfile.tar.gz
tar -xjvf myfile.tar.bz2

ownwership

sudo chown brad foo.txt  # changes owner of file to brad
chown :brad foot.txt  # changes group of a file to brad

Can’t remove a file????

Could be that the file is “immutable” – check with:

lsattr filename

If one of the attributes is “i” then it is immutable. To remove this, use

	sudo chattr -i filename

then

	rm filename

To copy a file from B to A while logged into B:

scp /path/to/file username@a:/path/to/destination   

example:

scp /home/brad/readme.txt root@192.168.242.147:/root

To copy a file from B to A while logged into A:

scp username@b:/path/to/file /path/to/destination

Removing damaged apt-get packages-m

sudo dpkg --remove --force-remove-reinstreq name_of_pkg

creates the necessary links and cache to the most recent shared libraries…the cache is used by the run time linker.

ldconfig 

************** More searches ***********

grep -rnw '/path/to/somewhere/' -e "pattern"

-r or -R is recursive,

-n is line number, and

-w stands match the whole word.

-l (lower-case L) can be added to just give the file name of matching files.

-i ignore case

Along with these, –exclude, –include, –exclude-dir or –include-dir parameters could be used for efficient searching:

This will only search through the files which have .c or .h extensions:

grep --include=*.{c,h} -rn '/path/to -m/somewhere/' -e "pattern"

This will exclude searching all the files ending with .o extension:

grep --exclude=*.o -rn '/path/to/somewhere/' -e "pattern"

Just like exclude files, it’s possible to exclude/include directories through –exclude-dir and –include-dir parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/:

grep --exclude-dir={dir1,dir2,*.dst} -rn '/path/to/somewhere/' -e "pattern"

NETCAT

To send udp:

nc -u ipAddr port    

example

nc -u 192.168.241.207 7001

To rcve udp:

nc -ul ipAddr (optional) port    

example – listens at localhost (127.0.0.1)

nc -ul 7001     

example

nc -ul 192.168.241.207 7001 

Note that the send command needs an IP addr, whereas receive defaults to localhost if one is not provided.

TCPDUMP

tcpdump -n src 192.168.240.100    
tcpdump -n src 192.168.240.100 and port 22

can also use “dst” or neither. Can specify “port” same way

SSH

Here’s how to install openssh-server:

sudo apt-get install openssh-server openssh-client

Running a process in the background:

When you run a process, then type ctrl-Z, it *SUSPENDS* the process.

Type bg to put it in the background, then disown it to detach it from your shell.

jobs
disown %1
exit

To find out which sub-directories consume how much disk size, use this command:

du -h --max-depth=1 | sort -hr