πŸ’»
Linux

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 . -type f -exec grep "forinstance" '{}' ; -print

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

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.txt - Empty file.txt or create a new file

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

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

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

  • 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]

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

&
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.

kill [kill-signal] [pid]
Using a kill command sends a kill signal: List all kill signals: kill -l. SIGTERM is the default kill signal
kill
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

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

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

#!/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

!!: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

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

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

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

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

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

: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
Last modified 2mo ago
Copy link
Edit on GitHub
On this page
Find text in Files
Find based on file content
Find SUID/GUID and Sticky bit Files
Find files based on modified time
File & Directory Operations
Creating files
Storage Usage
Permission
Easy copy/backup
Processes & PS
Foreground process
Background process
Killing a process
Bash Shell
Cut & Paste in Bash
Cursor movement in Bash
Bash Command Execution:
Spelling errors bash
VI Editor
VI Navigation
Input/Append in VI
VI Commands
Search & Replace in VI
VI Parameters
WSL2