Linux Basics

Linux

Linux is a family of open-source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically packaged in a Linux distribution.

Linux is used in the following ways: Server OS for web servers, database servers, file servers, email servers and any other type of shared server. Designed to support high-volume and multithreading applications, Linux is well-suited for all types of server applications. Desktop OS for personal productivity computing.

# 1. User Information

  • who It is used to get information about currently logged in user on to system. If you don’t provide any option or arguments, the command displays the following information for each logged-in user.
    1. Login name of the user
    2. User terminal
    3. Date & Time of login
    4. Remote host name of the user
$ who
admin    tty2         2022-02-10 01:36 (tty2)
root     pts/1        2022-02-15 00:29 (10.190.95.154)
  • whoami: It display the system’s username
$ whoami
Avani Punita
  • id: It display the user identification ( the real and effective user and group IDs ) information
$ id
uid=1000(sj) gid=1000(sj) groups=1000(sj),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),120(lpadmin),131(lxd),132(sambashare)
  • groups: This command is used to display all the groups for which the user belongs to.
$ group
sj: sj, adm, cdrom, sudo, dip, plugdev, lpadmin, lxd, sambashare
  • users: Displays usernames of all users currently logged on the system.
$ users
root
  • grep: It is a powerful pattern searching tool to find information about a specific user from the system accounts file: /etc/passwd.
$ grep -i sj /etc/passwd
sj:x:1000:1000:sj,,,:/home/sj:/bin/bash
  • W Command: It(W) is a command-line utility that displays information about currently logged in users and what each user is doing.
w [OPTIONS] [USER]
Example:
w
 18:45:04 up  2:09,  1 user,  load average: 0.09, 0.07, 0.02
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
sj       :0       :0               01:27   ?xdm?   1:14   0.01s /usr/lib/gdm3/g
  • last or lastb: Displays a list of last logged in users on the system. You can pass user names to display their login and hostname details.
last [options] [username...] [tty...]

Example:

root     pts/1        xx.xxx.xx.xxx    Tue Feb 15 00:29   still logged in
root     pts/2        xx.xxx.xx.xxx    Mon Feb 14 03:45 - 06:59  (03:13)
root     pts/1        xx.xxx.xx.xxx    Mon Feb 14 01:21 - 04:35  (03:13)
root     pts/1        xx.xxx.xx.xxx    Sun Feb 13 21:28 - 00:43  (03:15)
root     pts/1        xx.xxx.xx.xxx    Sun Feb 13 09:46 - 10:20  (00:33)
root     pts/1        xx.xxx.xx.xxx    Sat Feb 12 21:30 - 23:44  (02:13)
root     pts/1        xx.xxx.xx.xxx    Sat Feb 12 11:15 - 11:56  (00:40)
root     pts/1        xx.xxx.xx.xxx    Sat Feb 12 11:08 - 11:15  (00:06)
root     pts/1        xx.xxx.xx.xxx    Sat Feb 12 02:06 - 05:19  (03:12)
  • lastlog: The lastlog command is used to find the details of a recent login of all users or of a given user.
$ lastlog

Username         Port     From             Latest
root             pts/1    xx.xxx.xx.xxx    Tue Feb 15 00:29:27 -0500 2022
daemon                                     **Never logged in**
bin                                        **Never logged in**
sys                                        **Never logged in**
sync                                       **Never logged in**
games                                      **Never logged in**
man                                        **Never logged in**
lp                                         **Never logged in**
mail                                       **Never logged in**
news                                       **Never logged in**

# 2. Directory Commands

  1. pwd The pwd ( Present Working Directory ) command is used to print the name of the present/current working directory starting from the root.
$ pwd
/home/sj/Desktop/Linux
  1. mkdir The mkdir ( make directory ) command allows users to create directories or folders.
$ mkdir ubuntu
$ ls
ubuntu

The option ‘-p’ is used to create multiple directories or parent directories at once.

$ mkdir -p dir1/dir2/dir3
$ cd dir1/dir2/dir3
~/Desktop/Linux/dir1/dir2/dir3$
  1. rmdir: The rmdir ( remove directories ) is used to remove empty directories. Can be used to delete multiple empty directories as well. Safer to use compared to rm -r FolderName. This command can also be forced to delete non-empty directories.
    1. Remove empty directory:
rmdir FolderName
  1. Remove multiple directories:
rmdir FolderName1 FolderName2 FolderName3
  1. Remove non-empty directories:
rmdir FolderName1 --ignore-fail-on-non-empty
  1. Remove entire directory tree. This command is similar to rmdir a/b/c a/b a:
rmdir -p a/b/c
  1. rm: The rm ( remove ) command is used to remove objects such as files, directories, symbolic links etc from the file system.
    1. Remove file: The rm command is used to remove or delete a file
rm file_name
  1. Remove file forcefully: The rm command with -f option is used for removal of file without prompting for confirmation.
rm -f filename
  1. Remove directory: The rm command with -r option is used to remove the directory and its contents recursively.
rm -r myDir
  1. Remove directory forcefully: The rm command with -rf option is used to forcefully remove directory recursively.
rm -rf myDir
  1. touch: The touch command is is used to create, change and modify timestamps of a file without any content.
    1. Create a new file: You can create a single file at a time using touch command. The file created is an empty file.touch file_name
    2. Create multiple files: You can create the multiple numbers of files at the same time.touch file1_name file2_name file3_name
    3. Change access time: The touch command with a option is used to change the access time of a file.touch -a file_name
    4. Change modification time: The touch command with m option is used to change the modified time.touch -m file_name
    5. Use timestamp of other file: The touch command with r option is used to get timestamp of another file.touch -r file2 file1In the above example, we get the timestamp of file1 for file2.
    6. Create file with Specific time: The touch command with ‘t’ option is used to create a file with specified time.touch -t 1911010000 file_name
  2. cat: The cat command is used to create single or multiple files, view contain of file, concatenate files and redirect output in terminal or files.
    1. View file contents: You can view contents of a single or more files by mentioning the filenames.cat file_name1 file_name2

# 3. File Commands

