Linux

1 - SYSTEM INFORMATION

uname -a # Display Linux system information

uname -r # Display kernel release information

cat /etc/redhat-release # Show which version of redhat installed

uptime # Show how long the system has been running + load

hostname # Show system host name

hostname -I # Display the IP addresses of the host

last reboot # Show system reboot history

date # Show the current date and time

cal # Show this month's calendar

w # Display who is online

whoami # Who you are logged in as

2 - HARDWARE INFORMATION

dmesg # Display messages in kernel ring buffer

cat /proc/cpuinfo # Display CPU information

cat /proc/meminfo # Display memory information

free -h # Display free and used memory ( -h for human readable,

-m for MB, -g for GB.)

lspci -tv # Display PCI devices

lsusb -tv # Display USB devices

dmidecode # Display DMI/SMBIOS (hardware info) from the BIOS

hdparm -i /dev/sda # Show info about disk sda

hdparm -tT /dev/sda # Perform a read speed test on disk sda

badblocks -s /dev/sda # Test for unreadable blocks on disk sda

3 - PERFORMANCE MONITORING AND STATISTICS

top # Display and manage the top processes

htop # Interactive process viewer (top alternative)

mpstat 1 # Display processor related statistics

vmstat 1 # Display virtual memory statistics

iostat 1 # Display I/O statistics

tail 100 /var/log/messages # Display the last 100 syslog messages (Use

/var/log/syslog for Debian based systems.)

tcpdump -i eth0 # Capture and display all packets on interface eth0

tcpdump -i eth0 'port 80' # Monitor all traffic on port 80 ( HTTP )

lsof # List all open files on the system

lsof -u user # List files opened by user

free -h # Display free and used memory ( -h for human

readable, -m for MB, -g for GB.)

watch df -h # Execute "df -h", showing periodic updates

4 - USER INFORMATION AND MANAGEMENT

id # Display the user and group ids of your

current user.

last # Display the last users who have logged onto

the system.

who # Show who is logged into the system.

w # Show who is logged in and what they are

doing.

groupadd test # Create a group named "test".

useradd -c "John Smith" -m john # Create an account named john, with a

comment of "John Smith" and create the user's

home directory.

userdel john # Delete the john account.

usermod -aG sales john # Add the john account to the sales group

5 - FILE AND DIRECTORY COMMANDS

ls -al # List all files in a long listing (detailed) format

pwd # Display the present working directory

mkdir directory # Create a directory

rm file # Remove (delete) file

rm -r directory # Remove the directory and its contents

recursively

rm -f file # Force removal of file without prompting for

confirmation

rm -rf directory # Forcefully remove directory recursively

cp file1 file2 # Copy file1 to file2

cp -r source_directory

destination

# Copy source_directory recursively to

destination . If destination exists, copy

source_directory into destination ,

otherwise create destination with the

contents of source_directory .

mv file1 file2 # Rename or move file1 to file2 . If file2 is

an existing directory, move file1 into directory

file2

ln -s /path/to/file linkname # Create symbolic link to linkname

touch file # Create an empty file or update the access

and modification times of file.

cat file # View the contents of file

less file # Browse through a text file

head file # Display the first 10 lines of file

tail file # Display the last 10 lines of file

tail -f file # Display the last 10 lines of file and "follow"

the file as it grows.

6 - PROCESS MANAGEMENT

ps # Display your currently running processes

ps -ef # Display all the currently running processes on the

system.

ps -ef | grep processname # Display process information for processname

top # Display and manage the top processes

htop # Interactive process viewer (top alternative)

kill pid # Kill process with process ID of p id

killall processname # Kill all processes named processname

program & # Start program in the background

bg # Display stopped or background jobs

fg # Brings the most recent background job to

foreground

fg n # Brings job n to the foreground

7 - FILE PERMISSIONS

PERMISSION EXAMPLE

U G W

rwx rwx rwx chmod 777 filename # Use sparingly!

rwx rwx r-x chmod 775 filename

rwx r-x r-x chmod 755 filename

rw- rw- r-- chmod 664 filename

rw- r-- r-- chmod 644 filename

LEGEND

U = User

G = Group

W = World

r = Read

w = write

x = execute

- = no access

8 - NETWORKING

ifconfig -a # Display all network interfaces and ip address

ifconfig eth0 # Display eth0 address and details

ethtool eth0 # Query or control network driver and hardware settings

