Find text in Files

grep '^.' filename - Get all lines that start with a dot or period
grep -rnw '/path/' -e 'pattern'
  • -r or -R is recursive,
  • -n is line number, and
  • -w stands for match the whole word.
  • -l (lower-case L) can be added to just give the file name of matching files.
  • -e is the pattern used during the search
Along with these, --exclude, --include, --exclude-dir flags could be used for efficient searching:
  • This will only search through those files which have .c or .h extensions:
grep --include=*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
  • This will exclude searching all the files ending with .o extension:
grep --exclude=*.o -rnw '/path/to/somewhere/' -e "pattern"
  • For directories it's possible to exclude one or more directories using the --exclude-dir parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/
grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"

Find based on file content

find . -type f -exec grep "forinstance" '{}' ; -print

Find SUID/GUID and Sticky bit Files

find . -perm /4000 - SUID
find . -perm /2000 - GUID
find . -perm /1000 - Sticky Bit
find . -perm /6000 - Search for both SUID & GUID

Find files based on modified time

find -mmin 5 - Find files modified exactly 5 minute ago (In that minute only)
find -mmin -5 - List all files modified in the last 5 minutes
find -mmin +5 - Find all files modified more than 5 minutes ago
mtime is used to find files modified more than 24 hours ago, ctime can be used to search by file change times, size can also be used.
Operators like -o for or and -not can also be used (and is the default operator)

File & Directory Operations

Creating files

> file.txt - Empty file.txt or create a new file

Storage Usage

du -h --max-depth=1 - Show the size of all sub folders in the current directory


chmod --reference file1 file2 - Makes the permissions of file2 the same as file1, also works with chgrp & chown

Easy copy/backup

cp filename{,.bak} - Quickly backup or copy a file, copies filename to a file named filename.bak
rename 'y/ /_/' * - Replace spaces in filename with underscores

Processes & PS

Syntax: ps -[options]
All users
list the user who started the process or when -u user is specified list the process for that user
Processes not executed by current terminal
List all processes regardless of the terminal; alt: -A
Display parent child relationship
Jobs format
See which program this port belongs to: lsof -i tcp:80

Useful ps variations:

  • ps axjf: Show process relationship
  • ps aux: Show username, pid, cpu & memory, start date, & also the command that started the process.
  • pgrep [process]: Get PID without using ps | grep [process]

Foreground process

Kill a foreground process [SIGINT] stands for sig-interrupt
Sleep a foreground process [SIGSTOP]/[SIGCONT]
fg [job-id]
Bring a background process to foreground
Note: To check sleeping process use jobs command.

Background process

Execute a process in background. Appended at the end of command or script
bg [job-id]
Run a process in background
fg [job-id]
Bring a background process to foreground
Note: To check sleeping process use jobs command.

Killing a process

kill [kill-signal] [pid]
Using a kill command sends a kill signal: List all kill signals: kill -l. SIGTERM is the default kill signal
SIGTERM [Kill a process nicely, process can refuse this]
kill -9
SIGKILL [Kill forcefully]
fuser -k filename
Kill a process that is ocking a file
Killing with process name: pkill [kill-signal] [name]
Kill a process running in port:
  • sudo kill -9 $(lsof -t -i:8080)
  • sudo fuser -k 8770/tcp

Bash Shell

Cut & Paste in Bash

Ctrl+U- Cut everything to the left
Ctrl+W- Cut one word to the left
Ctrl+k - Cut everything to the right
Alt+d- Cut one word to the right
Ctrl+Y- Paste a line cut with Ctrl+U

Cursor movement in Bash

Ctrl+L- Clear the screen and redraw the current line
Ctrl+R- Reverse search history
Alt+F- Go forward by one word
Alt+B- Go backward by one word
Ctrl+A- Go to the beginning of a line
Ctrl+E- Go to the end of a line

Bash Command Execution:

#!/usr/bin/sudo /bin/bash - Execute entire shell script as sudo (This line replaces shabang line in shell script
sudo !! - Execute the last command as root
!whatever:p - Find the last command that begins with whatever, but avoid running it
<space>command - Execute a command without saving it in history

Spelling errors bash

!!:gs/foo/bar - run the previous command, replace every foo with bar
^ehco^echo - Run the previous command, replace first occurence of echo with echo

VI Editor

:w !sudo tee % - Save a file that is opened without sudo

VI Navigation

gg Begning of the file
G Move to the last line in the file
5G Move to line 5 of the file (5 can be any line number)
H Upper left corner (home) of the current screen
M Middle line
L Lower left corner
^ Beginning of line
$ End of line
l Forward a character
w One word forward
b Back one word

Input/Append in VI

a - Append after the cursor
i - Append before the cursor
I - Append at the beginning of a line
A - Append at the end of a line
o - Create new line under the cursor
O - Create new line above the cursor

VI Commands

u - Undo last change
U - Undo all changes on line
:w !sudo tee % - Save a file that is opened without sudo
dd - Delete current line
ndd - Delete n lines of buffer
%d - Delete all lines in a file

Search & Replace in VI

Syntax: :[address]s/old_text/new_text/
Address components
replace the first match of 'foo' with 'bar' on the current line only
replace all matches (`g` flag) of 'foo' with 'bar' on the current line only
replace all matches of 'foo' with 'bar' in the entire file (`:%s`)
ask to manually confirm (`c` flag) each replacement

VI Parameters

:set number - Show line numbers
:set list - Show invisible characters


wsl.exe -d wsl-vpnkit service wsl-vpnkit start - WSL2 with Cisco AnyConnect
wsl -t Ubuntu - Shutdown WSL
wsl --shutdown - Shutdown all WSL distros