Sl.No.CommandsDescription
01.lsdirectory listing
02.ls -alformatted listing with hidden files
03.cd dirchange directory to dir
04.cdchange to home
05.pwdshow current directory
06.mkdir dircreate a directory dir
07.rm filedelete file
08.rm -r dirdelete directory dir
09.rm -f fileforce remove file
10.rm -rf dirforce remove directory dir *
11.cp file1 file2copy file1 to file2
12.cp -r dir1 dir2copy dir1 to dir2; create dir2 if it doesn’t exist
13.mv file1 file2rename or move file1 to file2 if file2 is an existing directory, moves file1 into directory file2
14.ln -s file linkcreate symbolic link link to file
15.touch filecreate or update file
16.cat > fileplaces standard input into file
17.more fileoutput the contents of file
18.head fileoutput the first 10 lines of file
19.tail fileoutput the last 10 lines of file
20.tail -f fileoutput the contents of file as it grows, starting with the last 10 lines

# 4. File Permissions

  • chmod octal file – change the permissions of file to octal, which can be found separately for user, group, and world by adding
  • 4 – read (r)
  • 2 – write (w)
  • 1 – execute (x)

Examples

  • chmod 777 – read, write, execute for all
  • chmod 755 – rwx for owner, rx for group and world

Since Linux is a multi-user operating system, it is necessary to provide security to prevent people from accessing each other’s confidential files. So Linux divides authorization into 2 levels,

  1. Ownership: Each file or directory has assigned with 3 types of owners i. User: Owner of the file who created it. ii. Group: Group of users with the same access permissions to the file or directory. iii. Other: Applies to all other users on the system
  2. Permissions: Each file or directory has following permissions for the above 3 types of owners.i. Read: Give you the authority to open and read a file and lists its content for a directory.ii. Write: Give you the authority to modify the contents of a file and add, remove and rename files stored in the directory.iii. Execute: Give you the authority to run the program in Unix/Linux.The permissions are indicated with below characters,
  r = read permission

  w = write permission

  x = execute permission

  \- = no permission

The above authorization levels represented in a diagram

perm

There is a need to restrict own file/directory access to others.

Change access: The chmod command is used to change the access mode of a file. This command is used to set permissions ( read, write, execute ) on a file/directory for the owner, group and the others group.

chmod [reference][operator][mode] file...

Example
chmod ugo-rwx test.txt

There are 2 ways to use this command,

  1. Absolute mode: The file permissions will be represented in a three-digit octal number.The possible permissions types represented in a number format as below.
Permission TypeNumberSymbol
No Permission0
Execute1–x
Write2-w-
Execute + Write3-wx
Read4r–
Read + Execute5r-x
Read + Write6rw-
Read + Write + Execute7rwx

Let’s update the permissions in absolute mode with an example as below,

 chmode 764 test.txt
  1. Symbolic mode: In the symbolic mode, you can modify permissions of a specific owner unlike absolute mode.The owners are represented as below,OwnerDescriptionuuser/ownerggroupootheraalland the list of mathematical symbols to modify the file permissions as follows,OperatorDescription+Adds permission-Removes the permission=Assign the permission

Changing Ownership and Group: It is possible to change the the ownership and group of a file/directory using chown command.

chown user filename
chown user:group filename

Example:
chown John test.txt
chown John:Admin test.txt

Change group-owner only: Sometimes you may need to change group owner only. In this case, chgrp command need to be used

chgrp group_name filename

Example:
sudo chgrp Administrator test.txt
#PermissionrwxBinary
7read, write and executerwx111
6read and writerw-110
5read and executer-x101
4read onlyr–100
3write and execute-wx011
2write only-w-010
1execute only–x001
0none000

For a directory, execute means you can enter a directory.

UserGroupOthersDescription
644User can read and write, everyone else can read ( Default file permissions )
755User can read, write and execute, everyone else can read and execute ( Default directory permissions )
  • u – User
  • g – Group
  • o – Others
  • a – All of the above
ls -l /foo.sh            # List file permissions
chmod +100 foo.sh        # Add 1 to the user permission
chmod -100 foo.sh        # Subtract 1 from the user permission
chmod u+x foo.sh         # Give the user execute permission
chmod g+x foo.sh         # Give the group execute permission
chmod u-x,g-x foo.sh     # Take away the user and group execute permission
chmod u+x,g+x,o+x foo.sh # Give everybody execute permission
chmod a+x foo.sh         # Give everybody execute permission
chmod +x foo.sh          # Give everybody execute permission

# 5. Networking

  1. Display network information: ifconfig command is used to display all network information ( ip address, ports etc )
ifconfig -a
  1. Test connection to a remote machine: Send an echo request to test connection of a remote machine.ping <ip-address> or hostname Example: ping 10.0.0.11
  2. Show IP Address: Display ip address of a currennt machinehostname -I (OR) ip addr show
  3. Active ports: Shows active or listening portsnetstat -pnltu
  4. Find information about domain: whois command is used to find out information about a domain, such as the owner of the domain, the owner’s contact information, and the nameservers used by domain.whois [domain] Example: whois google.com

# 6. Installing Packages

  1. Install package:
yum install package_name
  1. Package description: The info command is used to display brief details about a package.
yum info package_name
  1. Uninstall package: The remove command is used to remove or uninstall package name.
yum remove package_name
  1. Install package from local file:

It is also possible to install package from local file named package_name.rpm.

rpm -i package_name.rpm
  1. Install from source code:
tar zxvf sourcecode.tar.gz
cd sourcecode
./configure
make
make install

# 7. Disk Usage

  1. Synopsis: du command is used to check the information of disk usage of files and directories on a machine
du [OPTION]... [FILE]...
  1. Disk usage of a directory: To find out the disk usage summary of a /home/ directory tree and each of its sub directories
du  /home/
  1. Disk usage in human readable format: To find out the disk usage in human readable format
du  -h /home/
  1. Total disk usage of a directory: To find out the total disk usage
du  -sh /home/
  1. Total disk usage of all files and directories: To find out the total disk usage of files and directories