ping host # Send ICMP echo request to host

whois domain # Display whois information for domain

dig domain # Display DNS information for domain

dig -x IP_ADDRESS # Reverse lookup of IP_ADDRESS

host domain # Display DNS ip address for domain

hostname -i # Display the network address of the host name.

hostname -I # Display all local ip addresses

wget http://domain.com/file # Download http://domain.com/file

netstat -nutlp # Display listening tcp and udp ports and corresponding

programs

9 - ARCHIVES (TAR FILES)

tar cf archive.tar directory # Create tar named archive.tar containing

directory .

tar xf archive.tar # Extract the contents from archive.tar .

tar czf archive.tar.gz directory # Create a gzip compressed tar file name

archive.tar.gz .

tar xzf archive.tar.gz # Extract a gzip compressed tar file.

tar cjf archive.tar.bz2 directory # Create a tar file with bzip2 compression

tar xjf archive.tar.bz2 # Extract a bzip2 compressed tar file.

10 - INSTALLING PACKAGES

yum search keyword # Search for a package by keyword .

yum install package # Install package .

yum info package # Display description and summary information

about package .

rpm -i package.rpm # Install package from local file named

package.rpm

yum remove package # Remove/uninstall package .

tar zxvf sourcecode.tar.gz

cd sourcecode

./configure

make

make install

# Install software from source code.

11 - SEARCH

grep pattern file # Search for pattern in file

grep -r pattern directory # Search recursively for pattern in directory

locate name # Find files and directories by name

find /home/john -name

'prefix*'

# Find files in /home/john that start with "prefix".

find /home -size +100M # Find files larger than 100MB in /home

12 - SSH LOGINS

ssh host # Connect to host as your local username.

ssh user@host # Connect to host as user

ssh -p port user@host # Connect to host using port

13 - FILE TRANSFERS

scp file.txt server:/tmp # Secure copy file.txt to the /tmp folder on

server

