### Terminal Shortcuts
Ctrl + l Clear
Ctrl + a Moving to the beginning of a line
Ctrl + e Moving to the end of a line
Alt + f Move one word forward in a line
Alt + b Move one word backward in a line
Alt + t Swaps two words
Ctrl + K Killing line to the right
Ctrl + U Killing line to the left
Alt + D Killing a word from the cursor point.
Ctrl + W Killing a word to the left to the beginning of the word
Ctrl + Y Yanking (Kinda reviving a line/word that was killed)
Ctrl + R Search the history
### LINUX Commands
#print environment vars `printenv | grep HOME` OR ` echo $USERNAME`
#Define custom shell variable
`variable =value // Shell variables are local. Only exists in current shell session`
#Define custom environment variable
`export <variablename> // export CONNECTIONSTRING=http://api.intetrnal.com`
#Startup files
`/etc/profile` - all users global profile
`~/etc/bash.bashrc` - Non login related . Global config for all users.
`~/.bashrc` - Each specific users home directory. We customize this for aliases, custom prompts etc for the given user.
#aliasing
`alias pig=echo OINK` # whenever you type pig OINK will be output
`type pig` #will show what the alias is about.
#Define the above in ~/.bashrc to make it always available.
### Date Time
```
date # prints current date/time
## Note that the arguments are positional (Order does matter)
cal # horizontal calendar
cal july 1969
ncal #vertical calendar with current day selected
ncal july 1969
ncal 1999
```
`echo` # echos what you type after the commands
`sort <filename>` # sorts and prints the contents of the file
### Accessing Manual pages
`man <command>`
Press q to exit
f to forward by one page
b to backward by one page
up/down arrows to line by line
SPACE forward by one window/page
/ -- use to search man pages
### Searching Manual
Example : man -k dog # searches for section that contains dog
For shell built-ins you may not find man pages. In that scenario use `help <commandname>`
`ls` # list contents of the directory
`history`
`history | less` # => History in pages
`Ctrl + R ` # search history.
`head` # Show first 10 lines
`tail` #Show last 10 lines
`tail -n 4` #Specific # of lines
`tail -f <filename>`
### Redirecting
Numeric file descriptor for Standard Streams
stdin 0
stdout 1
stderr 2
Examples
`ls 1> list.txt`
`ls zzz 2> error.txt`
Redirecting stdout and stderror n single statement
`cat bees.txt ants.txt > insects.txt 2>> error.log`
Redirecting stdout and stderror to the same destination
`ls > output.txt >2 output.txt` OR `ls > output.txt 2>&1` (latter is just a syntactic sugar)
### Piping
Examples
`ls -l /usr/bin | less` # Pass the output of ls -l to less
`date | rev` # Pass output of date to rev command
`ls /usr/bin -1 | wc -l` # count the number of files.
`ls /usr/bin -lh | sort -k5h |tail -3` # List three largest files in a given folder.
`du -ha /usr/bin/ | sort -h | tail -3` # Same as above. Better
### Expansions
Pathname expansion `echo *`
Wild Card : ? `ls *.???` # list all files that has three char extensions.
Range Wild Card:
`ls app[12].css` #Result: app1.css app2.css
`ls [a-z]*` # Result: files that begins with lowercase alphabet filename.
`ls [^a]*` # Result file list that does not begin with lower case a
### Finding files
`locate` #uses a database. Requires a search pattern
`find` # slower. But more powerful and realtime
# find with no arguments will list every single file in the directory and sub directories.
`find /` # willl start listing every single file in the system
`find -type f` # find only files
`find -type d` # find only directories
`find ~/Desktop -name *.txt` # find pattern based
`find ~ -type f -name "*.txt"`
`find ~ -type f -iname "*std*"` # find with case insensitive name search.
`find ~/Desktop -type f -name "*[0-9]*"` # any file that contains a number in it's name.
`find / -type f -size +1G` # find files that are bigger than 1G in size`
`find / -user ponting` # find file that are owned by certain users
`find ~/Desktop -empty -type d` # find empty folders
`find -mmin +30` # find files modified 30 minutes ago.
`find -mmin -30` # modified in 30 minutes
`find -mmin 30` # modified exactly 30 minutes ago
`find -type f -not -name "*.pdf"` # Find files that are not .pdf
`find -empty -exec ls -l '{}' ';' # ls
### Timestamp
mtime # modificationtime ex: ` ls -l`
ctime # change time. Rename move etc.` ls -lm`
atime # Last access time. ` ls -lu`
### Grep : Search inside files || text
`grep "chicken" animals.txt`
`grep bequeath SongOfMyself.txt`
`grep -w "I" SongOfMyself.txt` # search for I. But will include It as well.
`grep -w "I" SongOfMyself.txt -i` # Case insensitive search
`grep -w "I" SongOfMyself.txt` # Word search
`grep -r "9198027949" ~/Desktop` # Find across files ina particular folder / recursively
`grep "myself" SongOfMyself.txt -ic` # How many times myeslf is used
`grep "I" SongOfMyself.txt -iwc` # How many times I is used as a word
`grep "wagon" SongOfMyself.txt -A2` #include two lines after
`grep "wagon" SongOfMyself.txt -B2` #include two lines before
`grep "grass" SongOfMyself.txt -wn` # include line number as well
`grep "grass" SongOfMyself.txt -nC2` # show grass , 2 lines before and after and include line numbers.
### Regular Expressions
`. matches any single chcaracter`
`^ matches the start of a line`
'$ matches the end of a line'
'[abc] matches any character in the set'
'^[abc] matches any character NOT in the set'
'[A-Z] matches any characters in a range'
'* Repeat previous expression`
`\ Escape meta characters`
### Cron Jobs
`crontab -e`