Basic Linux commands
5 min readAug 12, 2020
The basic components of Linux are:
- Kernel: It is the core component of the Operating System that manages operations and hardware.
- Shell: Shell is a Linux interpreter that is used to execute commands.
- GUI: GUI stands for Graphical User Interface which is another way for a user to interact with the system. But unlike CLI, GUI consists of Images, Buttons, TextBoxes for interaction.
- System Utilities: These are the software functions that allow the user to manage the computer.
- Application Programs: Software programs or set of functions designed to accomplish a specific task.
The most common Shells used in Linux are
- bash: Bourne Again Shell is the default for most of the Linux distributions
- ksh: Korn Shell is a high-level programming language shell
- csh: C Shell follows C like syntax and provides spelling correction and Job Control
- zsh: Z Shell provides some unique features such as filename generation, startup files, login/logout watching, closing comments etc.
- fish: Friendly Interactive Shell provides some special features like web-based configuration, auto-suggestions, fully scriptable with clean scripts
The basic and most commonly used linux commands
- ls — and its options
A) ls -list all non-hidden files
B) ls -lrt — List all (ls) the files in the current directory in long format(l), sorted by modification time(t), oldest first(r- recursive).
C) ls -lart — List all(ls) the files in the current directory including .(a)in long format(l), sorted by modification time(t), oldest first(r- recursive). - hostname- used to obtain the DNS(Domain Name System) name and set the system’s hostname or NIS(Network Information System) domain name.
A) hostname — display the hostname.
B) sudo hostname <hostname> — set the hostname. - cp — used to copy the file and folder form one directory to another in a system
-cp [options] source dest
A) cp -n — no file overwrite.
B) cp -R — recursive copy (including hidden files).
C) cp -u — update — copy when the source is newer than dest.
D) cp *.java test — copy all java files in test directory. - cd — used to change the current directory
A) cd [directory] — go to another mentioned directory.
B) cd ~ — change to home directory.
C) cd .. — change to parent directory
D) cd / — change to root directory - mv — Move/ Rename files and directories.
A) mv -f — force move by overwriting destination file without prompt.
B) mv -I — interactive prompt before overwrite.
C) mv -u — update — move when source is newer than destination.
D) mv -v — destination. - ssh — is a network protocol that enables secure remote connections between two systems. We can login to the remote system using ssh.
A) ssh 192.168.56.101 — ssh without username.
B) ssh username@hostname_or_ip — ssh with username.
C) ssh test.server.com -p 3322 — ssh with port number (-p 3322). - scp — used to securely copy files over the SSH protocol using the SCP tool
A) scp fileName user@remotehost:/home/username/destination — copy in the remote system at the destination directly.
B) scp *.java user@remotehost:/home/username/destination — copy all java files in the remote system at the destination directly. - du — display disk space usage
A) du -h — print sizes in a human-readable format.
B) du -s — summary of the file system using -s option.
C) du -sh — print summary of the files in the current directory in a human-readable format. - chmod — used to modify the permissions of the file specified by file name to the permissions specified by permissions.
A) chmod u=rwx,g=rx,o=r myfile.
a) u — the file owner can read, write and execute the file.
b) g- the file’s group can read and execute the file.
c) o — the all other users can only read the file.
B) chmod 754 myfile (4 stands for “read”, 2 stands for “write”, 1 stands for “execute”, and 0 stands for “no permission.”)
a) 7 (4+2+1)- the file owner can read, write and execute the file.
b) 5 (4+1)- the file’s group can read and execute the file.
c)4 — all other users can only read the file. - pwd — used to locate the current working directory.
- kill — used to terminate processes manually.
A) kill -9 [pid] -kills the process running at [pid] - lsof — list of opened files
A) lost -U -list of opened files per user.
B) lsof -i — Files opened by network connections.
C) lsof -I:[port_num] -Files opened by mentioned port. - jps — used to list all the processes that are running on Java virtual machine.
- cat — the cat command is used for many purposes for text file/s like display, read, file concatenation, modifying, etc.
A) cat filename — displays the content of filename.
B) cat filename1 filename2 — displays the content of filename1 and filename2.
C) cat filename.txt — displays the content of filename with the printed line number for each line. - vi — is the text editor used in line for file modifications.
vi <filename_NEW> or <filename_EXISTING>
Below are some vi commands used for editing the file.
- i — Insert at cursor (goes into insert mode)
- a — Write after the cursor (goes into insert mode)
- A — Write at the end of the line (goes into insert mode)
- ESC — Terminate insert mode
- u — Undo last change
- U — Undo all changes to the entire line
- o — Open a new line (goes into insert mode)
- dd — Delete line
- 3dd — Delete 3 lines.
- D — Delete contents of line after the cursor
- C — Delete contents of a line after the cursor and insert new text. Press ESC key to end insertion.
- dw — Delete word
- 4dw — Delete 4 words
- cw — Change word
- x — Delete character at the cursor
- r — Replace character
- R — Overwrite characters from cursor onward
- s — Substitute one character under cursor continue to insert
- S — Substitute entire line and begin to insert at the beginning of the line
- ~ — Change case of individual character
There are many more commands which we need to deal with day to day work, but I mentioned a few of them for basic knowledge.