scp server:/var/www/*.html /tmp # Copy *.html files from server to the local

/tmp folder.

scp -r server:/var/www /tmp # Copy all files and directories recursively from

server to the current system's /tmp folder.

rsync -a /home /backups/ # Synchronize /home to /backups/home

rsync -avz /home

server:/backups/

# Synchronize files/directories between the local

and remote system with compression enabled

14 - DISK USAGE

df -h # Show free and used space on mounted filesystems

df -i # Show free and used inodes on mounted filesystems

fdisk -l # Display disks partitions sizes and types

du -ah # Display disk usage for all files and directories in

human readable format

du -sh # Display total disk usage off the current directory

15 - DIRECTORY NAVIGATION

cd .. # To go up one level of the directory tree. (Change into

the parent directory.)

cd # Go to the $HOME directory

cd /etc # Change to the /etc directory

Some Useful Linux Commands

A. System Related Commands

These commands are used to view and manage Linux system-related information.

  1.  uname : Displays linux system information. With -a switch you can view all the information, with -r switch you can view kernel release information and with -o you can view OS information
  2.  cat /etc/redhat_release : Shows which version of redhat installed 
  3.  uptime : Shows how long the system has been running
  4.  hostname : Shows system host name. With -i switch you can view the ip address of the machine and with -d you can view the domain name
  5.  last reboot : Shows system reboot history
  6.  date : Shows the current date and time. You can specify the format you want to view the date as well. As an example, by using 'date +%D' you can view the date in 'MM/DD/YY' format
  7.  cal : Shows the calendar of the current month. With -y switch you can view the calendar of the whole current year
  8.  w : Displays who is logged on and what they are doing
  9.  whoami : Shows current user id
  10.  finger user : Displays information about user
  11.  reboot : Reboots the system
  12.  shutdown : Shuts down the system

B. Hardware Related Commands

These commands are used to view and manage hardware-related aspects of the Linux machine.

  1.  dmesg : Displays all the messages from Kernel ring buffer. With -k switch you can view kernel messages and with -u you can view userspace messages
  2.  cat /proc/cpuinfo : Displays information about processes and CPUs of the system
  3.  cat /proc/meminfo : Displays details on hardware memory
  4.  cat /proc/interrupts : Lists the number of interrupts per CPU per I/O device
  5.  lshw : Displays information on hardware configuration of the system. But this command must be run as super user or it will only report partial information
  6.  lsblk : Displays block device related information of the machine. With -a you can view all block devices
  7.  free -m : Shows used and free memory (-m for MB)
  8.  lspci -tv : Shows information on PCI buses devices
  9.  lsusb -tv : Shows information on USB devices
  10.  dmidecode : Shows hardware info from the BIOS (vendor details)
  11.  hdparm -i /dev/sda : Shows info about disk sda
    hdparm -tT /dev/sda   : Performs a read speed test on disk sda
  1.  badblocks -s /dev/sda : Tests for unreadable blocks on disk sda

C. Statistic Related Commands

These set of commands are used to view various kinds of stats of the Linux system

  1.  mpstat 1 : Displays processors related statistics
  2.  vmstat 2 : Displays virtual memory statistics
  3.  iostat 2 : Displays I/O statistics
  4.  tail -n 500 /var/log/messages : Displays the last 500 kernel/syslog messages
  5.  tcpdump -i eth1 : Captures all packets flow on interface eth1. With -w switch you can specify a file where you can direct the output to
    tcpdump -i eth0 'port 80'     : Monitors all traffic on port 80 on interface eth0
  1.  lsof : Lists all open files belonging to all active processes
    lsof -u testuser              : Lists files opened by a specific user
  1.  free -m : Shows RAM memory details
  2.  watch df -h : Watches changeable disk usage continuously

D. User-Related Commands

These commands are used to manage Linux users

  1.  id : Shows the active user and group information. With -G switch you can view the IDs of groups
  2.  last : Shows a list of last logins on the system. Using -a switch you can add the hostname to the last column of the output
  3.  who : Shows who is logged on the system
  4.  groupadd admin : Adds the group "admin"
  5.  useradd -c "Sam Tomshi" -g admin -m sam : Creates user "sam" and adds to group "admin"
  6.  userdel sam : Deletes user sam
  7.  adduser sam : Adds user "sam"
  8.  usermod : Modifies user information
  9.  passwd user1 : Changes the password of user1

 

E. File Related Commands

These commands are used to handle files and directories

  1.  ls -al : Displays all information about files/directories. This includes displaying all hidden files as well
  2.  pwd : Shows current directory path
  3.  mkdir directory-name : Creates a directory
  4.  rm file-name : Deletes file
    rm -r directory-name                   : Deletes directory recursively 

    rm -f file-name                        : Forcefully removes file

    rm -rf directory-name                  : Forcefully removes directory recursively
  1.  cp file1 file2 : Copies linux files, here file1 to file2
    cp -r dir1 dir2                        : Copies dir1 to dir2, creates dir2 if it doesn't  exist
  1.  mv file1 file2 : Moves files from one place to another/renames file1 to file2
  2.  ln -s /path/to/file-name link-name : Creates a symbolic link to file-name
  3.  touch file : Creates empty file
  4.  cat file : Prints the file content in terminal
  5.  more file : Display the contents of file
  6.  head file : Display the first 10 lines of file
  7.  tail file : Outputs the last 10 lines of file
    tail -f file                           : Outputs the contents of file as it grows starting with the last 10 lines
  1.  gpg -c file : Encrypts file
    gpg file.gpg                           : Decrypts file
  1.  cksum file : View the checksum of the file
  2.  diff file1 file2 : View the differences between contents of file1 and file2
  3.  ln -s link file : Create a soft link named link to the file
  4.  sort : Sorts files in alphabetical order
  5.  uniq : Compares adjacent lines in a file and removes/reports any duplicate lines
  6.  wc : Counts number of words/lines
  7.  dir : Lists the content of the directory
  8.  tee : Command for chaining and redirection
  9.  tr : Command for translating characters

 

F. Process Related Commands

These commands are used to handle Linux processes

  1.  ps : Displays your currently active processes
    ps aux | grep 'telnet'   : Displays all process ids related to telnet process
  1.  pmap : Display Memory map of process
  2.  top : Display all running processes and cpu/memory usage
  3.  kill pid : Kills process with mentioned pid
  4.  killall proc : Kills all processes named proc
  5.  pkill processname : Sends kill signal to a process with its name
  6.  bg : Resumes suspended jobs without bringing them to foreground
  7.  fg : Brings the most recent job to foreground
    fg n                     : Brings job n to the foreground

 

G. File Permission Related Commands

These commands are used to change permissions of the files

  1.  chmod octal file-name      : Changes the permissions of file to octal
    chmod 777 /data/test.c                   : Sets rwx permission for owner , group and others

    chmod 755 /data/test.c                   : Sets rwx permission for owner and rx for group and others
  1.  chown owner-user file : Changes owner of the file
    chown owner-user:owner-group  file-name  : Changes owner and group owner of the file

    chown owner-user:owner-group directory   : Changes owner and group owner of the directory
  1.  chgrp group1 file : Changes the group ownership of the file to group1

 

G. Network Related Commands

These commands are used to view and edit network configurations related aspects of the system

  1.  ifconfig -a : Displays all network interface and set ip address
  2.  ifconfig eth0 : Displays eth0 ethernet port ip address and details
  3.  ip addr show : Display all network interfaces and ip addresses
  4.  ip address add 192.168.0.1 dev eth0 : Sets ip address of eth0 device
  5.  ethtool eth0 : Linux tool to show ethernet status
  6.  mii-tool eth0 : Linux tool to show eth0 status
  7.  ping host : Sends echo requests to the host to test ipv4 connection
  8.  whois domain : Gets who is information for domain
  9.  dig domain : Gets DNS nameserver information for domain
    dig -x host        : Reverse lookup host 
  1.  host google.com : Lookup DNS ip address for the name
  2.  hostname -i : Lookup local ip address
  3.  wget file : Downloads file
  4.  netstat -tupl : Lists all active listening ports
  5.  nslookup : Resolves domain names to IP addresses

 

H. Compression / Archive Related Commands

These commands are used to compress and decompress files

  1.  tar cf home.tar home : Creates a tar named home.tar containing home/
    tar xf file.tar               : Extracts the files from file.tar

    tar czf  file.tar.gz  files   : Creates a tar with gzip compression
  1.  gzip file : Compresses file and renames it to file.gz
  2.  bzip2 -z file : Compresses file and renames it to file.bz2
    bzip2 -d file.bz2             : Decompress the file

 

I. Package Installation Related Commands

These commands are used to manage Linux packages

  1.  rpm -i pkgname.rpm : Installs rpm based package
    rpm -e pkgname         : Removes package
  1.  make : Install from source file

 

J. Search Related Commands

These commands are used to search for files and patterns

  1.  grep pattern files : Searches for pattern in files
    grep -r pattern dir             : Searches recursively for pattern in dir
  1.  locate file : Finds all instances of file
  2.  find /home/tom -name 'index*' : Finds file names that start with "index" inside /home/tom directory
    find /home -size +10000k        : Finds files larger than 10000k in /home

 

K. Login Related Commands

These commands are used to log into another host

  1.  ssh user@host : Securely connect to a host as user
    ssh -p port $ user@host    : Connects to host using specific port
  1.  telnet host : Connects to the system using telnet port

 

L. File Transfer Related Commands

These commands are used to copy files from one system to another system

  1.  scp file.txt server2:/tmp : Secure copy file.txt to remote host  /tmp folder
    scp nixsavy@server2:/www/*.html /www/tmp     : Copies *.html files from remote host to current host /www/tmp folder

    scp -r nixsavy@server2:/www   /www/tmp       : Copies all files and folders recursively from remote server to the current system /www/tmp folder
  1.  rsync -a /home/apps /backup/ : Synchronizes source to destination
    rsync -avz /home/apps $ saurav@192.168.10.1:/backup  : Synchronize files/directories between the local and remote system with compression enabled

 

M. Disk Usage Related Commands

These commands are used to view disk statistics

  1.  df -h : Shows free space on mounted filesystems
     df -i                     : Shows free inodes on mounted filesystems
  1.  fdisk -l : Shows disks partitions sizes and types
  2.  du -ah : Displays disk usage in human readable form
     du -sh                         : Displays total disk usage on the current directory
  1.  findmnt : Displays target mount point for all filesystems
  2.  mount device-path mount-point : Mounts a device to the device-path

 

N. Directory Traverse Related Commands

These commands are used to change the directory

  1.  cd .. : Goes up one level of the directory tree
     cd             : Goes to $HOME directory
     cd /test       : Changes to /test directory

———————————————–END———————————————–


#Unable To release lock

E: Could not get lock /var/lib/dpkg/lock - open (11 Resource temporarily unavailable) E: Unable to lock the administration directory (/var/lib/dpkg/) is another process using it

If you try

ps aux | grep apt

that will catch processes containing the word apt, at least. If you see an apt-get process or an aptitude process that looks stuck, you can try

kill processnumber

and if that doesn’t work try

kill -9 processnumber

This should kill the process and may remove the lock. Killing an apt or aptitude process is harmless unless it is actually in the middle of package installation. In any case, if the process got stuck, you probably don’t have a choice but to kill it.

Killing a dpkg process directly, if present, is not a good idea, because if dpkg is active, it is probably manipulating the package database, and killing it may leave the package database in an inconsistent state; i.e. corrupted.

Killing an apt-get or aptitude process is in general much safer.

This should be used as last resort. If you use this carelessly you can end up with a broken system. Please try the other answers first before doing this.

You can delete the lock file with the following command:

sudo rm /var/lib/apt/lists/lock

You may also need to delete the lock file in the cache directory

sudo rm /var/cache/apt/archives/lock sudo rm /var/lib/dpkg/lock

After that, try opening Synaptic again.


#Checking File Sizes

ls -l filename /* Size of the file*/ ls -l *        /* Size of All the files in the current directory */ ls -al *       /* Size of All the files including hidden files in the current directory */ ls -al dir/    /* Size of All the files including hidden files in the 'dir' directory */

