Bash tricks – Some of the useful bash shortcuts
The bash tricks list:
Quick
cd
to previous directorycd -
Set the default text editor
echo ‘export EDITOR=“vim”’ >> ~/.bashrc
Change the default history size (number of commands that are saved)
echo “HISTSIZE=2000” >> ~/.bash_profile echo “HISTFILESIZE=2000” >> ~/.bash_profile
Find and run a command from history
history | grep search_keyword !n #where n is the command number
or you can use
CTRL-r
Run the last command
!!
Delete the actual line
CTRL-u
and paste it backCTRL-y
Exit bash without saving history
kill -9 $$
List all in directory sorted by modify date
ls -latr
Preview a command in an interval
watch <command />
Create new nested directories at once
mkdir -p /tpm/this/and/that/will/be/created
Find files and execute a command on them
find . -name ‘*.php’ | xargs wc -l
Loop through a command numerous times
for i in {1..10} ; do touch filenumber${i} ; done; #will create 10 files named file_number_1 .. file_number_10
Shorter way of doing above :
touch file_number_{1..10}
Synchronize a local direcotry with a remote one
$ rsync -r -a -z -v -e “ssh -l remoteuser” –delete /local/path host.name:/remote/path
Delete all files in directory (shortcut for directories with large amount of files)
mkdir empty rsync -a –delete empty/ target_directory/
Print file by columns using awk
awk -F”delimiter” ‘{ print $1 }’ /path/to/file
It will print first column, determining columns by “delimiter”
Real life exapmle, print all usernames in system:
cat /etc/passwd | awk -F":" '{ print $1 }'
Replace Xth column in a row using awk
awk -F”delimiter” ‘$X=“replace”’ /path/to/file #X - is the number of column
It will print first column, determining columns by “delimiter”
Real life exapmle, print all usernames in system and replace the password column with a dash:
cat /etc/passwd | awk -F":" '{ $2=" - "; print $1 $2 }'
Simple way to change date:
date +%Y%m%d -s “20081128”
Simple way to change time:
date +%T -s “10:13:13”
Grep recursively :
grep -R “searchword” /path/to/search
Examine local network:
nmap -n -sP 10.0.0.0/24
Find out which directories consume most of the disk space:
find / -mount -size +1024k -type f -exec ls -la {} \;|sort -rnb -k 5 | less
Quick & dirty way to reboot
kill -9 0
If You want more, check this cheatsheet.
BASH / Redline Shortcuts
ALT + .
– Use the last word of previous commandCTRL + t
– Auto correct misspelled wordCTRL-x CTRL-e
– Edit a command / script in default editorCTRL + u
– Cuts text up until the cursor.CTRL + k
– Cuts text from the cursor until the end of the lineCTRL + y
– Pastes textCTRL + e
– Move cursor to end of lineCTRL + a
– Move cursor to the beginning of the lineALT + f
– Jump forward to next spaceALT + b
– Skip back to previous spaceALT + Backspace
– Delete previous wordCTRL + W
– Cut word behind cursorShift + Insert
– Pastes text into terminal