du  -ah /home/
  1. Total disk usage of all files and directories upto certain depth: print the total for a directory only if it is N or fewer levels below the command
du  -ah --max-depth 2 /home/
  1. Total disk usage with excluded files: To find out the total disk usage of files and directories, but excludes the files that matches given pattern.
du -ah --exclude="*.txt" /home/
  1. Help: This command gives information about du
du  --help

# 8. System and Hardware Information

  1. Print all informationuname is mainly used to print system information.
$ uname -a
  1. Print kernel name:
$ uname -s
  1. Print kernel release:
$ uname -r
  1. Print Architecture:
$ uname -m
  1. Print Operating System:
$ uname -o

# 9. Search Files

  1. Pattern search: The grep command is used to search patterns in files.
grep pattern files
grep -i // Case sensitive
grep -r // Recursive
grep -v // Inverted search

Example:
grep "^hello" test.txt // Hello John
grep -i "hELLo" text.txt // Hello John
  1. Find files and directories:

The find command is used to find or search files and directories by file name, folder name, creation date, modification date, owner and permissions etc and perform subsequent operations on them.

i. Search file with name:

find ./directory_name -name file_name

Example:
find ./test -name test.txt // ./test/test.txt

ii. Search file with pattern:

find ./directory_name -name file_pattern

Example:
find ./test -name *.txt // ./test/test.txt

iii. Search file with executable action:

find ./directory_name -name file_name -exec command

Example:
find ./test -name test.txt -exec rm -i {} \; // Search file and delete it after confirmation

iv. Search for empty files or directories:

The find command is used to search all empty folders and files in the entered directory or sub-directories.

find ./directory_name -empty

Example:
find ./test -empty
//./test/test1
//./test/test2
//./test/test1.txt

v. Search for files with permissions:

The find command is used to find all the files in the mentioned directory or sub-directory with the given permissions

find ./directory_name -perm permission_code

Example:
find ./test -perm 664

vi. Search text within multiple files:

find ./ -type f -name file_pattern -exec grep some_text  {} \;

Example:
find ./ -type f -name "*.txt" -exec grep 'World'  {} \; // Hello World
  1. Whereis to locate binary or source files for a command: The whereis command in Linux is used to locate the binary, source, and manual page files for a command. i.e, It is used to It is used to find executables of a program, its man pages and configuration files.
whereis command_name

Example:
whereis netstat //netstat:  /bin/netstat /usr/share/man/man8/netstat.8.gz ( i.e, executable and location of its man page )
  1. Locate to find files: The locate command is used to find the files by name. This command is faster compared to find command because it searches database for the filename instead of searching your filesystem.
locate [OPTION] PATTERN

Example:
locate "*.txt" -n 10 // 10 file search results ending with .txt extension

# 10. SSH

SSH ( Secure Shell ) is a network protocol that enables secure remote connections between two systems.

  1. Connect remote machine using IP address or machine name: The remote server can be connected with local user name using either host name or IP address
ssh <host-name> or <ip-address>

Example:
ssh 192.111.66.100
ssh test.remoteserver.com
  1. Connect remote machine using username: It is also possible specify a user for an SSH connection.
ssh username@hostname_or_ip-address

Example:
ssh john@192.0.0.22
ssh john@test.remoteserver.com
  1. Connect remote machine using custom port By default, the SSH server listens for a connection on port 22. But you can also specify the custom port.
ssh <host-name> or <ip-address> -p port_number

Example:
ssh test.remoteserver.com -p 3322
  1. Generate SSH keys using keygen: SSH Keygen is used to generate a key pair which consists of public and private keys to improve the security of SSH connections.
ssh-keygen -t rsa
  1. Copying SSH keys to servers: For SSH authentication, ssh-copy-id command will be used to copy public key ( id_rsa.pub ) to server.
ssh-copy-id hostname_or_IP
  1. Copy a File Remotely over SSH: SCP tool is used to securely copy files over the SSH protocol.
scp fileName user@remotehost:destinationPath

Example:
scp test.txt test@10.0.0.64:/home/john/Desktop
  1. Edit SSH Config File SSH server options customized by editing the settings in sshd_config file.
sudo vim /etc/ssh/sshd_config
  1. Run commands on a remote server SSH commands can be executed on remote machine using the local machine.
ssh test.remoteserver.com mkdir NewDirectoryName // Creating directory on remote machine
  1. Restart SSH service: You need to restart the service in Linux after making changes to SSH configuration.
sudo ssh service restart
(or)
sudo sshd service restart

# 11. List of Basic SSH Commands