ls command will not list the actual size of directories. we need to use du for this purpose.

Checking Directory sizes

du -sh directory_name    /* Gives you the summarized(-s) size of the directory in human readable(-h) format*/ du -bsh *                /* Gives you the apparent(-b) summarized(-s) size of all the files and directories in the current directory in human readable(-h) format*/

Including -h option in any of the above commands (for Ex: ls -lh * or du -sh) will give you size in human readable format (kb, mb,gb, …)

Size of a directory:

$ du -sh /tmp

Size of a file:

$ du -h /tmp/xyz

Listing Files and Their Data Format (KB, MB, GB) using ‘ls -lah’. you can also use ‘ls -all’

Linux ‘ls’ Flags Quick Reference Table

FLAG DESCRIPTION EXAMPLE
-l Displays file types, owner, group, size, date and filename. ls -l
-a Displays all files, including hidden files (suffixed with “.”). ls -a
-h Displays files with human readable format, e.g. 1k, 100MB, 1G. ls -h

The most common kill signals are:

Signal Name Single Value Effect

SIGHUP 1 Hangup

SIGINT 2 Interrupt from keyboard

SIGKILL 9 Kill signal

SIGTERM 15 Termination signal

SIGSTOP 17, 19, 23 Stop the process

What’s nice about this is that you can use the Signal Value in place of the Signal Name. So you don’t have to memorize all of the names of the various signals.
So, let’s now use the kill command to kill our instance of chrome. The structure for this command would be:
to see running processes or threads
>ps

