Jump to content

User:Altro971

From Wikipedia, the free encyclopedia

Command-line interpreter

[edit]

In computing, a command is a directive to a computer program to perform a specific task.

It may be issued via a command-line interface, such as a shell, or as input to a network service as part of a network protocol, or as an event in a graphical user interface triggered by the user selecting an option in a menu.

Basic Linux Commands

[edit]

● ls - Lists directory contents.

● cd - Changes the current directory.

● pwd - Displays the present working directory.

● cat - Concatenates and displays files.

● echo - Displays arguments to the screen.

● man - Displays the online manual.

● exit - Exits the shell or your current session.

● clear - Clears the screen.

Directory tree

[edit]

a directory contained inside another directory is called a sub directory. The terms parent and child are often used to describe the relationship between a sub directory and the directory in which it is cataloged, the latter being the parent. The top-most directory in such a file system, which does not have a parent of its own, is called the root directory.

Example : 1 directories, 6 files

[edit]
path/to/folder/

├── a-first.html
├── b-second.html
├── subfolder
│  ├── readme.html
│  ├── code.cpp
│  └── code.h
└── z-last-file.html

ls(list_Unix)

[edit]

ls is a command to list computer files in Unix and Unix-like operating systems

Usage

[edit]

The most common options to reveal this information or change the list of files are:

·        -l        long format,
·        -f        do not sort. Useful for directories containing large numbers of files.
·        -F        appends a character revealing the nature of a file, 
                   for example, * for an executable, or / for a directory. Regular files have no suffix.
