Copy File
cp <source> <destination>
source and destination can be directories.
Recursively:
cp -R <source> <destination>
Move command
mv command is used to rename and overwrite files in directories. To rename a file, set the first argument to the original file name and second argument to the new file name or destination directory.
You can also rename directories.
mv <source> <destination>
Wild cards
cat *.txt ===> cat file1.txt file2.txt ....
cat b{ee,oo}p.txt ===> cat beep.txt book.txt
mv b{ee,oo}p.txt ===> mv beep.txt boop.txt
Delete file:
rm <file-name>
Delete directory
rm -r <dir-name>
Absolute and relative paths
.
and ..
are relative paths.
paths starting with /
are absolute paths
Writing and reading file
echo hello > <file-name> (saves the text hello in file name)
Append to a file:
echo world >> <file-name>
whenever you find yourself typing same sequence of commands several times, consider making a script. Just the put the commands you would normally type into a file and add #!/bin/bash
to the top of the file.
Naming
You can name your bash script without any extension. e.g If I want to make a script named logger, I can just make a file called logger and put my commands in it.
Bash scripts can have an extension of sh but it's not necessary.
e.g logger.sh
#!bin/bash
cd home && rm cool.txt
Permissions
You can not run bash script directly after creating them, first you have to give the script the permission to execute
Give the executable
permission to the bash file.
Usually setting the executable bit on the file does the trick.
Give the permission by following command.
chmod +x <bash-file-name>
Execute Script
To run the bash script you need to provide a relative path for the script in command line. e.g ./logger
You can use arguments in your script.
What ever argument you pass it will be passed to you as environment variables as $1
, $2
, $3
and so on.
For the following script $1
argument will be passed when the user will execute the script.
argument_script.sh
#!/bin/bash
echo $1
Running the above script with ./argument_script hello
, hello will be the argument passed.
If argument is multiple words like hello world
, you can use backticks around the argument. Or you can use $* for the argument.
READ command
if you want to ask the user for input, there is a command called read
. This sets the result in the environment variable.
For the following script, running the script will prompt the user for the input. This input will be saved in the environment variable and will be used from the name given to the variable.
#!/bin/bash
read INPUT
echo $INPUT
Whatever input you'll give will be printed out to the terminal as a result of the above script.
- User (Owner of the file) represented by
u
- Group (Group of the file) represented by
g
- Other (everyone else) represented by
o
To check the permissions use ls -l
command.
how to read permissions
-rwxr-xr-x
-> First 3 bits for u
, Middle three bits for g
, last bits for o
. The -
means the permission is not available.
rwx -> read write executable
Set permissions with chmod
In octal
644 -> ugo
#4 for write
#2 to read
#1 to execute
-rwxr-xr-x
Each character describes a permission. (r)ead, (w)rite, and e(x)cute.
A '-' in place of one of those letters mean the permission is not available.
x is the executable bit => If set for directory, that means you can list the directory contents => If set for file, that means you can run it
Example of setting a permission.
If I want to add a (w)rite permission to the group.
chmod g+w <file>
. (Grant the permission)
chmod g-w <file>
. (Revoke the permission)
If you run a command and it's successfull, the exit code will be 0.
You can read out what the exit code it by $?
echo $?
if test -f cool.txt; then echo true; else echo false; fi
test -f
runs the while loop silently
Bash is built to handle multiple programs in parallel
ctrl + z
puts the process in background, when you want to resume that process typefg
- type
jobs
to see what jobs are running. If you typefg
it'll put the last job in the foreground. - If you want to specify the job, give it an argument like
fg %1
to specify job - You can also use the command
kill
to send the signal to program to end like thiskill %1
. Sometimes this command won't end the program, because some programs don't accept end signal. - End Forcefully -
kill -9 %1
kill %%
kills last job- All signals are like
SIG<name>
- Another way to put program to background is to use
&:
Search for a process by it's name
pgrep <name> -lf (additional info)
Kill by id
kill <id>
Regex is a pattern matching language. Many programming languages and system tools have a regex engine
System tools with regex:
- sed
- grep
- perl
- vim
- less
regex in javascript
- str.split(re)
- str.match(re)
- str.replace(re)
- re.test(str)
- re.exec(str)
/PATTERN/FLAGS
s/PATTERN/REP/FLAGS
i
- case insensitiveg
- match all occurences (global)m
- treat string as multiple liness
- treat string as single line
.
- matches any character?
- zero or one time*
- zero or more times+
- one or more times[]
- character class^
- anchor at the beginning$
- anchor to the end(a|b)
- matcha
orb
()
- capture group(?:)
- non capture group\d
- digit[0-9]
\w
- word[A-Za-z0-9_]
\s
- whitespace[ \t\r\n\f]
Examples
echo 'hello beep boop' | sed 's/b..p/honk/'
Output: hello honk boop
echo 'hello beep boop' | sed 's/b..p/honk/g'
Output: hello honk honk
.
is the wild card
?
- zero or one time*
- zero or more times+
- one or more times
how many times a thing should appear
When to escape meta characters
In some engines you need to escape metacharacters such as +
and ?
. In others you don't.
In javascript and perl, you generally don't need to escape metacharacters. To use grep
and sed
in a similar way, use:
sed -r
grep -E
Character classes are ways of specifiying different characters that can match.
echo 'dog days cats' | sed -E 's/d[ao]/DA/g'
Output: DAg DAys cats
echo 'doooog daaays cats' | sed -E 's/d[ao]/DA/g'
Output: DAooog DAaays cats
you can put quantifiers around character classes
echo 'doooog daaays cats' | sed -E 's/d[ao]+/DA/g'
Output: DAg DAys cats
You can also do a negation of character class That means it will match everything outside of that character class
e.g I want to replace every character other than vowels with *
echo 'replace all non vowels in this sentance' | sed -E 's/[^aeiou]/*/g'
Output: *e**a*e*a****o***o*e***i****i***e**a**e
Remove all digits from stirng
echo 'hello1233 what9999 834958734' | sed -E 's/[0-9]+//g'
Output: hello what
Remove all alphabets from string
echo 'hello1233 what9999 834958734' | sed -E 's/[a-z]+//g'
Output: 1233 9999 834958734
You can mix and match ranges
echo 'HYIUello1233 what9999 834958734' | sed -E 's/[A-Za-z]+//g'
Output: 1233 9999 834958734
echo 'HYIUello1233__ __ what9999 834958734' | sed -E 's/[A-Za-z_]+//g'
Output: 1233 9999 834958734
Practice
A password field with minimum 8 characters, it has to container number, a lower case letter, a capital letter and one of a handful of symbols
Input: pw = 'abc432~A';
/^[\w!@$~]{8,}$/.test(pw) && /[a-z]/.test(pw) && /[A-Z]/.test(pw) && /\d/.test(pw) && /[!~@#$%^&]/.test(pw)
Output: true
\w
- Word character :[A-Za-z0-9_]
\W
- non word character :[^A-Za-z0-9_]
\s
- whitespace :[ \t\r\n\f]
\S
- non-whitespace :[^ \t\r\n\f]
\d
- digit :[0-9]
\D
- non-digit :[^0-9]
^
- anchor at the beginning$
- anchor to the end
Following will replace the word 'beans' if it's at the start of string
echo 'cool beans' | sed -E 's/^beans/BEANS/'
Output: cool beans
Following will replace the word 'beans' if it's at the end of string
echo 'cool beans' | sed -E 's/beans$/BEANS/'
Output: cool BEANS
(a|b) - match a or b
()
capture group?:
non capture group
This is the way of telling the engine, either one of these patterns is valid. These patterns can be multiple characters not just the single characters
echo 'beep boop' | sed -E 's/b(ee|oo)p/XXX/g'
Output: XXX XXX
echo 'beep boop' | sed -E 's/b(e|o){2}p/XXX/g'
Output: XXX XXX
var str = 'cool <beans> zzzz';
var m = /<([^>]+)>/.exec(str);
Output m : [ '<beans>',
'beans',
index: 5,
input: 'coll <beans> zzzz',
groups: undefined ]
m[1] = 'beans'
_You can refer to this capture group by $1_
"cool <beans> zzz".replace(/<([^>]+)>/, "$1");
Output: "cool beans zzz";
"cool <beans> zzz".replace(/<([^>]+)> (\w+)/g, "$1 :$2");
Output: "cool beans :zzz";
In sed
echo 'cool <beans> zzz' | sed -E 's/<([^>]+)>/\1/g'
Output: cool beans zzz
echo 'cool <beans> zzz' | sed -E 's/<([^>]+)>/~~\1~~/g'
Output: cool ~~beans~~ zzz
TODO
The following commands gives pretty handy reference to Regexp.
perldoc perlreref
Vim is a popular text editor
- edit files on a remote server over ssh
- works without graphical desktop environment
- many programming specific features
really fast editing
- Easily change indentation
- Syntax highlighting
- nano
- emacs
- vi
Vim uses ANSI codes to control a cursor and position blocks of text on the screen
- Go to
INSERT MODE
by pressingi
- If you want to go back to command mode, hit
esc
- To quit, from command mode hit
:q
- To save, from command mode hit
:w
- If you want to do saving and quitting at the same time, from command mode hit
:wq
- If you want to quit without saving, from command mode hit
:q!
- If you already opened vim using
vim
command and you want to open a file, hit:o
and name of the file from command mode
You can use the arrow keys to move around.
k
to goup
j
to godown
h
to goleft
l
to goright
You can usek
andj
to goup
anddown
respectively from command mode^
or0
- Move to the beginning of line$
- Move to the end on linegg
- Move to the beginning of the fileG
- Move to the end of file
x
- Delete highlighted character from command moded$
- Delete from current character to the end of lined0
ord^
- Delete from current character to the beginning of lineu
- Undo the operationctrl + r
- Redo the operation
Combine these commands
dj
- delete current and next linedk
- delete current and previous linedgg
- delete from current line to beginning of filedG
- delete from current line to end of file2dd
- delete 2 lines (including current)3dd
- delete 3 lines (including current) and so ondl
- delete character to the rightdh
- delete character to the left
You can search for text using Regular Expressions
From command mode, type in regular expression for the word you want searched like /word
and press enter. You will be skipped ahead of the next occurence of the word you searched.
After searching
n
- To move to the next occurenceN
- To go to the previous occurence
If you want to search backwords try using '?word'
/[0-9]
search for numbers
From command mode, press d and then type the pattern. This will delete everything from current position to that pattern. (not including the pattern)
d/PATTERN
- delete to the next match of the patternd?PATTERN
- delete to the previous match of the patterndn
- delete the next already matched patterndN
- delete the previous already matched pattern
f<character>
- jump forward to the character in the linet<character>
- jump forward to the start of the character in the lineF<character>
- jump backword to the character in the lineT<character>
- jump backword to the start of the character in the line
Combine with delete
dt<character>
- delete to the character (not including the character)df<character>
- delete to the character (including the character)dT<character>
- delete backword to the character (not including the character)dF<character>
- delete backword to the character (including the character)
:s/PATTERN/REPLACEMENT/FLAGS
Try these on the line with string cats
Line Substitution
:s/cat/dog
:s/cat/dog/g
:s/cat/dog/i
Global Substitution
:%/cat/dog
:%/cat/dog/g
:%/cat/dog/ig
Press v
to go into visual select mode. Move the cursor around to select the text.
v
- select by charactersV
- select by lines- ctrl +
v
- select in a block
Once you've selected the block, you can press:
y
- "yank" the text into paste bufferx
ord
- delete the selected text>>
- indent text right by shiftwidth<<
- indent the text left by shiftwidth
You can push .
to indent again.
You can also use all the ways of moving around like 0
, $
, PATTERN
There is also visual line mode
You can switch to visual line mode by entering V
. It will only apply to whole lines.
Following command indents the spaces by two instead of four.
s/ / /g
Whenever you delete something in Visual
mode, it actually gets into paste buffer. If you navigate to anywhere else in the file and press p
, it will get pasted. If you want to copy without deleting, Press y
and paste with p
.
There are more ways to insert mode that just i
.
o
- Goto insert mode, inserting a line below the current lineO
- Goto insert mode, inserting a line above the current linea
- Goto insert mode, at one character to the rightA
- Goto insert mode, at the end of the current lineJ
- Move the next line to the end of the current line(backtick) + .
- jump to the last edit
If you really really like vim, you can make your terminal follow the rules of vim
From you terminal, run
set -o vi