kill SIGNAL PID
Where SIGNAL is the signal to be sent and PID is the Process ID to be killed. We already know, from our ps command that the IDs we want to kill are 3827, 3919, 10764, and 11679. So to send the kill signal, we’d issue the commands:

kill -9 3827
_________
shutdown -h 10
this will machine after 10 min automatically,

similary for reboot as well
_________
install wine in linux
Wine 2.0.1 Stable Released, Here’s How To Install it on Ubuntu
________ to know a size of a directory du -sh directoryPath (disk usage -show All directoryPath) ________ history history -d 'historyNumber' !x (x = historyNumber) history -c _______ ps aux | grep processNamea = all processesors u user visibility x = all executing programs even its not a part of terminal task. https://www.cyberciti.biz/faq/mounting-windows-partition-onto-ubuntu-linux/ how to copy a line/s from one text document to another text document(suppose doc2 is 500 line doc, i want to paste 5 lines from doc1 at 120th line in doc2) from terminal ___

TASK 1: to view Windows Disk Partitions via linux terminal

#in terminal -> gedit /etc/fstab (you can use any text editor)

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda7 during installation
UUID=4be43727-260c-4408-b75a-a7f6c7623b4b / ext4 errors=remount-ro 0 1
/swapfile none swap sw 0 0
UUID=464C596D4C59592F /media/Ddrive/ ntfs defaults 0 0
UUID=4212659E12659825 /media/Edrive/ ntfs defaults 0 0
UUID=E05A8AE55A8AB834 /media/Cdrive/ ntfs defaults 0 0

# add last three lines only
# mount -av (in terminal : a=all, v=verbose)
Design a site like this with WordPress.com
Get started