·        -a        lists all files in the given directory, including "." (which are hidden files in Unix).
·        -R        recursively lists sub directories. The command ls -R / would therefore list all files.
·        -d        shows information about a symbolic link or directory.
·        -t        sort the list of files by modification time.
·        -h        print sizes in human readable format. (e.g., 1K, 234M, 2G, etc
·        -1        (the numeric digit one) force output to be one entry per line.

File Permission:Decoding ls -l Output

[edit]
$ ls -l
-rw-rw-r-- 1 jason users 10400 Sep 27 08:52 sales.data

Permissions -rw-rw-r--

Number of links 1

Owner name jason

Group name users

Number of bytes in the file 10400

Last modification time Sep 27 08:52

File name sales.data

Symbolic

notation

Numeric

notation

English
---------- 0000 no permissions
-rwx------ 0700 read, write, & execute only for owner
-rwxrwx--- 0770 read, write, & execute for owner and group
-rwxrwxrwx 0777 read, write, & execute for owner, group and others
---x--x--x 0111 execute
--w--w--w- 0222 write
--wx-wx-wx 0333 write & execute
-r--r--r-- 0444 read
-r-xr-xr-x 0555 read & execute
-rw-rw-rw- 0666 read & write
-rwxr----- 0740 owner can read, write, & execute; group can only read; others have no permissions

Example:

[edit]
d(rwx)r--rw-
·d  : drafts is a directory (denoted by the file descriptor d).
·rwx: the owner (user) has the right to read (r), write (w) and execute (x).
·rw-: group members (users part of the editors group) have read and write (w) permissions;execute are not permitted.
·r--: others (users aside from the owner or members of editors) have read-only permissions; write and execute are not permitted, 
      as denoted by the hyphen characters (-).

cd (command)

[edit]

The cd command, also known as chdir (change directory)

Usage

[edit]
user@wikipedia:~$ ls                                                                                                                                                                                 workreports games encyclopedia text.txt                                                                                                               user@wikipedia:~$ cd games                                                                                                                                       user@wikipedia:~/games$                                                                                                                                 

A similar session in DOS (though the concept of a "home directory" may not apply, depending on the specific version) would look like this:

C:\> dir                                                                                                                                                 workreports        <DIR>       Wed Oct 9th   9:01                                                                                         games              <DIR>       Tue Oct 8th  14:32                                                                                           encyclopedia       <DIR>       Mon Oct 1st  10:05                                                                                          text                txt   1903 Thu Oct10th  12:43                                                                                                                C:\> cd games                                                                                                                                                                         C:\games>   

Pwd

[edit]

Unix examples[edit]

Command Explanation
pwd Display the current working directory. Example: /home/foobar
pwd -P Display the current working directory physical path - without symbolic link name, if any. Example: If standing in a dir /home/symlinked, that is a symlink to /home/realdir, this would show /home/realdir
pwd -L Display the current working directory logical path - with symbolic link name, if any. Example: If standing in a dir /home/symlinked, that is a symlink to /home/realdir, this would show /home/symlinked

Help commands

[edit]

Man is used to display documentation.

$PATH controls your search path.

● Learn the full path to commands with which.

● Ask commands for help with --help or -h.

● Search man pages by using man -k.

Apropos (Unix) or man -k

[edit]

apropos - search the manual page names and descriptions

Sample usage

[edit]

The following example demonstrates the output of the apropos command:

$ apropos mount
free (1)             - Display amount of free and used memory in the system
mklost+found (8)     - create a lost+found directory on a mounted Linux second extended file system
mount (8)            - mount a file system
mountpoint (1)       - see if a directory is a mountpoint
ntfsmount (8)        - Read/Write userspace NTFS driver.
sleep (1)            - delay for a specified amount of time
switch_root (8)      - switch to another filesystem as the root of the mount tree.
umount (8)           - unmount file systems

regexp keyword (abc.n) and a regular keyword:

$ apropos abc.n xzless
XTestGrabControl (3) - XTest extension functions
xzless (1)           - view xz or lzma compressed (text) files

man page

[edit]

A man page (short for manual page) is a form of software documentation usually found on a Unix or Unix-like operating system. Topics covered include computer programs (including library and system calls), formal standards and conventions, and even abstract concepts. A user may invoke a man page by issuing the man command.

Command usage

[edit]

To read a manual page for a Unix command, a user can type:

man <command_name>

Creating and Removing Directories

[edit]

mkdir [-p] directory - Create a directory.

rmdir [-p] directory - Remove a directory.

rm -rf directory - Recursively removes directory.

Displaying the Contents of Files

[edit]

cat file Display the contents of file.

more file Browse through a text file.

less file More features than more.

head file Output the beginning (or top) portion of file.

tail file Output the ending (or bottom) portion of file.

The Vi Editor

[edit]

vi [file] Edit file.

vim [file] Same as vi, but more features.

view [file] Starts vim in read-only mode.

Vi Line Mode

[edit]

:w Writes (saves) the file.

:w! Forces the file to be saved.

:q Quit.

:q! Quit without saving changes.

:wq! Write and quit.

:x Same as :wq.

Emacs

[edit]

● Emacs is Powerful editor.

● Some people prefer vi, other emacs.

● Either are great editors.

● Use what feels comfortable to you.

emacs [file] Edit file.

C-<char> Ctrl while pressing <char>.

M-<char> "Meta" key (alt key) while

pressing <char>.

M-<char> Esc, then type <char>

Graphical Editors

[edit]

● emacs Emacs has a graphical mode too.

● gedit The default text editor for Gnome.

● gvim The graphical version of vim.

● kedit The default text editor for the KDE.

● AbiWord Microsoft Word alternative.

● LibreOffice Full office suite.

● Kate Source code editor.

Summary

[edit]

● Various graphical editors exist for Linux.

● Graphical modes exist for emacs and vi.

its function to concatenate files

Examples

[edit]
Command Explanation
cat file1.txt Display contents of file
cat file1.txt file2.txt Concatenate two text files and display the result in the terminal
cat file1.txt file2.txt > newcombinedfile.txt Concatenate two text files and write them to a new file
cat >newfile.txt Create a file called newfile.txt. Type the desired input and press CTRL+D to finish. The text will be in file newfile.txt.
cat -n file1.txt file2.txt > newnumberedfile.txt Some implementations of cat, with option -n, can also number lines
cat file1.txt > file2.txt Copy the contents of file1.txt into file2.txt
cat file1.txt >> file2.txt Append the contents of file1.txt to file2.txt
sort > test4 Concatenate the files, sort the complete set of lines, and write the output to a newly created file
less Run the program "less" with the concatenation of file1 and file2 as its input
cat Cancel "command" special behavior (e.g. paging) when it writes directly to TTY (cf. UUOC below)

Vi or Vim

[edit]

vim - Vi IMproved, a programmers text editor

It can be used to edit all kinds of plain text.It is especially useful for editing programs.

SYNOPSIS
       vim [options] [file ..]
       vim [options] -
       vim [options] -t tag
       vim [options] -q [errorfile]

       ex gex
       view
       gvim gview vimx evim eview
       rvim rview rgvim rgview

example:

[edit]
$ vi text.txt
Press i or insert and type your text and click esc and enter :wq! for quit

caution

[edit]

Another program may be editing the same file.  If this is the case,be careful not to end up with two different instances of the same file when making changes.

If edit session for this file crashed use ":recover" or "vim -r text.txt"to recover the changes (see ":help recovery").If you did this already, delete the swap file ".text.txt.swp".

Deleting, Copying, Moving, and Renaming Files

[edit]

● rm

● cp

● mv

● gzip

gzcat Concatenates compressed files.

zcat Concatenates compressed files.

● tar   

copying files and directories

Usage

[edit]

Copying a file to another file:

cp [-fHip][--] sourcefile targetfile

Copying file(s) to a directory

cp [-fHip] [--] sourcefile... targetdirectory

Copying a directory to a directory (-r or -R must be used)

cp -r|-R [-fHip] [--]  sourcedirectory... targetdirectory

Examples

[edit]

Creating a copy of a file in the current directory:

cp prog.c prog.bak

Copy two files in the current directory into another directory:

cp jones smith /home/nick/clients

Copy a file to a new file and preserve the modification date, time, and access control list associated with the source file:

cp -p smith smith.jr

Copy a directory, including all its files and subdirectories, to another directory:

cp -R /home/nick/clients /home/nick/customers

Mv(Unix)

[edit]

mv (short for move) is a Unix command that moves one or more files or directories from one place to another.

SYNOPSIS
       mv [OPTION]... [-T] SOURCE DEST
       mv [OPTION]... SOURCE... DIRECTORY
       mv [OPTION]... -t DIRECTORY SOURCE...

 Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

Examples

[edit]
Command Explanation
mv myfile mynewfilename renames 'myfile' to 'mynewfilename'.
mv myfile subdir/myfile moves 'myfile' to 'subdir/myfile' relative to the current directory
mv myfile subdir same as the previous command, filename is implied to be the same
mv be.03 /mnt/bkup/bes copies 'be.03' to the 'bes' directory of the mounted volume 'bkup', then 'be.03' is removed
mv afile another mydir moves multiple files to directory 'mydir'
mv /var/log/*z ~/logs This takes longer than expected if '/var' is on a different file system, as it frequently is, since files will be copied and deleted. The shell expands ~ to the user's home directory and treats * as a wildcard character.

gzip(unix)

[edit]

gzip, gunzip, zcat - compress or expand named files.

Compressed files can be restored to their original form using gzip -d or gunzip or zcat.

command to zip:

[edit]
gzip <file_name>

command to unzip(decompress):

[edit]
gzip -d <file_name>

tar(unix) --Archive

[edit]

tar’ saves many files together into a single tape or disk archive, and can restore individual files from the archive.

EXAMPLES

[edit]
       tar -cf archive.tar foo bar
              # Create archive.tar from files foo and bar.

       tar -tvf archive.tar
              # List all files in archive.tar verbosely.

       tar -xf archive.tar
              # Extract all files from archive.tar.

Main operation mode:

       -A, --catenate, --concatenate
              append tar files to an archive

       -c, --create
              create a new archive
	
       -d, --diff, --compare
              find differences between archive and file system

       --delete
              delete from the archive (not on mag tapes!)

       -r, --append
              append files to the end of an archive

       -t, --list
              list the contents of an archive

       --test-label
              test the archive volume label and exit

       -u, --update
              only append files newer than copy in archive

       -x, --extract, --get
              extract files from an archive

Wildcards

[edit]

● A character or string used for pattern matching.

● Globbing expands the wildcard pattern into a list of files and/or directories. (paths)

● Wildcards can be used with most commands.(ls,rm,cp)

Wildcards characters

[edit]

● * - matches zero or more characters.

[edit]

○ *.txt

○ a*

○ a*.txt

● ? - matches exactly one character.

[edit]

○ ?.txt

○ a?

○ a?.txt

More Wildcards - Character Classes

● [] - A character class.
[edit]

○ Matches any of the characters included between the brackets. Matches exactly one character.

○ [aeiou]

○ ca[nt]*

■ can

■ cat

■ candy

■ catch

More Wildcards - Character Classes

● [!] - Matches any of the characters NOT included between the brackets. Matches exactly one character.
[edit]

○ [!aeiou]*

■ baseball

■ cricket

More Wildcards - Ranges

● Use two characters separated by a hyphen to

create a range in a character class.

● [a-g]*

○ Matches all files that start with a, b, c, d, e, f, or g.

● [3-6]*

○ Matches all files that start with 3, 4, 5 or 6.

Named Character Classes
[edit]

● [[:alpha:]]

● [[:alnum:]]

● [[:digit:]]

● [[:lower:]]

● [[:space:]]

● [[:upper:]]

Matching Wildcard patterns

● \ - escape character. Use if you want to match

a wildcard character.

○ Match all files that end with a question mark:

■ *\?

● done?

Input/Output Types

[edit]

standard streams are interconnected input and output communication channels between a computer program and its environment when it begins execution.

Standard input is a stream from which a program reads its input data. The program requests data transfers by use of the read operation. Not all programs require stream input.

Standard output is a stream to which a program writes its output data. The program requests data transfer with the write operation. Not all programs generate output. For example, the file rename command (variously called mv, move, or ren) is silent on success.

Standard error is another output stream typically used by programs to output error messages or diagnostics. It is a stream independent of standard output and can be redirected separately.

Redirection

[edit]

> Redirects standard output to a file.

Overwrites (truncating) existing contents.

>> Redirects standard output to a file.

Appends to any existing contents.

< Redirects input from a file to a command.

& Used with redirection to signal that a file descriptor is being used.

2>&1 Combine stderr and standard output.

2>file Redirect standard error to a file.

The Null Device

[edit]

>/dev/null Redirect output to nowhere.

$ ls here not-here 2> /dev/null

here

$ ls here not-here > /dev/null 2>&1

Comparing the Contents of Files

[edit]

diff file1 file2 Compare two files.

sdiff file1 file2 Side-by-side comparison.

vimdiff file1 file2 Highlight differences in vim.

Searching in Files and Using Pipes

[edit]

● grep

● file

● cut

● tr

● column

● more

● less

● Pipes

Grep(unix)

[edit]

grep is a command-line utility for searching plain-text data sets for lines that match a regular expression.

command g/re/p (globally search for a regular expression and print matching lines)

It searches files for specified words or patterns

The grep command is case sensitive

To ignore upper/lower case distinctions, use the -i option, i.e. type

Sample usage

[edit]

The following example demonstrates the output of the grep command given different arguments

$ grep -i science science.txt

recursive grep

[edit]
Contains
$ grep -r -l "<to match>" --include="<wildcard for files>"

Doesn't Contain
$ grep -r -L "<to match>" --include="<wildcard for files>"

Find Occurrences and output to CSV
$ grep -r "<to match>" --include="<wildcard>" | cut -d : -f 1,3 | sed 's/: /,/g'

Find Occurrences and output to CSV with prepended string
$ grep -r "<to match>" --include="<wildcard>" | cut -d : -f 1,3 | sed 's/: /,/g' | awk '{print "<prepend string>"$0}'

agrep (approximate grep)

[edit]

agrep (approximate grep) matches even when the text only approximately fits the search pattern.

This following invocation finds net masks in file my file, but also any other word that can be derived from it, given no more than two substitutions.

agrep -2 netmasks myfile

This example generates a list of matches with the closest, that is those with the fewest, substitutions listed first. The command flag B means best:

agrep -B netmasks myfile

file (command)-- determine file type

[edit]

file tests each argument in an attempt to classify it. There are three sets of tests, performed in this order: file system tests, magic tests, language tests.

Classifies the named files according to the type of data they contain.

Examples

[edit]
$ To report on all files in your home directory 
$file *
LinkSource.txt: ASCII text, with CRLF line terminators
Loc1:           directory
copytext.txt:   ASCII text, with no line terminators
$ file file.c
file.c: C program text
$ file program
program: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked
    (uses shared libs), stripped
$ file /dev/hda1
/dev/hda1: block special (0/0)
$ file -s /dev/hda1
/dev/hda1: Linux/i386 ext2 filesystem

more is a command to view (but not modify) the contents of a text file one screen at a time.

Usage(Unix)

[edit]
more [options] [file_name]

If no file name is provided, more looks for input from standard input.

less used to view (but not change) the contents of a text file one screen at a time. It is similar to more, but has the extended capability of allowing both forward and backward navigation through the file.

Users should realize that less provides more emulation plus extensive enhancements.

Usage

[edit]
less [options] [file_name]

Frequently used options[edit]

[edit]
  • -g: Highlight just the current match of any searched string.
  • -i: Search case-insensitively.
  • -m: Show more detailed prompt, including file position.
  • -N: Show line numbers (useful for viewing source code).
  • -x3: Set tabstops (the number of columns per hard tab character) to the specified number (3, in this example) (useful for viewing source code).
  • -S: Disable line wrapping ("chop long lines"). Long lines can be seen by side-scrolling.
  • -X: Leave file contents on screen when less exits.
  • -?: Show help.
  • --follow-name: Follow mode, for log files that get replaced while being viewed

Examples

[edit]
less -M readme.txt           # Read "readme.txt"
less +F /var/log/mail.log    # Follow mode for log
file * | less                # Easier file analysis
less -I -g void *.c          # Case insensitive search for "void" in all .c files

Copying Files over the Network

[edit]

● SCP - Secure copy

● SFTP - SSH file transfer protocol

● PuTTY Secure Copy client - pscp.exe

● PuTTY Secure File Transfer client - psftp.exe

Graphical SCP/SFTP Clients

● Cyberduck

● FileZilla

● WinSCP

scp / sftp command line utilities

[edit]

scp source destination

Copy source to destination.

sftp host

Start a secure file transfer session with host.

sftp jason@host

ftp Command Line Utility

ftp host

Start a file transfer session with host.

Summary

[edit]

● Transfer files using a client.

● Command line clients: scp, sftp, ftp.

● Graphical clients: Cyberduck, FileZilla.

● Use scp or sftp over ftp when possible.

Process Management

[edit]
sudo
Top
Ps
sar
iostat
free
kill
who
df
du
id
sort
link

sudo(Unix)

[edit]

security privileges of another user, by default the superuser

It originally stood for "superuser do"

Usage example

[edit]
sudo su - wladmin ---> super user as web logic admin

Running ssh command:

sudo su - wladmin -c '/apps/xxx/xxx/xxxxxxx.cmd'

top (table of processes) is a task manager program, found in many Unix-like operating systems, that displays information about CPU and memory utilization.

SYNOPSIS

       top -hv|-bcHiOSs -d secs -n max -u|U user -p pid -o fld -w [cols]
The traditional switches `-' and white space are optional.

When operating top, the two most important keys are the help (h or ?)  key and quit ('q') key.  Alternatively, you could simply use the traditional interrupt key (^C) when you're done.

Startup Defaults

[edit]

       items shown with an asterisk (`*') could be overridden through the command-line.  All are explained in detail in the sections that follow.

Global-defaults

[edit]
           Global-defaults
              A - Alt display      Off (full-screen)
            * d - Delay time       1.5 seconds
            * H - Threads mode     Off (summarize as tasks)
              I - Irix mode        On  (no, `solaris' smp)
            * p - PID monitoring   Off (show all processes)
            * s - Secure mode      Off (unsecured)
              B - Bold enable      On  (yes, bold globally)

Summary-Area-defaults

[edit]
              l - Load Avg/Uptime  On  (thus program name)
              t - Task/Cpu states  On  (1+1 lines, see `1')
              m - Mem/Swap usage   On  (2 lines worth)
              1 - Single Cpu       Off (thus multiple cpus)

Task-Area-defaults

[edit]
  b - Bold hilite      Off (use `reverse')
            * c - Command line     Off (name, not cmdline)
            * i - Idle tasks       On  (show all tasks)
              J - Num align right  On  (not left justify)
              j - Str align right  Off (not right justify)
              R - Reverse sort     On  (pids high-to-low)
            * S - Cumulative time  Off (no, dead children)
            * u - User filter      Off (show euid only)
            * U - User filter      Off (show any uid)
              V - Forest view      On  (show as branches)
              x - Column hilite    Off (no, sort field)
              y - Row hilite       On  (yes, running tasks)
              z - color/mono       On  (show colors)
MAN TOP ---use manual command for info

Ps - (short for "process status")

[edit]

ps displays information about a selection of the active processes.  If you want a repetitive update of the selection and the displayed information, use top(1) instead.

EXAMPLES

[edit]

       To see every process on the system using standard syntax:

ps -e      
ps -ef        
ps -eF        
ps -ely

To print a process tree:

ps -ejH                                                                                                                                        ps axjf

Users can also utilize the ps command in conjunction with the grep command to find information about a single process, such as its id:

$ # Trying to find the PID of `firefox-bin` which is 2701
$ ps -A | grep firefox-bin
2701 ?        22:16:04 firefox-bin

The use of pgrep simplifies the syntax and avoids potential race conditions:

$ pgrep -l firefox-bin
2701 firefox-bin

To see every process running as root in user format:

# ps -U root -u
USER   PID  %CPU %MEM    VSZ   RSS TT  STAT STARTED        TIME COMMAND
root     1   0.0  0.0   9436   128  -  ILs  Sun00AM     0:00.12 /sbin/init --

sar (Unix)

[edit]

System Activity Report (sar):Collect, report, or save system activity information.

sar command is used to get the usage history of system resource such as CPU, RAM, SWAP, IO etc. /var/log/sa/ directory contains the information of system usage for the specified number of days as per OS configuration.

Syntax

[edit]
sar [-flags] [ -e time ] [ -f filename ] [-i sec ] [ -s time ]
-f
filename Uses filename as the data source for sar. The default is the current daily data file /var/adm/sa/sadd.
-e
time Selects data up to time. The default is 18:00.
-i
sec Selects data at intervals as close as possible to sec seconds.

Example

[edit]
[user@localhost]$ sar  # Displays current CPU activity. 

pgrep, pkill - look up or signal processes based on name and other attributes

pgrep  looks  through  the currently running processes and lists the process IDs which match the selection criteria to stdout. All the criteria have to match. pkill will send the specified signal (by default SIGTERM) to each process instead of listing them on stdout.

example;

[edit]
$ pgrep -u root sshd

       will only list the processes called sshd AND owned by root.  On the other hand,

$ pgrep -u root,daemon

       will list the processes owned by root OR daemon.

iostat (input/output statistics) is a computer system monitor tool used to collect and show operating system storage input and output statistics.

The iostat command is used for monitoring system input/output device loading by observing the time the devices are active in  relation to their average transfer rates. The iostat command generates reports that can be used to change system configuration to better balance the input/output load between physical disks.

Syntax and availability

[edit]

iostat –x -displays output where each line (row) gives numerical data for one device.

iostat -x 30 - It is best to run iostat specifying a time interval in seconds

iostat –En -To check san storage external disk

iostat  [ -c ] [ -d ] [ -h ] [ -k | -m ] [ -N ] [ -t ] [ -V ] [ -x ] [ -y ] [ -z ]
        [ -j { ID | LABEL | PATH | UUID | ... } ] 
        [ [ -T] -g group_name ] [ -p [ device [,...] | ALL ] ] 
        [ device [...] | ALL ] [ interval [ count ] ]
 The iostat command generates two types of reports, the CPU Utilization report and the Device Utilization report. 

Free (unix)

[edit]

free - Display amount of free and used memory in the system

free  displays  the total amount of free and used physical and swap memory in the system, as well as the buffers and caches used by the kernel. The information is gathered by parsing /proc/meminfo.

example

[edit]
wladmin@user# free -g
              total        used        free      shared  buff/cache   available
Mem:             15          11           1           0           2           3
Swap:             7           0           7
Usage:
 free [options]

Options:
 -h, --human         show human-readable output
     --si            use powers of 1000 not 1024
 -l, --lohi          show detailed low and high memory statistics
 -t, --total         show total for RAM + swap
 -s N, --seconds N   repeat printing every N seconds
 -c N, --count N     repeat printing N times, then exit
 -w, --wide          wide output
 -V, --version  output version information and exit

kill - terminate a process The  command kill sends the specified signal to the specified process or process group.If no signal is specified, the TERM signal is sent.The TERM signal will kill processes which do not catch this signal.  For other processes, it may be necessary to use  the KILL (9) signal, since this signal cannot be caught.

kill -9 <process id>

Swap(Unix)

[edit]
# swap -s ----To view the summary of the current swap space allocation total:                                                               106256k bytes allocated + 8512k reserved = 114768k used, 587512k available
# swap –l --- To list details of the system’s virtual swap space: swapfile dev swaplo blocks free
/dev/dsk/c0t0d0s1 136,1 16 1206736 1084736
/export/data/swapfile -16 40944 40944

who - show who is logged on

who [OPTION]... [ FILE | ARG1 ARG2 ]
[edit]
-a, --all
              same as -b -d --login -p -r -t -T -u

-b, --boot
              time of last system boot

-d, --dead
              print dead processes

-H, --heading
              print line of column headings

df (abbreviation for disk free) is a standard Unix command used to display the amount of available disk space for file systems

Example

[edit]

The following is an example of the output of the df command.

$ df
Filesystem    1024-blocks      Free %Used    Iused %Iused Mounted on
/dev/hd4            32768     16016   52%     2271    14% /
/dev/hd2          4587520   1889420   59%    37791     4% /usr
/dev/hd9var         65536     12032   82%      518     4% /var
/dev/hd3           819200    637832   23%     1829     1% /tmp
/dev/hd1           524288    395848   25%      421     1% /home
/proc                   -         -    -         -     -  /proc
/dev/hd10opt        65536     26004   61%      654     4% /opt

du (Unix):  summarize disk usage

[edit]

du (abbreviated from disk usage) is a standard Unix program used to estimate file space usage—space used under a particular directory or files on a file system.

Usage

[edit]

du takes a single argument, specifying a path name for du to work; if it is not specified, the current directory is used

Examples

[edit]
du [OPTION]... [FILE]...
du [OPTION]... --files0-from=F
[OPTION]
-S, --separate-dirs      
    ->for directories do not include size of sub directories

-s, --summarize      
    ->display only a total for each argument 
disk usage of all sub directories and files including hidden files within the current directory (sorted by reverse file size) :                $ du -sk .[!.]* *| sort -nr

id

[edit]

Return user identity

To check for user present are not

id -a <user name> :  gives the details of the uid, gid, groups
id <user name>  : gives the details of the uid, gid

sort (Unix)

[edit]

prints the lines of its input or concatenation of all files listed in its argument list in sorted order

Syntax

[edit]
sort [OPTION]... [FILE]...

With no FILE, or when FILE is -, the command reads from standard input.

Parameters[edit]

[edit]
-b Ignores leading blanks.
-d Considers only blanks and alphanumeric characters.
-f Fold lower case to upper case characters.
-g Compares according to general numerical value.
-i Considers only printable characters.
-M Compares (unknown) < 'JAN' < ... < 'DEC'.
-h Compare human readable numbers (e.g., 2K 1G).
-n Compares according to string numerical value.
-R Shuffles, but groups identical keys. See also: shuf
-r Reverses the result of comparisons.

passwd is used to change a user's password

An example record may be:

[edit]
jsmith:x:1001:1000:Joe Smith,Room 1007,(234)555-8910,(234)555-0044,email:/home/jsmith:/bin/sh

The fields, in order from left to right, are:

1. jsmith: User name

2. x: Information used to validate a user's password;

3. 1001: user identifier number, used by the operating system for internal purposes. It need not be unique.

4. 1000: group identifier number, which identifies the primary group of the user

5. Room 1007...: Gecos field, commentary that describes the person or account.

6. /home/jsmith: Path to the user's home directory.

7. /bin/sh: Program that is started every time the user logs into the system.

[edit]

link - call the link function to create a link to a file

link FILE1 FILE2
link OPTION

ln - make links between files-Create hard link by default.

       ln [OPTION]... [-T] TARGET LINK_NAME   (1st form)--create a link to TARGET with the name LINK_NAME
       ln [OPTION]... TARGET                  (2nd form)--create a link to TARGET in the current directory.
       ln [OPTION]... TARGET... DIRECTORY     (3rd form)--create links to each TARGET in DIRECTORY
       ln [OPTION]... -t DIRECTORY TARGET...  (4th form)--create links to each TARGET in DIRECTORY
[edit]
 -s, --symbolic  make symbolic links instead of hard links

pushd and popd

[edit]

The pushd command saves the current working directory in memory so it can be returned to at any time, optionally changing to a new directory.

Syntax

[edit]
pushd [path | ..]

path This optional command-line argument specifies the directory to make the current directory. If path is omitted, the path at the top of the directory stack is used, which has the effect of toggling between two directories.

Popd

[edit]
popd

Bourne shell

[edit]

The Bourne shell (sh) is a shell command-line interpreter for computer operating systems.

/bin/sh—which will be the Bourne shell, or a symbolic link or hard link to a compatible shell—even when other shells are used by most users.

Sed      

[edit]

cap vs less & more Vs grep

[edit]

Bash Variables

[edit]
env Show enviro­nment variables
echo $NAME Output value of $NAME variable
export NAME=value Set $NAME to value
$PATH Executable search path
$HOME Home directory
$SHELL Current shell


File Operations

[edit]
touch file1

Create file1

cat file1 file2

Concat­enate files and output

less file1

View and paginate file1

file file1

Get type of file1

cp file1 file2

Copy file1 to file2

mv file1 file2

Move file1 to file2

rm file1

Delete file1

head file1

Show first 10 lines of file1

tail file1

Show last 10 lines of file1

tail -F file1

Output last lines of file1 as it changes

File Permission Numbers

[edit]
First digit is owner permis­sion, second is group and third is everyone.
Calculate permission digits by adding numbers below.
4 read (r)
2 write (w)
1 execute (x)

Pipes

[edit]
cmd1 | cmd2

stdout of cmd1 to cmd2

cmd1 |& cmd2

stderr of cmd1 to cmd2

Process Management

[edit]
ps Show snapshot of processes
top Show real time processes
kill pid Kill process with id pid
pkill name Kill process with name name
killall name Kill all processes with names beginning name

IO Redire­ction

[edit]
cmd < file
Input of cmd from file
cmd1 <(cmd2)
Output of cmd2 as file input to cmd1
cmd > file
Standard output (stdout) of cmd to file
cmd > /dev/null
Discard stdout of cmd
cmd >> file
Append stdout to file
cmd 2> file
Error output (stderr) of cmd to file
cmd 1>&2
stdout to same place as stderr
cmd 2>&1
stderr to same place as stdout
cmd &> file
Every output of cmd to file
cmd refers to a command.
Pipes
cmd1 | cmd2
stdout of cmd1 to cmd2
cmd1 |& cmd2
stderr of cmd1 to cmd2
Command Lists
cmd1 ; cmd2
Run cmd1 then cmd2
cmd1 && cmd2
Run cmd2 if cmd1 is successful
cmd1 || cmd2
Run cmd2 if cmd1 is not successful
cmd &
Run cmd in a subshell

Bash Commands

[edit]
uname -a Show system and kernel
head -n1 /etc/issue Show distri­bution
mount Show mounted filesy­stems
date Show system date
uptime Show uptime
whoami Show your username
man command Show manual for command

Bash Shortcuts

[edit]
CTRL-c Stop current command
CTRL-z Sleep program
CTRL-a Go to start of line
CTRL-e Go to end of line
CTRL-u Cut from start of line
CTRL-k Cut to end of line
CTRL-r Search history
!! Repeat last command
!abc Run last command starting with abc
!abc:p Print last command starting with abc
!$ Last argument of previous command
ALT-. Last argument of previous command
!* All arguments of previous command
^abc^123 Run previous command, replacing abc with 123

Bash Variables

[edit]
env Show enviro­nment variables
echo $NAME Output value of $NAME variable
export NAME=value Set $NAME to value
$PATH Executable search path
$HOME Home directory
$SHELL Current shell

IO Redire­ction

[edit]

Pipes

[edit]

Command Lists

[edit]
cmd1 ; cmd2

Run cmd1 then cmd2

cmd1 && cmd2

Run cmd2 if cmd1 is successful

cmd1 || cmd2

Run cmd2 if cmd1 is not successful

cmd &

Run cmd in a subshell

Directory Operations

[edit]
pwd Show current directory
mkdir dir Make directory dir
cd dir Change directory to dir
cd .. Go up a directory
ls List files

ls Options

[edit]
-a Show all (including hidden)
-R Recursive list
-r Reverse order
-t Sort by last modified
-S Sort by file size
-l Long listing format
-1 One file per line
-m Comma-­sep­arated output
-Q Quoted output

Search Files

[edit]
grep pattern files Search for pattern in files
grep -i Case insens­itive search
grep -r Recursive search
grep -v Inverted search
grep -o Show matched part of file only
find /dir/ -name name* Find files starting with name in dir
find /dir/ -user name Find files owned by name in dir
find /dir/ -mmin num Find files modifed less than num minutes ago in dir
whereis command Find binary / source / manual for command
locate file Find file (quick search of system index)

File Operations

[edit]
touch file1

Create file1

cat file1 file2

Concat­enate files and output

less file1

View and paginate file1

file file1

Get type of file1

cp file1 file2

Copy file1 to file2

mv file1 file2

Move file1 to file2

rm file1

Delete file1

head file1

Show first 10 lines of file1

tail file1

Show last 10 lines of file1

tail -F file1

Output last lines of file1 as it changes

Watch a Command

[edit]
watch -n 5 'ntpq -p'

Issue the 'ntpq -p' command every 5 seconds and display output

Process Management

[edit]
ps Show snapshot of processes
top Show real time processes
kill pid Kill process with id pid
pkill name Kill process with name name
killall name Kill all processes with names beginning name

Nano Shortcuts

[edit]
Files
Ctrl-R Read file
Ctrl-O Save file
Ctrl-X Close file
Cut and Paste
ALT-A Start marking text
CTRL-K Cut marked text or line
CTRL-U Paste text
Navigate File
ALT-/ End of file
CTRL-A Beginning of line
CTRL-E End of line
CTRL-C Show line number
CTRL-_ Go to line number
Search File
CTRL-W Find
ALT-W Find next
CTRL-\ Search and replace


Screen Shortcuts

[edit]
screen -r

Resume a screen session.

screen -list

Show your current screen sessions.

CTRL-A

Activate commands for screen.

CTRL-A c

Create a new instance of terminal.

CTRL-A n

Go to the next instance of terminal.

CTRL-A p

Go to the previous instance of terminal.

CTRL-A "

Show current instances of terminals.

CTRL-A A

Rename the current instance.


File Permis­sions

[edit]
chmod 775 file

Change mode of file to 775

chmod -R 600 folder

Recurs­ively chmod folder to 600

chown user:group file

Change file owner to user and group to group

File Permission Numbers

[edit]
First digit is owner permis­sion, second is group and third is everyone.
Calculate permission digits by adding numbers below.
4 read (r)
2 write (w)
1 execute (x)

Command Lists

[edit]
cmd1 ; cmd2

Run cmd1 then cmd2

cmd1 && cmd2

Run cmd2 if cmd1 is successful

cmd1 || cmd2

Run cmd2 if cmd1 is not successful

cmd &

Run cmd in a subshell

1 – SYSTEM INFORMATION

[edit]
# Display Linux system information
uname -a

# Display kernel release information
uname -r

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

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

# Show system host name
hostname

# Display the IP addresses of the host
hostname -I

# Show system reboot history
last reboot

# Show the current date and time
date

# Show this month's calendar
cal

# Display who is online
w

# Who you are logged in as
whoami

2 – HARDWARE INFORMATION

[edit]
# Display messages in kernel ring buffer
dmesg

# Display CPU information
cat /proc/cpuinfo

# Display memory information
cat /proc/meminfo

# Display free and used memory ( -h for human readable, -m for MB, -g for GB.)
free -h

# Display PCI devices
lspci -tv

# Display USB devices
lsusb -tv

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

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

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

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

3 – PERFORMANCE MONITORING AND STATISTICS

[edit]
# Display and manage the top processes
top

# Interactive process viewer (top alternative)
htop

# Display processor related statistics
mpstat 1

# Display virtual memory statistics
vmstat 1

# Display I/O statistics
iostat 1

# Display the last 100 syslog messages  (Use /var/log/syslog for Debian based systems.)
tail 100 /var/log/messages

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

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

# List all open files on the system
lsof

# List files opened by user
lsof -u user

# Display free and used memory ( -h for human readable, -m for MB, -g for GB.)
free -h

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

4 – USER INFORMATION AND MANAGEMENT

[edit]
# Display the user and group ids of your current user.
id

# Display the last users who have logged onto the system.
last

# Show who is logged into the system.
who

# Show who is logged in and what they are doing.
w

# Create a group named "test".
groupadd test

# Create an account named john, with a comment of "John Smith" and create the user's home directory.
useradd -c "John Smith" -m john

# Delete the john account.
userdel john

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

5 – FILE AND DIRECTORY COMMANDS

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

# Display the present working directory
pwd

# Create a directory
mkdir directory

# Remove (delete) file
rm file

# Remove the directory and its contents recursively
rm -r directory

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

# Forcefully remove directory recursively
rm -rf directory

# Copy file1 to file2
cp file1 file2

# Copy source_directory recursively to destination. If destination exists, copy source_directory into destination, otherwise create destination with the contents of source_directory.
cp -r source_directory destination

# Rename or move file1 to file2. If file2 is an existing directory, move file1 into directory file2
mv file1 file2

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

# Create an empty file or update the access and modification times of file.
touch file

# View the contents of file
cat file

# Browse through a text file
less file

# Display the first 10 lines of file
head file

# Display the last 10 lines of file
tail file

# Display the last 10 lines of file and "follow" the file as it grows.
tail -f file

6 – PROCESS MANAGEMENT

[edit]
# Display your currently running processes
ps

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

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

# Display and manage the top processes
top

# Interactive process viewer (top alternative)
htop

# Kill process with process ID of pid
kill pid

# Kill all processes named processname
killall processname

# Start program in the background
program &

# Display stopped or background jobs
bg

# Brings the most recent background job to foreground
fg

# Brings job n to the foreground
fg n

7 – FILE PERMISSIONS

[edit]
        PERMISSION      EXAMPLE

         U   G   W
        rwx rwx rwx     chmod 777 filename
        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

# NOTE: Use 777 sparingly!

        LEGEND
        U = User
        G = Group
        W = World

        r = Read
        w = write
        x = execute
        - = no access

8 – NETWORKING

[edit]
# Display all network interfaces and ip address
ifconfig -a

# Display eth0 address and details
ifconfig eth0

# Query or control network driver and hardware settings
ethtool eth0

# Send ICMP echo request to host
ping host

# Display whois information for domain
whois domain

# Display DNS information for domain
dig domain

# Reverse lookup of IP_ADDRESS
dig -x IP_ADDRESS

# Display DNS ip address for domain
host domain

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

# Display all local ip addresses
hostname -I

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

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

9 – ARCHIVES (TAR FILES)

[edit]
# Create tar named archive.tar containing directory.
tar cf archive.tar directory

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

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

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

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

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

10 – INSTALLING PACKAGES

[edit]
# Search for a package by keyword.
yum search keyword

# Install package.
yum install package

# Display description and summary information about package.
yum info package

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

# Remove/uninstall package.
yum remove package

# Install software from source code.
tar zxvf sourcecode.tar.gz
cd sourcecode
./configure
make
make install
[edit]
# Search for pattern in file
grep pattern file

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

# Find files and directories by name
locate name

# Find files in /home/john that start with "prefix".
find /home/john -name 'prefix*'

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

12 – SSH LOGINS

[edit]
# Connect to host as your local username.
ssh host

# Connect to host as user
ssh user@host

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

13 – FILE TRANSFERS

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

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

# Copy all files and directories recursively from server to the current system's /tmp folder.
scp -r server:/var/www /tmp

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

# Synchronize files/directories between the local and remote system with compression enabled
rsync -avz /home server:/backups/

14 – DISK USAGE

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

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

# Display disks partitions sizes and types
fdisk -l

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

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

15 – DIRECTORY NAVIGATION

[edit]
# To go up one level of the directory tree.  (Change into the parent directory.)
cd ..

# Go to the $HOME directory
cd

# Change to the /etc directory
cd /etc


Q & A

[edit]

How to search the file for specified word or keywords?