Sl.No.CommandsDescription
01.lsShow directory contents (list the names of files).
02.ls -aList all files in a directory
03.ls -hList files along with file sizes
04.ls *.htmllist all files ending in .html
05.cdChange directory (e.g. cd /var/www will put you in the www directory)
06.cd ~Go to the home folder
07.cd /Go to the root directory
08.cd –Go to the previous directory
09.cd ..Move up one directory
10.mkdirCreate a new folder (e.g. mkdir myfoldername)
11.touchCreate a new file.
12.rmDelete a file (e.g. rm filename.html)
13.rmdirDelete a folder (e.g. rmdir foldername)
14.catShow the contents of a file (e.g. cat filename.html)
15.pwdShow current directory (full path to where you are right now).
16.cpCopy a file (e.g. cp index.html /mydirectory/index.html)
17.mvMove a file (e.g. mv index.html /mydirectory/index.html)
18.grepSearch for a string (e.g. grep “word” index.html). Searches for “word” in the index.html file
19.findSearch files and directories.
20.vi/nanoText editors.
21.historyShow last 50 used commands.
22.clearClear the terminal screen.
23.tarCreate & Unpack compressed archives.
24.wgetDownload files and store them in your current directory (e.g. wget https://website.com/filename.ext)
25.duGet file size.
26.vimOpen or create a file with the Vim text editor (e.g. vim filename.html).
28.nanoOpen or create a file with the nano text editor (e.g. nano filename.html)
29.zipCompress a folder (e.g. zip -r folder.zip folder). Takes “folder” and compresses it as a file called “folder.zip”
30.unzipDecompresses a folder (e.g. unzip folder.zip)
31.chmodChange a file’s permissions (e.g. chmod 604 folder). Use this Unix permissions calculator to determine which chmod command you should be using
32.netstatDisplay network connections
33.free-mDisplay your machine’s current memory usage
34.exitExit the remote server and return to your local machine SSH Keys
35.cat filename.txtcat the contents of filename.txt to your screen
36.taillike cat, but only reads the end of the file
37.tail /var/log/messagessee the last 20 (by default) lines of /var/log/message
38.tail -f /var/log/messageswatch the file continuously, while it’s being updat
39.tail -200 /var/log/messagesprint the last 200 lines of the file to the screen
40.morelike cat, but opens the file one screen at a time rather than all at once
41.more /etc/userdomainsbrowse through the userdomains file.
42.picofriendly, easy to use file editor
43.pico /home/burst/public_html/index.htmledit the index page for the user’s website.
44.vianother editor, tons of features
45.vi /home/burst/public_html/index.htmledit the index page for the user’s website.
46.grep root /etc/passwdshows all matches of root in /etc/passwd
47.grep -v root /etc/passwdshows all lines that do not match root
48.touch /home/burst/public_html/404.htmlcreate an empty file called 404.html in the directory /home/burst/public_html/
49.lncreate’s “links” between files and directories
50.ln -s /home/username/tmp/webalizer webstatsNow you can display http://www.yourdomain.com/webstats to show your webalizer stats online. You can delete the symlink (webstats) and it will not delete the original stats on the server.
51.rm filename.txtdeletes filename.txt, will more than likely ask if you really want to delete it
52.rm -f filename.txtdeletes filename.txt, will not ask for confirmation before deleting.
53.rm -rf tmp/recursively deletes the directory tmp, and all files in it, including subdirectories.
54.lastshows who logged in and when
55.last -20shows only the last 20 logins
56.last -20 -ashows last 20 logins, with the hostname in the last field
57.wshows who is currently logged in and where they are logged in from.
58.netstatshows all current network connections.
59.netstat -anshows all connections to the server, the source and destination ips and ports.
60.netstat -rnshows routing table for all ips bound to the server.
61.topshows live system processes in a nice table, memory information, uptime and other useful info.
62.psps is short for process status, which is similar to the top command. It’s used to show currently running processes and their PID.
63.ps U usernameshows processes for a certain user
64.ps auxshows all system processes
65.ps aux –forestshows all system processes like the above but organizes in a hierarchy that’s very useful!
66.fileattempts to guess what type of file a file is by looking at it’s content.
67.file *prints out a list of all files/directories in a directory
68.dushows disk usage.
69.du -shshows a summary, in human-readble form, of total disk space used in the current directory, including subdirectories.
70.du -sh *same thing, but for each file and directory. helpful when finding large files taking up space.
71.wcword count
72.wc -l filename.txttells how many lines are in filename.txt
73.cp filename filename.backupcopies filename to filename.backup
74.cp -a /home/burst/new_design/* /home/burst/public_html/copies all files, retaining permissions form one directory to another.
75.find * -type dxargs -i cp –verbose php.ini {}
76.killterminate a system process
77.kill -9 PID EGkill -9 431
78.kill PID EGkill 10550

Editing Text
  1. Change word: Change word/part of word to right of cursorcw
  2. Change line Change entire linecc
  3. Change line from specific character Change from cursor to end of lineC
Deleting Text
  1. Deleting One Character: Position the cursor over the character to be deleted and type xx X //To delete one character before the cursor
  2. Deleting a Word: Position the cursor at the beginning of the word and type dwdw
  3. Deleting a Line: Position the cursor anywhere on the line and type dd.dd
Cut, Copy & Paste

Copy, Cut and Paste operations can be done in either Normal or visual Mode.

Exiting
These commands are used to exit from the file.
```cmd
:w	    # Write (save) the file, but don't exit
:wq	    # Write (save) and quit
:wq!	# Force write (save) and quit
:q	    # Quit, but it fails if anything has changed
:q!	    # Quit and throw away for any changes

# 12. Vi Editor

Vi editor is the most popular text editor from the early days of Unix. Whereas Vim ( Vi IMproved ) is an improved version of vi editor to be used in CLI ( command line interface ) for mainly text editing tasks in many configuration files. Some of the other alternatives are Elvis, Nvi, Nano, Joe, and Vile. It has two main operation modes,

  1. Command Mode: It allows the entry of commands to manipulate text.
  2. Entry mode ( Or Insert mode ): It allows typed characters on the keyboard into the current file.

1. Start with Vi Editor

You can create a new file or open an existing file using vi filename command.

 vi <filename_NEW> or <filename_EXISTING> // Create a new file or open an existing file

 Example:
 vi first.txt

Let’s see how do you create file, enter the content and leave the CLI by saving the changes.

  1. Create a new file named first.txt
  2. Press i to enter the insert mode
  3. Enter the text “Hello World!”
  4. Save the text and exit by pressing :wq! command
  5. Check the entered text

2. Cursor movement

These commands will be used in Command mode.

Move cursor

You can use arrow keys ( left, right, up and down ) to move the cursor on the terminal. But you can also other keys for this behavior.

 h        # Move left
 j        # Move down
 k        # Move up
 l        # Move right

Jump one word

These commands used to jump one word at a time

w        # Jump forwards to the start of a word
W        # Jump forwards to the start of a WORD
e        # Jump forwards to the start of a word
E        # Jump forwards to the start of a WORD
b        # Jump backwords to the start of a word
B        # Jump backwords to the start of a WORD

Jump to start or end of a line or next line

These commands used to jump starting or ending of a line or a next line.

^        # Jump to the start of a current line
$        # Jump to the end of a current line
return   # Jump to the start of a next line

Move sides

These commands used to moves all sides of the screen

Backspace # Move cursor one character to the left
Spacebar  # Move cursor one character to the right
H ( High )   # Move cursor to the top of the screen
M ( Middle ) # Move cursor to the middle of the screen
L ( Low )    # Move cursor to the bottom of the screen
Paging and Scrolling

Paging is used to moves the cursor up or down through the text a full screen at a time. Whereas Scrolling happens line by line.

Ctrl + f     # move forward one full screen
Ctrl + b     # move backward one full screen
Ctrl + d     # move forward half a screen
Ctrl + u     # move backward half a screen

Inserting Text

These commands places vi in entry mode from command mode. First, you need to be in command mode to use the below commands.

Insert

i    # Insert text to the left of the cursor
I    # Insert text at the beginning of a line
ESC  # Exit insert mode

Append

a    # Insert ( or Append ) text to the right of the cursor
A    # Insert ( or Append ) text at the end of a line

Open a line

o    # Open a line below the current cursor position
O    # open a line above the current cursor position

Editing Text

  1. Change word: Change word/part of word to right of cursor
cw

Change line Change entire line

cc

Change line from specific character Change from cursor to end of line

c

Deleting Text

  1. Deleting One Character: Position the cursor over the character to be deleted and type x
x
X       //To delete one character before the cursor

Deleting a Word: Position the cursor at the beginning of the word and type dw

dw

Deleting a Line: Position the cursor anywhere on the line and type dd.

dd

Cut, Copy & Paste

Copy, Cut and Paste operations can be done in either Normal or visual Mode.

  1. Normal mode: This mode appears on click of Esc key.Copy There are various copy or yank commands based on amount of text to be copied. The y character is used to perform this operation.
    i. Copy an entire line: Just place the cursor at the beginning of the line and type yy
yy

ii.Copy three lines: Just place the cursor from where to start copying and type 3yy

3yy

iii. Copy word with trailing whitespace: Place the cursor at the beginning of the word and type yaw

yaw

iv. Copy word without trailing whitespace: Place the cursor at the beginning of the word and type yiw.

yiw

v. Copy right of the cursor: Copy text right of the cursor to the end of line using y$ command

y$

vi.Copy left of the cursor: Copy text left of the cursor to the end of line using y^ command

y^

vii. Copy text between the cursor and character: Copy text between the cursor and specified character.

ytx ( Copy until x and x is excluded )
yfx ( Copy until x and x is included )

Cut There are various cutting or deleting commands based on amount of text to be deleted. The d character is used to perform this operation.

i. Cut entire line: Cut the entire line where the cursor is located

dd

ii.Cut three lines: Cut the three lines starting from the place where cursor is located

3dd

iii.Cut right of the cursor: Cut the text from the right of the cursor till the end of line

d$

iii.Cut left of the cursor: Cut the text from the left of the cursor till the beginning of line

d^

Paste This operation is performed using p command to paste the selected text

p
  1. Visual Mode In this mode, first select the text using below keys
    1. v ( lowercase ): To select individual characters
    2. V ( uppercase ): To select the entire line
    3. Ctrl+v: To select by block
    and perform copy, cut and paste operations using y,d and p commands

Exiting

These commands are used to exit from the file.
```cmd
:w	    # Write (save) the file, but don't exit
:wq	    # Write (save) and quit
:wq!	# Force write (save) and quit
:q	    # Quit, but it fails if anything has changed
:q!	    # Quit and throw away for any changes
```

# 13. Process Management

Sl.No.CommandsDescription
01.psdisplay your currently active processes
02.topdisplay all running processes
03.kill pidkill process id pid
04.killall prockill all processes named proc *
05.bglists stopped or background jobs; resume a stopped job in the background
06.fgbrings the most recent job to foreground
07.fg nbrings job n to the foreground

# 14. SSH

  • ssh user@host – connect to host as user
  • ssh -p port user@host – connect to host on port port as user
  • ssh-copy-id user@host – add your key to host for user to enable a keyed or passwordless login

Searching

  • grep pattern files – search for pattern in files
  • grep -r pattern dir – search recursively for pattern in dir
  • command | grep pattern – search for pattern in the output of command
  • locate file – find all instances of file

System Info

Sl.No.CommandsDescription
01.dateshow the current date and time
02.calshow this month’s calendar
03.uptimeshow current uptime
04.wdisplay who is online
05.whoamiwho you are logged in as
06.finger userdisplay information about user
07.uname -ashow kernel information
08.cat /proc/cpuinfocpu information
09.cat /proc/meminfomemory information
10.man commandshow the manual for command
11.dfshow disk usage
12.dushow directory space usage
13.freeshow memory and swap usage
14.whereis appshow possible locations of app
15.which appshow which app will be run by default

Compression

Sl.No.CommandsDescription
01.tar cf file.tar filescreate a tar named file.tar containing files
02.tar xf file.tarextract the files from file.tar
03.tar czf file.tar.gz filescreate a tar with Gzip compression
04.tar xzf file.tar.gzextract a tar using Gzip
05.tar cjf file.tar.bz2create a tar with Bzip2 compression
06.tar xjf file.tar.bz2extract a tar using Bzip2
07.gzip filecompresses file and renames it to file.gz
08.gzip -d file.gzdecompresses file.gz back to file

Network

  • ping host – ping host and output results
  • whois domain – get whois information for domain
  • dig domain – get DNS information for domain
  • dig -x host – reverse lookup host
  • wget file – download file
  • wget -c file – continue a stopped download

Installation

  • dpkg -i pkg.deb – install a package (Debian)
  • rpm -Uvh pkg.rpm – install a package (RPM)

Install from source

  • ./configure
  • make
  • make install

Shortcuts

  • Ctrl+C – halts the current command
  • Ctrl+Z – stops the current command, resume with
  • fg in the foreground or bg in the background
  • Ctrl+D – log out of current session, similar to exit
  • Ctrl+W – erases one word in the current line
  • Ctrl+U – erases the whole line
  • Ctrl+R – type to bring up a recent command
  • !! – repeats the last command
  • exit – log out of current session

Command History

!!            # Run the last command

touch foo.sh
chmod +x !$   # !$ is the last argument of the last command i.e. foo.sh

Navigating Directories

pwd                       # Print current directory path
ls                        # List directories
ls -a|--all               # List directories including hidden
ls -l                     # List directories in long form
ls -l -h|--human-readable # List directories in long form with human readable sizes
ls -t                     # List directories by modification time, newest first
stat foo.txt              # List size, created and modified timestamps for a file
stat foo                  # List size, created and modified timestamps for a directory
tree                      # List directory and file tree
tree -a                   # List directory and file tree including hidden
tree -d                   # List directory tree
cd foo                    # Go to foo sub-directory
cd                        # Go to home directory
cd ~                      # Go to home directory
cd -                      # Go to last directory
pushd foo                 # Go to foo sub-directory and add previous directory to stack
popd                      # Go back to directory in stack saved by `pushd`

Creating Directories

mkdir foo                        # Create a directory
mkdir foo bar                    # Create multiple directories
mkdir -p|--parents foo/bar       # Create nested directory
mkdir -p|--parents {foo,bar}/baz # Create multiple nested directories

mktemp -d|--directory            # Create a temporary directory

Moving Directories

cp -R|--recursive foo bar                               # Copy directory
mv foo bar                                              # Move directory

rsync -z|--compress -v|--verbose /foo /bar              # Copy directory, overwrites destination
rsync -a|--archive -z|--compress -v|--verbose /foo /bar # Copy directory, without overwriting destination
rsync -avz /foo username@hostname:/bar                  # Copy local directory to remote directory
rsync -avz username@hostname:/foo /bar                  # Copy remote directory to local directory

Deleting Directories

rmdir foo                        # Delete non-empty directory
rm -r|--recursive foo            # Delete directory including contents
rm -r|--recursive -f|--force foo # Delete directory including contents, ignore nonexistent files and never prompt

Creating Files

touch foo.txt          # Create file or update existing files modified timestamp
touch foo.txt bar.txt  # Create multiple files
touch {foo,bar}.txt    # Create multiple files
touch test{1..3}       # Create test1, test2 and test3 files
touch test{a..c}       # Create testa, testb and testc files

mktemp                 # Create a temporary file

Standard Output, Standard Error and Standard Input

echo "foo" > bar.txt       # Overwrite file with content
echo "foo" >> bar.txt      # Append to file with content

ls exists 1> stdout.txt    # Redirect the standard output to a file
ls noexist 2> stderror.txt # Redirect the standard error output to a file
ls 2>&1 out.txt            # Redirect standard output and error to a file
ls > /dev/null             # Discard standard output and error

read foo                   # Read from standard input and write to the variable foo

Moving Files

cp foo.txt bar.txt                                # Copy file
mv foo.txt bar.txt                                # Move file

rsync -z|--compress -v|--verbose /foo.txt /bar    # Copy file quickly if not changed
rsync z|--compress -v|--verbose /foo.txt /bar.txt # Copy and rename file quickly if not changed

Deleting Files

rm foo.txt            # Delete file
rm -f|--force foo.txt # Delete file, ignore nonexistent files and never prompt

Reading Files

cat foo.txt            # Print all contents
less foo.txt           # Print some contents at a time (g - go to top of file, SHIFT+g, go to bottom of file, /foo to search for 'foo')
head foo.txt           # Print top 10 lines of file
tail foo.txt           # Print bottom 10 lines of file
open foo.txt           # Open file in the default editor
wc foo.txt             # List number of lines words and characters in the file

Finding Files

Find binary files for a command.

type wget                                  # Find the binary
which wget                                 # Find the binary
whereis wget                               # Find the binary, source, and manual page files

locate uses an index and is fast.

updatedb                                   # Update the index

locate foo.txt                             # Find a file
locate --ignore-case                       # Find a file and ignore case
locate f*.txt                              # Find a text file starting with 'f'

find doesn’t use an index and is slow.

find /path -name foo.txt                   # Find a file
find /path -iname foo.txt                  # Find a file with case insensitive search
find /path -name "*.txt"                   # Find all text files
find /path -name foo.txt -delete           # Find a file and delete it
find /path -name "*.png" -exec pngquant {} # Find all .png files and execute pngquant on it
find /path -type f -name foo.txt           # Find a file
find /path -type d -name foo               # Find a directory
find /path -type l -name foo.txt           # Find a symbolic link
find /path -type f -mtime +30              # Find files that haven't been modified in 30 days
find /path -type f -mtime +30 -delete      # Delete files that haven't been modified in 30 days

Find in Files

grep 'foo' /bar.txt                         # Search for 'foo' in file 'bar.txt'
grep 'foo' /bar -r|--recursive              # Search for 'foo' in directory 'bar'
grep 'foo' /bar -R|--dereference-recursive  # Search for 'foo' in directory 'bar' and follow symbolic links
grep 'foo' /bar -l|--files-with-matches     # Show only files that match
grep 'foo' /bar -L|--files-without-match    # Show only files that don't match
grep 'Foo' /bar -i|--ignore-case            # Case insensitive search
grep 'foo' /bar -x|--line-regexp            # Match the entire line
grep 'foo' /bar -C|--context 1              # Add N line of context above and below each search result
grep 'foo' /bar -v|--invert-match           # Show only lines that don't match
grep 'foo' /bar -c|--count                  # Count the number lines that match
grep 'foo' /bar -n|--line-number            # Add line numbers
grep 'foo' /bar --colour                    # Add colour to output
grep 'foo\|bar' /baz -R                     # Search for 'foo' or 'bar' in directory 'baz'
grep --extended-regexp|-E 'foo|bar' /baz -R # Use regular expressions
egrep 'foo|bar' /baz -R                     # Use regular expressions

Replace in Files

sed 's/fox/bear/g' foo.txt               # Replace fox with bear in foo.txt and output to console
sed 's/fox/bear/gi' foo.txt              # Replace fox (case insensitive) with bear in foo.txt and output to console
sed 's/red fox/blue bear/g' foo.txt      # Replace red with blue and fox with bear in foo.txt and output to console
sed 's/fox/bear/g' foo.txt > bar.txt     # Replace fox with bear in foo.txt and save in bar.txt
sed 's/fox/bear/g' foo.txt -i|--in-place # Replace fox with bear and overwrite foo.txt

Symbolic Links

ln -s|--symbolic foo bar            # Create a link 'bar' to the 'foo' folder
ln -s|--symbolic -f|--force foo bar # Overwrite an existing symbolic link 'bar'
ls -l                               # Show where symbolic links are pointing

Compressing Files

zip

Compresses one or more files into *.zip files.

zip foo.zip /bar.txt                # Compress bar.txt into foo.zip
zip foo.zip /bar.txt /baz.txt       # Compress bar.txt and baz.txt into foo.zip
zip foo.zip /{bar,baz}.txt          # Compress bar.txt and baz.txt into foo.zip
zip -r|--recurse-paths foo.zip /bar # Compress directory bar into foo.zip

gzip

Compresses a single file into *.gz files.

gzip /bar.txt foo.gz           # Compress bar.txt into foo.gz and then delete bar.txt
gzip -k|--keep /bar.txt foo.gz # Compress bar.txt into foo.gz

tar -c

Compresses (optionally) and combines one or more files into a single *.tar, *.tar.gz, *.tpz or *.tgz file.

tar -c|--create -z|--gzip -f|--file=foo.tgz /bar.txt /baz.txt # Compress bar.txt and baz.txt into foo.tgz
tar -c|--create -z|--gzip -f|--file=foo.tgz /{bar,baz}.txt    # Compress bar.txt and baz.txt into foo.tgz
tar -c|--create -z|--gzip -f|--file=foo.tgz /bar              # Compress directory bar into foo.tgz

Decompressing Files

unzip

unzip foo.zip          # Unzip foo.zip into current directory

gunzip

gunzip foo.gz           # Unzip foo.gz into current directory and delete foo.gz
gunzip -k|--keep foo.gz # Unzip foo.gz into current directory

tar -x

tar -x|--extract -z|--gzip -f|--file=foo.tar.gz # Un-compress foo.tar.gz into current directory
tar -x|--extract -f|--file=foo.tar              # Un-combine foo.tar into current directory

Disk Usage

df                     # List disks, size, used and available space
df -h|--human-readable # List disks, size, used and available space in a human readable format

du                     # List current directory, subdirectories and file sizes
du /foo/bar            # List specified directory, subdirectories and file sizes
du -h|--human-readable # List current directory, subdirectories and file sizes in a human readable format
du -d|--max-depth      # List current directory, subdirectories and file sizes within the max depth
du -d 0                # List current directory size

Memory Usage

free                   # Show memory usage
free -h|--human        # Show human readable memory usage
free -h|--human --si   # Show human readable memory usage in power of 1000 instead of 1024
free -s|--seconds 5    # Show memory usage and update continuously every five seconds

Packages

apt update             # Refreshes repository index
apt search wget        # Search for a package
apt show wget          # List information about the wget package
apt install wget       # Install the wget package
apt remove wget        # Removes the wget package
apt upgrade            # Upgrades all upgradable packages

Shutdown and Reboot

shutdown                     # Shutdown in 1 minute
shutdown now "Cya later"     # Immediately shut down
shutdown +5 "Cya later"      # Shutdown in 5 minutes

shutdown --reboot            # Reboot in 1 minute
shutdown -r now "Cya later"  # Immediately reboot
shutdown -r +5 "Cya later"   # Reboot in 5 minutes

shutdown -c                  # Cancel a shutdown or reboot

reboot                       # Reboot now
reboot -f                    # Force a reboot

Identifying Processes

top                    # List all processes interactively
htop                   # List all processes interactively
ps all                 # List all processes
pidof foo              # Return the PID of all foo processes

CTRL+Z                 # Suspend a process running in the foreground
bg                     # Resume a suspended process and run in the background
fg                     # Bring the last background process to the foreground
fg 1                   # Bring the background process with the PID to the foreground

sleep 30 &             # Sleep for 30 seconds and move the process into the background
jobs                   # List all background jobs
jobs -p                # List all background jobs with their PID

lsof                   # List all open files and the process using them
lsof -itcp:4000        # Return the process listening on port 4000

Process Priority

Process priorities go from -20 (highest) to 19 (lowest).

nice -n -20 foo        # Change process priority by name
renice 20 PID          # Change process priority by PID
ps -o ni PID           # Return the process priority of PID

Killing Processes

CTRL+C                 # Kill a process running in the foreground
kill PID               # Shut down process by PID gracefully. Sends TERM signal.
kill -9 PID            # Force shut down of process by PID. Sends SIGKILL signal.
pkill foo              # Shut down process by name gracefully. Sends TERM signal.
pkill -9 foo           # force shut down process by name. Sends SIGKILL signal.
killall foo            # Kill all process with the specified name gracefully.

Date & Time

date                   # Print the date and time
date --iso-8601        # Print the ISO8601 date
date --iso-8601=ns     # Print the ISO8601 date and time

time tree              # Time how long the tree command takes to execute

Scheduled Tasks

   *      *         *         *           *
Minute, Hour, Day of month, Month, Day of the week
crontab -l                 # List cron tab
crontab -e                 # Edit cron tab in Vim
crontab /path/crontab      # Load cron tab from a file
crontab -l > /path/crontab # Save cron tab to a file

* * * * * foo              # Run foo every minute
*/15 * * * * foo           # Run foo every 15 minutes
0 * * * * foo              # Run foo every hour
15 6 * * * foo             # Run foo daily at 6:15 AM
44 4 * * 5 foo             # Run foo every Friday at 4:44 AM
0 0 1 * * foo              # Run foo at midnight on the first of the month
0 0 1 1 * foo              # Run foo at midnight on the first of the year

at -l                      # List scheduled tasks
at -c 1                    # Show task with ID 1
at -r 1                    # Remove task with ID 1
at now + 2 minutes         # Create a task in Vim to execute in 2 minutes
at 12:34 PM next month     # Create a task in Vim to execute at 12:34 PM next month
at tomorrow                # Create a task in Vim to execute tomorrow

HTTP Requests

curl https://example.com                               # Return response body
curl -i|--include https://example.com                  # Include status code and HTTP headers
curl -L|--location https://example.com                 # Follow redirects
curl -o|--remote-name foo.txt https://example.com      # Output to a text file
curl -H|--header "User-Agent: Foo" https://example.com # Add a HTTP header
curl -X|--request POST -H "Content-Type: application/json" -d|--data '{"foo":"bar"}' https://example.com # POST JSON
curl -X POST -H --data-urlencode foo="bar" http://example.com                           # POST URL Form Encoded

wget https://example.com/file.txt .                            # Download a file to the current directory
wget -O|--output-document foo.txt https://example.com/file.txt # Output to a file with the specified name

Network Troubleshooting

ping example.com            # Send multiple ping requests using the ICMP protocol
ping -c 10 -i 5 example.com # Make 10 attempts, 5 seconds apart

ip addr                     # List IP addresses on the system
ip route show               # Show IP addresses to router

netstat -i|--interfaces     # List all network interfaces and in/out usage
netstat -l|--listening      # List all open ports

traceroute example.com      # List all servers the network traffic goes through

mtr -w|--report-wide example.com                                    # Continually list all servers the network traffic goes through
mtr -r|--report -w|--report-wide -c|--report-cycles 100 example.com # Output a report that lists network traffic 100 times

nmap 0.0.0.0                # Scan for the 1000 most common open ports on localhost
nmap 0.0.0.0 -p1-65535      # Scan for open ports on localhost between 1 and 65535
nmap 192.168.4.3            # Scan for the 1000 most common open ports on a remote IP address
nmap -sP 192.168.1.1/24     # Discover all machines on the network by ping'ing them

DNS

host example.com            # Show the IPv4 and IPv6 addresses

dig example.com             # Show complete DNS information

cat /etc/resolv.conf        # resolv.conf lists nameservers

Hardware

lsusb                  # List USB devices
lspci                  # List PCI hardware
lshw                   # List all hardware

Terminal Multiplexers

Start multiple terminal sessions. Active sessions persist reboots. tmux is more modern than screen.

tmux             # Start a new session (CTRL-b + d to detach)
tmux ls          # List all sessions
tmux attach -t 0 # Reattach to a session

screen           # Start a new session (CTRL-a + d to detach)
screen -ls       # List all sessions
screen -R 31166  # Reattach to a session

exit             # Exit a session

Secure Shell Protocol (SSH)

ssh hostname                 # Connect to hostname using your current user name over the default SSH port 22
ssh -i foo.pem hostname      # Connect to hostname using the identity file
ssh user@hostname            # Connect to hostname using the user over the default SSH port 22
ssh user@hostname -p 8765    # Connect to hostname using the user over a custom port
ssh ssh://user@hostname:8765 # Connect to hostname using the user over a custom port

Set default user and port in ~/.ssh/config, so you can just enter the name next time:

$ cat ~/.ssh/config
Host name
  User foo
  Hostname 127.0.0.1
  Port 8765
$ ssh name

Secure Copy

scp foo.txt ubuntu@hostname:/home/ubuntu # Copy foo.txt into the specified remote directory

Bash Profile

  • bash – .bashrc
  • zsh – .zshrc
# Always run ls after cd
function cd {
  builtin cd "$@" && ls
}

# Prompt user before overwriting any files
alias cp='cp --interactive'
alias mv='mv --interactive'
alias rm='rm --interactive'

# Always show disk usage in a human readable format
alias df='df -h'
alias du='du -h'

Bash Script

Variables

#!/bin/bash

foo=123                # Initialize variable foo with 123
declare -i foo=123     # Initialize an integer foo with 123
declare -r foo=123     # Initialize readonly variable foo with 123
echo $foo              # Print variable foo
echo ${foo}_'bar'      # Print variable foo followed by _bar
echo ${foo:-'default'} # Print variable foo if it exists otherwise print default

export foo             # Make foo available to child processes
unset foo              # Make foo unavailable to child processes

Environment Variables

#!/bin/bash

env        # List all environment variables
echo $PATH # Print PATH environment variable

Functions

#!/bin/bash

greet() {
  local world = "World"
  echo "$1 $world"
  return "$1 $world"
}
greet "Hello"
greeting=$(greet "Hello")

Exit Codes

#!/bin/bash

exit 0   # Exit the script successfully
exit 1   # Exit the script unsuccessfully
echo $?  # Print the last exit code

Conditional Statements

Boolean Operators

  • $foo – Is true
  • !$foo – Is false

Numeric Operators

  • -eq – Equals
  • -ne – Not equals
  • -gt – Greater than
  • -ge – Greater than or equal to
  • -lt – Less than
  • -le – Less than or equal to
  • -e foo.txt – Check file exists
  • -z foo – Check if variable exists

String Operators

  • = – Equals
  • == – Equals
  • -z – Is null
  • -n – Is not null
  • < – Is less than in ASCII alphabetical order
  • > – Is greater than in ASCII alphabetical order

If Statements

#!/bin/bash

if [[$foo = 'bar']]; then
  echo 'one'
elif [[$foo = 'bar']] || [[$foo = 'baz']]; then
  echo 'two'
elif [[$foo = 'ban']] && [[$USER = 'bat']]; then
  echo 'three'
else
  echo 'four'
fi

Inline If Statements

#!/bin/bash

[[ $USER = 'rehan' ]] && echo 'yes' || echo 'no'

While Loops

#!/bin/bash

declare -i counter
counter=10
while [$counter -gt 2]; do
  echo The counter is $counter
  counter=counter-1
done

For Loops

#!/bin/bash

for i in {0..10..2}
  do
    echo "Index: $i"
  done

for filename in file1 file2 file3
  do
    echo "Content: " >> $filename
  done

for filename in *;
  do
    echo "Content: " >> $filename
  done

Case Statements

#!/bin/bash

echo 'What's the weather like tomorrow?'
read weather

case $weather in
  sunny | warm ) echo 'Nice weather: ' $weather
  ;;
  cloudy | cool ) echo 'Not bad weather: ' $weather
  ;;
  rainy | cold ) echo 'Terrible weather: ' $weather
  ;;
  * ) echo 'Don't understand'
  ;;
esac

The Linuc Basics is a github repository by Pradeep Kumar