This repository contains my personal notes on the linux commands with shell programming course given by sir Durga. This repository is not intended to explain the Linux operating system, but it can be very useful as a reminder of how a specific Linux command works. In the first part of this repository, you can find good examples of almost everything related to linux commands, in the second part you will find everything related to shell programming (not yet).
- Introduction
- Useful Linux commands
- Linux Files Types
- Terminal
- File Navigation System
- Linux sub directories
- ls command and its options
- Creation of directories
- Removing directories rm and rmdir commands
- Copying moving and renaiming files and directories
- Creation of files using cat touch gedit and vi
- View files contents using cat tac rev head and tail
- View files content using less and more
- Creation of hidden files and directories
- Copying moving and renaiming files and directories using cp and mv commands (2)
- Comparing content of files (diff sdiff...)
- Soft and hard link
- Sort commands
- Uniq commands
- Input and Output
- Redirecting stdInput stdOutput and stdError
- Piping
- Execute multiple command
- Commands aliasing
- Regular expressions
- locate
- find command
- Compression and uncompression of files (episode 33)
- grep command
- Regular expressions patterns
- The cut command
- The Linux File Permissions Concept
- Working with editors
- Stackoverflow questions
- How many users are logged in:
who - Who is the current user:
whoami - Print the date:
date - Show calendar:
cal - Print working directory:
pwd - Print files and directory of the current directory:
ls - Manual help:
man whoami - Create a new directory:
mkdir directoryname - Create a new empty file:
touch filename - Remove a directory:
rmdir directoryname - Remove a file:
rm filename - Remove a directory and its content:
rm -r directoryname - Display list of available commands:
help - Clear terminal:
clear,ctrl+l - Exit session: ``exit`
- Display date:
date - Display time:
time - Print information:
hello - Increase the font size:
Ctrl, shift and + - Print 5 random numbers between 1 and 100:
shuf -i 1-100 -n 5 - Creating file using the
shufcommand:touch dir$(shuf -i 1-10 -n 1)/sunny.txt - Simple user:
$ prompt - Root user/admin:
# prompt - Switch from simple user to root user:
sudo -i - Switch from root to simple user:
exit
- Directory file.
- Normal file [binary, text].
- Device file : every device is represented as a file.
- To show the type of a file:
file file.txt - Linux read files based on their content and not based on their extensions.
- Open the terminal:
ctrl+alt+t - Close the terminal:
ctrl+d - Show the file that represent the current terminal:
ttl--->/dev/pts/0 - Show files:
ls -l-lmeans long listing.
- Display hidden files:
ls -a-ameans all. - Home directory:
/home/user1,/homecontains the home users directories. - Go directly to the home directory:
cdorcd ~ - Go to the previous directory:
cd - /is the topmost root.
- bin directoery: it contains all binary executables related to our linux commands,
which touchwill print/user/bin/touch - sbin directory: it means system bin, an it contains binary executables related to the super user.
- etc directory: it contains system configuration informations required by the operationg system, for example the passwords of the users
/etc/passwd, groups info/etc/groupand hosts info/etc/hosts(ip addresses and dns names). - tmp directory:
tmpmeans temporary, if any file or directory is required temporary, it is created under the tmp folder in the current session. the content of the directory will be deleted automatically after the shutdown. - dev directory:
devmeans device, all the device related files will be stored in this directory, using those files, we can communicate with the device, tty file for terminal related files, hd for harde drive file ... - mnt directory, media directory(for mounting).
- opt directory: opt means optional, it contains all the 3rd part software installaion files.
- lib directory: lib means libraries, which are required by applications.
- var: var means variable data, variable data will be stored inside this directory, for example the log files.
- usr: all users related software.
- home: every user has a separate folder to hold his specific data like images, documents and so one.
- root directory: home directory of the super user.
- proc: proc means process, for each process a unique id is allocated, and a separate directory will be created inside the proc folder.
- boot directory: it contains the required files to boot the linux system.
- List all files and directories of the directory dir1:
ls dir1 - List all files and directories of the current directory:
ls - List content in reverse alphabetical order:
ls -r - List content in a long format(more details on the content):
ls -l - List content based on creation time:
ls -t - Possibilioty to combine all the above options:
ls -ltr - List also the hidden files:
ls -a - List contents by type:
ls -F(/, *, @) - List contents recursively:
ls -R - List contents with total size(1 block = 1024kb):
ls -s - List contents of the top 10 entries:
ls -l /dir | head - List contents of the top 5 entries:
ls -l /dir | head -5 - List contents of the bottom 10 entries:
ls -l /dir | tail - List contents page by page:
ls -l /dir | moreorls -l /dir | less, less is more powerful(forward and backword direction).
- To create a directory in the same pwd:
mkdir directoryName - To create multiple directories in the same pwd:
mkdir dir1 dir2 dir3 - To create multiple directories from the same pwd:
mkdir -p dir1/dir2/dir3.dir1, dir2will be created only if they don't exist already. - To create multiple directories
mkdir -p dir1/dir2{dir3-1,dir3-2,dir3-3}/dir{1..31}, every dir3-x will contains 31 directories. - Show directory content as a tree datastructure:
tree
- To remove directories:
rmdir dir1 dir2(rmdir removes only directories, no file will be deleted, and it will works only if the directories are empty). - To remove directories:
rm -r dir1, orrm -R dir1. For files no need to the-roption,-rfor recursive operations. - To get confirmation before deleting the contents:
rm -r -i dir1 - Force removal
-f:rm -rf dir1 - Verbose option
-v:rm -rv dir1, to print the flow of the deletion process.
- To copy file content from one file to another:
cp a.txt b.txt, ifb.txtdoesn't exist, a new file will be created, if it already exists, the file will be overwritten.cp a.txt home/anotherDirectory/c.txtto copy content to another destination. - To copy files to directories:
cp a.txt b.txt c.txt home/anotherDirectory/d.txt, to copy all filescp dir1/* dir2 - To copy total diretory:
cp -r dir1 dir2, the content ofdir1will be copied todir2 - Renaiming a file:
mv a.txt b.txt, to rename a directorymv dir1 dir2, ifdir2already exists,dir1wil be moved insidedir2 - Moving files from one directory to another:
mv dir1/* dir2,mv a.txt b.txt dir2
- Using the cat command:
cat > a.txt, a possibility to write content on the a.txt file, ctrl+c to save and exit, if the file is already available, the data will be overwritten, to perform append operation,cat >> a.txt - Using the touch command:
touch b.txt, a new empty file will be created, if the file is already available, the data will be not be overwritten, only the last date of modification will be modified. - Using the gedit command:
gedit c.txt, ctr+s to save, ctrl+q to quit the graphical editor. - Using vi editor:
gedit d.txt, press i to enter the insert mode, press esc key, :wq to save and quit the editor. vim is an advanced version of the vi editor.
- View content using cat command:
cat < a.txt, where<symbol is optional.-noption to display line numbers,-nto skip blank lines numbering.cat a.txt b.txtwill print the content of the two passed files. - Create and add content to a file:
cat > a.txt,added content,ctrl+dto exit the editing section.>>for append operation. - Copy content from one file to another using cat command:
cat a.txt >> b.txt, using>will overwrite the content of the b.txt file. - Copy content from many files to one file:
cat a.txt b.txt >> c.txt - View content of a file in reversed order:
tac a.txt, the order here is the order of the file lines. - View content of a file in a horizental reversed order:
rev a.txt, if the content ofa.txtiscontent, the result of the previous command will betnetnoc catcommand is suitable for smallfiles, if the files are huge,headandtail,moreandlessare the most recommended commands.- To view specified lines from the top of a file:
head a.txt, first 10 lineshead -n 10 a.txt, abbreviation:head -10 a.txt - To view specified lines except some specified lines:
head -n -5 a.txtwill diplay all lines except the last 5 lines. - To view specified number of character:
head -c 5 a.txtwill show the first 5 characters. - To view specified lines from the bottom of a file:
tail a.txtlast 10 lines, last 5 linestail -n 5 a.txt, abbreviation:tail -5 a.txt,tail -c 100 a.txtwill print the last100characters. - To view content from line 3 to line 7 using piping:
head -7 a.txt | tail -5
- To view file content page per page:
more a.txt, pressenterto diplay next line, pressspace barto diplay the next page, pressqto exit,more -d a.txtto diplay more info about the file. By using more command, we can view file content page per page only in forward direction. - To view file content page per page either in forward direction or backward direction:
less a.txt, pressdto diplay the next page (d means down), pressbto diplay the previous page (b means backward).
Creation of hidden files and directories
- If any file name starts with
., such file is a hidden file. - Create hiiden files: the name of the file should begin by a
., example:touch .a.txt,cat > .b.dat - Display hiden files:
ls -a - Create hiiden directories:
mkdir .dir1 - Conversion of Normal/hidden files: using renaiming operation,
mv .a.txt a.txt
cp a.txt b.txt, if b.txt exists, its content will be ovewritten directly, to get confirmation message,cp -i a.txt b.txt- Copy multiple files content to one destination file:
cat a.txt b.txt > c.txt - Moving and renaming files:
mv oldname.txt newname.txt - Moving and renaming directories:
mv olddirname newdirname, if olddirname exists already, then olddirname will be moved to newdirname, otherwise the renaming operation will take place. - Moving selected files to a directory:
mv a.txt b.txt dir1 - Moving all files of one directory to another directory:
mv dir1/* dir2 - Moving using absolute path:
mv ~/dir1/* ~/dir2,~/dir2equivalent to/home/userName/dir2
-
cmpcommand: comparing files byte by byte,cmp file1.txt file2.txt, if the files content are the same, no output, otherwise information about the first found difference, byte number and line number will be shown. Example:echo "hello" >> file1.txt,echo "hello2" >> file2.txt,cmp file1.txt file2.txt, output will befile1.txt file2.txt differ: char 6, line 1 -
diffcommand: it will show all differences,diff file1.txt file2.txt -
sdiffcommand: all differences in a parallel comparison. -
vimdiffcommand: advanced tool to show the differences, it will highlight all differnces in a vim editor,sudo apt-get install vimto install the tool, two windows will be shown,ctrl+w+wto go to the other window,:qto close current window,:qato close all windows,:qaiclose and ignore all changes.
- In Windows, we have only soft link files (shortcut|raccourci), in linux we have also hard link files.
- Hard link file is just another name of the same file. We can create hard link files using the following command
ln sourcefile.txt hardlinkfile.txt - If we deleted the source file, the hard link file wil still exists. If we modify the content of the (sourcefile|link hard file), the update will be reflected also in the (sourcefile|link hard file).
- Soft link file is just a shortcut of the original file (same as windows). If we delete the original file, the soft link file will be broken. We can create soft link files using the following command
ln -s sourcefile.txt softlinkfile.txt - Change made in the original file will also be reflected to the Soft link file.
- For directories, it is not possible to create hard link.
- Sort command, to sort files content line by line in an alphabetical order:
sort a.txtwill only print the sorted order.sort a.txt > b.txtwill redirect the sorted order output to the new file.sort a.txt > temp.txtandmv temp.txt > a.txtwill make change on the same source file. - Sort in a reverse alphabetical order:
sort -r a.txt - The spaces in the beginnning of a line are ignored, blank line came first when sorting, Upper case before lowercase, If the file contains numbers, the numbers will come before letters.
- If we want only unique lines, no duplicate, we should use
-uoption,sort -u a.txt - If we want to sort based on numerical values:
sort -n a.txt,sort -nr a.txt - Sort based on the column line number:
ls etc/ -l | head -7 | sort -rk 5will show the info of the top 7 files in etc folder and will sort them recursivly based on the fifth column (length of file). - Sort based on the column lines in a file composed by lines in the following format
token1:token2:token3,:is called a separator. To sort such file:sort -k 3 -t ":" a.txt
- Find unique content
sort -u a.txt, but uniq command is more powerful,uniq a.txtworks only on sorted files,sort a.txt | uniq. - With uniq command, we can use multiple options:
-d: to disply ony duplicate lines.-c: to disply numbers of occurences of each line.-i: to ignore case while comparing.-u: to disply ony unique lines.
- command take some input and produces some output:
- Input in two forms: stdin or command line argument, stdin example:
echo "hello" >> file.txt, command line argument example:touch file.txt - Output in two forms: stdout or stderror, stderror in cas when the terminal print arrors.
- Input in two forms: stdin or command line argument, stdin example:
- Standard Input, Standard Output and Standard Error are data stream and can be redirected from one place to another place. Hence, piping and redirection are possible on those data streams.
- stdIn associated with the number 0, by defualt it is connected with the keyboard.
- stdOutput associated with the number 1, connected with the terminal.
- stdError associated with the number 2, connected with the terminal.
- Redirecting stdOutput: We can perform redirection using
>and>>,cat 1> output.txt - Redirecting standard error: Instead of terminal we can redirect messages from terminal to another place, a file as an example.
cal jkbjkcabjkac 2> log.txt, 2 stands for stdError. - Redirecting standard input: we can use the
<symbol to perform input redirection,cat 0< a.txt, 0 is optional. - Examples:
cat 0<a.txt 1>copy.txt 2>error.txtcat <a.txt >copy.txt 2>error.txtcat <a.txt &>copy.txt, means either output or error direction to the copy.txt file.- Redirect the documentation content of the ls command to some output file:
man ls > newfile.txt.
ls -l /etc | more, the output of the first command will be the input of the second command.ls -l /etc > somefile.txt | more, no piping beacause the redirection symbol breaks the piping.teecommmand: if we want to save the output of intermedite command and want to pass that output as input to next command,ls -l /etc tee somefile.txt | morermcommand can't take data stream as input, but can only get arguments from the command line terminal, for that purpose, the commandxargscan be used to converts data stream to arguments. Example:cat /etc | xargs rm- pipe symbol
|can be used to link two commands,>is useful when redirecting the output to another place.
ls -l /etc | more: the preceding commands are dependent commands, to run multiple independent commands in one line, we can use the;symbol or by using&&.- By using
;: e.g.cmd1;cmd2;cmd3, ifcmd2fails,cmd3will be executed. - By using
&&: e.g.cmd1&&cmd2&&cmd3, ifcmd2fails,cmd3will not be executed.
- Aliases are nicknames given to a given command.
- Syntax:
alias nickname="original command name", example:alias cls=clear. Spaces are not allowed before and after the=symbol. - To list the existed aliases:
alias, to delete a given alias:unalias cls, to delete all aliasesunalias -a - To know the original name of an alias:
type cls - Aliases are temporary unless we persist them permanently:
- Editing the .bashrc file, It could be found inside the user home directory.
- Create our new aliasing file, the name of the file should be .bash_aliasses and should be located in the user home directory.
- If we want to represent a collection of strings according to a particular pattern, then we should go for Regular expressions.
- Some reg expressions rules:
- * ----> 0 or more character.
- ? ----> 1 character.
- [abc] ----> a or b or c.
- [!abc] ----> only one character but not a or b or c.
- [a-z] ----> any lower case character from a to z.
- [A-Z] ----> any upper case character from A to Z.
- [a-zA-Z] ----> any alphabet from a to z or from A to Z.
- [0-9] ----> any digit from 0 to 9.
- [a-zA-Z0-9] ----> any of the two above.
- [!a-zA-Z0-9] ----> negation of the above.
- Examples:
ls *,ls *.*,ls *.txt,ls a*,ls a*t.*,ls a?.*.ls [abc]*: aaaaaa, bbbb, ....ls [!abc]*: should not start with a, b or c.
- We can use locate to locate files and directories in our system.
locate file.txt,locate *.txt: to locate the given entries, locate command look in a database to locate the given entries, the database is updated only one time a day, so to update the database we should use the following command:sudo updatedblocate -S: to get the statistics of the database,locate -i FilE.txtto ignore case sensivity.locate --limit 5 a.txtto limit the search to 5 entries.locate --existing b.jpeg, or with-eoption: check if the gievn file exist or not.- Advantage of using a database is related to performance.
- It provides more search option comparing to the locate command. contrary to the locate command, the find command will search directly in the file system.
- We can search only files, only directories, search by name, search by size...
find: by default, it will look for all files and directories from the current directory and below in the linux file system.find /home: look for all files and directories from the current directory and below.find -maxdepth 2: will limit the search only in the 2 levels.- By default the find command will also look for hidden files.
find -type f: will limit the search to the files.find -type d: will limit the search to the directories.find . -type f -name d?.*: look for files inside the current directory and below with the given name that match given regular expression,-inameoption to ignore case.find . -type f -size +200k: look for files that have size greater than 200 kbytes,-size -200kfor less than,-size 100kfor equality size.
tarcommand: tape archive, to group multiple files and directories into a single archive,tar -cvf demo.tar file1 file2 dir1,-ccreate,-vverbose,-fnamed file.tar -tvf demo.tar: print table of content of the tar file.tar -xvf demo.tar: extract the given tar file.
gzip demo.tar: To compress a given file,demo.tar.gzwill be created.gunzip demo.tar.gz: To decompress a given compressed file.bzip2andbunzip2as a second alternative to compress and decompress files.- Archiving and compressing (
gzip) in one command: using the-zoption,tar -czvf demo.tar file1 file2 dir1 - Decompressing (
gunzip) and extracting the archive in one command: using the-xoption,tar -xvzf demo.tar.gz
- Globally search a regular expression and print it, global regular expression print, global regular expression parser.
locateandfindhelp to find required files and directories,grepcommand help to find content within the file.- Syntax:
grep <pattern> filname, and it will print all matched lines. grep someword filname1.txt filname2.txt: look for the pattern in the given files.grep someword *: look for the pattern in the current directory and not in the sub-directories, to ignore case we should use the-ioption,--colorto display the result in a colored form.grep someword -c *: to print how much time the pattern is found.grep someword -n *: to print the lines number where the pattern is found.grep someword -l *: to print only the file names where the pattern is found.grep someword -v *: to print the lines where the pattern is not found.grep -w 'word1' *: will search for the exact word,aword100will not be valid, onlyword1will be a correct awnser.- Display before, after and surrounding lines including search result:
-Ameans After,-Bmeans Before,-Cmeans Before and After.grep 'word1' -B3 * - Search multiple contents in a file:
egrep '(word1|word2)' *,grepcan understand only some patterns; for this reason we useegrep grep -o 'word1' *: will print only matched pattern.egrep -R '(word1|word2)' *: will execute recursivly, it means also checking the sub-directories.
grep 'd*' *: display all lines which contains d followed by any number of characters.grep 'c[aei]b' *: all lines wich containscab,ceborcibgrep 'b..x' *:.means any character, it will search for all 4 letter words beginning with b and ending by x.
grep 'linux' demo.txt: display all lines which contains the wordlinux, for example linuxworld will be valid.grep '\<linux\>' demo.txt: display all lines which contains exactlylinux, so linuxworld is invalid.grep '\<linux' demo.txt: display all lines which starts withlinux.grep 'linux\>' demo.txt: display all lines which ends withlinux.
^: line starts with,grep '^d' demo.txtwill print all lines that start withd.$: line ends with.\<the\>: lines that start exactly with the wordthe^[aeiu]: lines that start witha,e,ioru^[^aeiu]: lines that do not start witha,e,ioru^....$: lines that contains only 4 charachters.^\.: lines that starts with the.symbol.^$: blank lines, to remove blank lines,grep '^$' file.txt > temp.txt,mv temp.txt file.txt.
|: it matches any of the passed string,egrep '(linux|java|docker)' *.{m}: it matches exactly m occurences of the preceiding character.egrep '[0-9]{10}' *will match every 10-digit mobile numbers.{m, n}: it matches minimum m occurences and maximum occurences of the preceiding character.egrep '[0-9]{8, 10}' *will match every 8-digit, 9-digit and 10-digit mobile numbers.{m, }means no restriction for the maximum occurences.
- we use the cut command to extract specific data from files, the file can be either normal or tabular.
cut -c 9 emp.dat: print the9thcharacter in each line.cut -c 3-5 emp.dat: print the3th,4th,5thcharacter in each line,-c 3-the 3th to the last character,-c -3will print the3first characters by each line.cut -c 3-5 7-9 emp.dat: will display the3th,4th,5thcharacters and also the7th,8thand9th- Display content based on delimiter:
name|username|password, to print the second column we can usecut -d "|" -f 3 tabularfile,doption for delimiter andffor field,-f 1-3to get the columns 1, 2 and 3,-f -2to get up to the second column,-f 1, 3to get only the first and third column, to get all columns execpt the third column, we can usecut -d "|" --complement -f 3 tabularfile
File persmmisions describe the allowed operations by various users.
- User categories.
- Permission types.
- Operations allowed related permissions.
- All users are divided into
4types:- User/owner: represented by
u, the user who created the file. - Group: represented by
g. - Others: represented by
o. - All: represented by
a.
- User/owner: represented by
r: Read, on files we can view the content, on directories we can view the content of the directorties; for examplelscommand.w: Write, on files we can edit the content of files, on directories we can create and delete files.x: Execute, on files we can execute the file just like a program, on directories we can enter into a directory; for example usingcdcommand. Keep in mind to execute a file, both read permission and execute permission are required.-: No permission.- By default, the owner of the file get only read and write permissions.
- Without execute permission, read and write permissions on directories are useless.
+: add a partcular permission to auser|group|others|all.-: remove a partcular permission from auser|group|others|all.-: assign a set of permissions to auser|group|others|all.chmodcommand:- It means change mode. we can use it to change files or directories permission.
- Syntax:
chmod <user_category><permission>, example:chmod u+x script.sh, gives execute permission to the owner on the given file. chmod u+x,g+w,o-r demo.txt: gives the owner execute permission, gives write permission to the group and remove read permission from others.chmod u=rw,g=rw,o=r demo.txt: we assing read and write permission to the owner and the group, the read opertion to others.chmod a=- demo.txt: we retrieve all permissions from all.chmod a=rwx demo.txt: we give all permissions to all.r, w, xare symbolic permissions, we can also specify permissions by using octal numbers.0:000means No permission.1:001means only execute permission.2:010means only write permission.3:011means write and execute permission.4:100means only read permission.5:101means only read and execute permission.6:110means only read and write permission.7:111means read, write and execute permission.- Note that
4means read permmision,2means write and1means execute. - Example:
chmod u=rw,g=wx,o=w demo.txtcould be also written aschmod 632 demo.txt.chmod 77 demo.txt: is the same thing aschmod 077 demo.txt.chmod demo.txtis not allowed.
sudo addgroup coursegroup: to create a group with namecoursegroup, we can check if the group was added by checking the following fileetc/group.sudo adduser --ingroup coursegroup user1: to create a new user in the groupcoursegroup. We can check if the user was succefuly added by checking the following fileetc/passwd.sudo adduser user2: when executing this command, a new user will be createduser2, a group with the same name will also be created, and the new user will be added to thatgroup.su - user1: change current linux user touser1.
- umask means user mask. Based on the umask value, default permissions will be there for files and directories. The default umask value is
022. - Default permissions for files:
666- umask value,666-022=644, it means read and write for the user, read for the group and the others. - Default permissions for directories:
777- umask value,755-022=644, it means read, write and execute for the user, read and execute for the group and the others. - To congigure ou own umask value, by using the umask command:
umask 002. - Se case:
- For newly created files, default permission should be
444, what should be the mask value:444=666-umask=222. - The most used umask values are:
022:644on files means read and write for the user, read for the group and others.002:664on files means read and write for the user and the group, read for the others.077:600on files means read for the user, nothing for the group and others.007:660on files means read and write for the user and the group, nothing for others.
- For newly created files, default permission should be
- There are multiple editors: graphical editor (gedit), vi editor, nano editor...
- gedit: same as windows note pad.
gedit file1.txt: if the file exist already, it will be opened for editing, otherwise a new file will be created and opened for editing. Note thatgeditcan work only on the desktop version.
viorvisual editor: it is a unix based editor, on the contrary to gedit and nano that are linux based. We can use it to create new files and to edit the contrent of the existing ones.vi file2.txt: if the file exist already, it will be opened for editing, otherwise a new file will be created and opened for editing.- To save the file:
:wq(w means write, q means quit). - There are
3mode ofvi:Command mode: It is the default mode, in this mode you can use anyvicommand, from command mode we can enter into insert mode by using multiple ways, for example we can usei. To enter into exit mode we press:key.Insert mode: In this mode we can insert/modify/append data, to return to the command mode we can pressEsckey.Exit mode: To insert the exit mode from the command mode, we press:, in this mode we can exit thevieditor. To return to the command mode we pressEsc
- How to insert/append data when we are in the command mode, we can press the following keys:
A: To append data at the end of the line.I: To isert data at the beginning of the line.a: To append data to the right side of the cursor position (just after the cursor position).i: To insert data to the left side of the cursor position (just before the cursor position).
- To delete data when we are in the command mode, we can press the following keys:
- Delete characters and words:
x: To delete the current character.nx: To delete the n characters starting from the current position.X: To delete the previous charachter.nX: To delete the n previous charachters.dw: To delete the current word.ndw: To delete the n words starting from the current position.
- Delete lines:
dd: To delete the current line.ndd: To delete n lines.d$: To delete from the current position to the end of line.d^: To delete from the beginning to the current position.dgg: To delete from the beginning of the file to the current position.dG: To delete from the current position to the end of the file.
- Delete characters and words:
- To replace data when we are in the command mode, we can press the following keys:
r: To replace the current character.R: To replace multiple charatcers from the current position.Sorcc: To replace a line.O: To open a new line above te cursor position.o: To open a new line below te cursor position.
- To copy/paste data when we are in the command mode, we can press the following keys:
yy: To copy one line,yfor yanking.nyy: To copy n lines.yw: To copy a word.nyw: To copy n words.y$: To copy from current cursor to the end of line.y^: To copy from the beginning of the line to the cursor position.p: small p, To paste below the cursor position.P: cpaital P, To paste above the cursor position.
- Navigation when we are in the command mode:
k: up.j: bottom.l: right.h: left.- Arrow keys can replace the above keys.
$: go to the end of the current line.^: go to the beginning of the current line.H: go to the beginning of the current page.M: go to the middle of the current page.L: go to the end of the current page.b: backword, back to the beginning of the word.nb: back to the beginning of the nth word.e: end of the currentrword.ne: end of the third word.w: forward, forwarding to the beginning of the next word.nw: forward, forwarding to the beginning of the nth next word.G: move to the last line.ctrl+f: page down.ctrl+b: page up.u: undo operatio, to undo the previous operation.
- Exit mode commands:
:w: save file data.:wq: save file data and quit from the editor.:q: quit from the editor.:q!: ignore changes and quit from the editor.:set nu: set line numbers on the editor.:set nonu: to remove line numbers from the editor.:n: place the cursor to the nth line.:$: place the cursor to the laste line.:!<unix_command>: to execute any unix command,:!ls,:!date, ...