Thursday, September 29, 2011

Bash command line for Linux

Linux Bash commands with definitions,

adduserAdd a user to the system
addgroupAdd a group to the system
aliasCreate an alias
aproposSearch Help manual pages (man -k)
apt-getSearch for and install software packages (Debian/Ubuntu)
aptitudeSearch for and install software packages (Debian/Ubuntu)
aspellSpell Checker
awkFind and Replace text, database sort/validate/index
basenameStrip directory and suffix from filenames
bashGNU Bourne-Again SHell 
bcArbitrary precision calculator language
bgSend to background
breakExit from a loop
builtinRun a shell builtin
bzip2Compress or decompress named file(s)
calDisplay a calendar
caseConditionally perform a command
catConcatenate and print (display) the content of files
cdChange Directory
cfdiskPartition table manipulator for Linux
chgrpChange group ownership
chmodChange access permissions
chownChange file owner and group
chrootRun a command with a different root directory
chkconfigSystem services (runlevel)
cksumPrint CRC checksum and byte counts
clearClear terminal screen
cmpCompare two files
commCompare two sorted files line by line
commandRun a command - ignoring shell functions
continueResume the next iteration of a loop
cpCopy one or more files to another location
cronDaemon to execute scheduled commands
crontabSchedule a command to run at a later time
csplitSplit a file into context-determined pieces
cutDivide a file into several parts
dateDisplay or change the date & time
dcDesk Calculator
ddConvert and copy a file, write disk headers, boot records
ddrescueData recovery tool
declareDeclare variables and give them attributes
dfDisplay free disk space
diffDisplay the differences between two files
diff3Show differences among three files
digDNS lookup
dirBriefly list directory contents
dircolorsColour setup for `ls'
dirnameConvert a full pathname to just a path
dirsDisplay list of remembered directories
dmesgPrint kernel & driver messages 
duEstimate file space usage
echoDisplay message on screen
egrepSearch file(s) for lines that match an extended expression
ejectEject removable media
enableEnable and disable builtin shell commands
envEnvironment variables
ethtoolEthernet card settings
evalEvaluate several commands/arguments
execExecute a command
exitExit the shell
expectAutomate arbitrary applications accessed over a terminal
expandConvert tabs to spaces
exportSet an environment variable
exprEvaluate expressions
FALSEDo nothing, unsuccessfully
fdformat Low-level format a floppy disk
fdiskPartition table manipulator for Linux
fgSend job to foreground
fgrepSearch file(s) for lines that match a fixed string
fileDetermine file type
findSearch for files that meet a desired criteria
fmtReformat paragraph text
foldWrap text to fit a specified width.
forExpand words, and execute commands
formatFormat disks or tapes
freeDisplay memory usage
fsckFile system consistency check and repair
ftpFile Transfer Protocol
function Define Function Macros
fuserIdentify/kill the process that is accessing a file
gawkFind and Replace text within file(s)
getoptsParse positional parameters
grepSearch file(s) for lines that match a given pattern
groupsPrint group names a user is in
gzipCompress or decompress named file(s)
hashRemember the full pathname of a name argument
headOutput the first part of file(s)
helpDisplay help for a built-in command
historyCommand History
hostnamePrint or set system name
iconvConvert the character set of a file
idPrint user and group id's
ifConditionally perform a command
ifconfig Configure a network interface
ifdownStop a network interface
ifupStart a network interface up
importCapture an X server screen and save the image to file
installCopy files and set attributes
jobsList active jobs
joinJoin lines on a common field
killStop a process from running
killallKill processes by name
lessDisplay output one screen at a time
letPerform arithmetic on shell variables
lnMake links between files
localCreate variables
locateFind files
lognamePrint current login name
logoutExit a login shell
lookDisplay lines beginning with a given string
lpcLine printer control program
lprOff line print
lprintPrint a file
lprintdAbort a print job
lprintqList the print queue
lprmRemove jobs from the print queue
lsList information about file(s)
lsofList open files
makeRecompile a group of programs
manHelp manual
mkdirCreate new folder(s)
mkfifoMake FIFOs (named pipes)
mkisofsCreate an hybrid ISO9660/JOLIET/HFS filesystem
mknodMake block or character special files
moreDisplay output one screen at a time
mountMount a file system
mtoolsManipulate MS-DOS files
mtrNetwork diagnostics (traceroute/ping)
mvMove or rename files or directories
mmvMass Move and rename (files)
netstatNetworking information
niceSet the priority of a command or job
nlNumber lines and write files
nohupRun a command immune to hangups
notify-sendSend desktop notifications
nslookup Query Internet name servers interactively
openOpen a file in its default application
opOperator access
passwdModify a user password
pasteMerge lines of files
pathchkCheck file name portability
pingTest a network connection
pkillStop processes from running
popdRestore the previous value of the current directory
prPrepare files for printing
printcap Printer capability database
printenv Print environment variables
printfFormat and print data
psProcess status
pushdSave and then change the current directory
pwdPrint Working Directory
quotaDisplay disk usage and limits
quotacheck Scan a file system for disk usage
quotactl Set disk quotas
ramram disk device
rcpCopy files between two machines
readRead a line from standard input
readarrayRead from stdin into an array variable
readonly Mark variables/functions as readonly
rebootReboot the system
renameRename files
reniceAlter priority of running processes
remsyncSynchronize remote files via email
returnExit a shell function
revReverse lines of a file
rmRemove files
rmdirRemove folder(s)
rsyncRemote file copy (Synchronize file trees)
screenMultiplex terminal, run remote shells via ssh
scpSecure copy (remote file copy)
sdiffMerge two files interactively
sedStream Editor
selectAccept keyboard input
seqPrint numeric sequences
setManipulate shell variables and functions
sftpSecure File Transfer Program
shiftShift positional parameters
shoptShell Options
shutdown Shutdown or restart linux
sleepDelay for a specified time
slocateFind files
sortSort text files
sourceRun commands from a file `.'
splitSplit a file into fixed-size pieces
sshSecure Shell client (remote login program)
straceTrace system calls and signals
suSubstitute user identity
sudoExecute a command as another user
sumPrint a checksum for a file
suspendSuspend execution of this shell
symlinkMake a new name for a file
syncSynchronize data on disk with memory
tailOutput the last part of files
tarTape ARchiver
teeRedirect output to multiple files
testEvaluate a conditional expression
timeMeasure Program running time
timesUser and system times
touchChange file timestamps
topList processes running on the system
traceroute Trace Route to Host
trapRun a command when a signal is set(bourne)
trTranslate, squeeze, and/or delete characters
TRUEDo nothing, successfully
tsortTopological sort
ttyPrint filename of terminal on stdin
typeDescribe a command
ulimitLimit user resources
umaskUsers file creation mask
umountUnmount a device
unaliasRemove an alias
unamePrint system information
unexpand Convert spaces to tabs
uniqUniquify files
unitsConvert units from one scale to another
unsetRemove variable or function names
unsharUnpack shell archive scripts
untilExecute commands (until error)
useraddCreate new user account
usermodModify user account
usersList users currently logged in
uuencode Encode a binary file
uudecode Decode a file created by uuencode
vVerbosely list directory contents (`ls -l -b')
vdirVerbosely list directory contents (`ls -l -b')
viText Editor
vmstatReport virtual memory statistics
watchExecute/display a program periodically
wcPrint byte, word, and line counts
whereisSearch the user's $path, man pages and source files for a program
whichSearch the user's $path for a program file
whileExecute commands
whoPrint all usernames currently logged in
whoamiPrint the current user id and name (`id -un')
WgetRetrieve web pages or files via HTTP, HTTPS or FTP
writeSend a message to another user
xargsExecute utility, passing constructed argument list(s)
xdg-open Open a file or URL in the user's preferred application.
yesPrint a string until interrupted
.Run a command script in the current shell
!!Run the last command again
###Comment / Remark



Wednesday, September 28, 2011

Linux commands for Compressing and Archiving File

1. gzip and gunzip
gzip command is used to compress the file, and gunzip is used to de-compress it.
Usage:
gzip <file name>
It provides the extension .gz and removes the original file
Example:
$ wc sample_copy.txt
      65 2776 17333 sample_copy.txt
$ gzip sample_copy.txt
$ wc sample_copy.txt.gz
      26 155 7095 sample_copy.txt.gz

The compression ratio depends on the type, size and nature of the file

Usage:
gunzip <file name with.gz>
Example:
$ gunzip sample_copy.txt.gz
$ /*do ls and you can see the original file*/

If you want to compress the directory contents recursively, use -r option with gzip command and unzip it use the same option with gunzip command.

2. tar : The archival program
tar command is used to create archive that contains a group or file or entire directory structure.
It is generally used for back ups.
Usage:
tar [options] <file1 or dir> . . .

Options:
  • -c Create an archive
  • -x Extract files from archive
  • -t Display files in archive
  • -f arch Name the archive arch
Examples:
$ tar -cvf compression.tar compression
compression/ //v for verbose
compression/temp/
compression/temp/sample2.txt
compression/sample1.txt
  • We can use tar and gzip command in succession to compress the tar file.
$ tar -cvf compression.tar compression
$ gzip compression.tar
$ //will create compression.tar.gz file
  • For un-compression the file first use gunzip command, which will create a tar file and then use tar command to untar the contents
$ gunzip compression.tar.gz
$ tar -xvf compression.tar
  • To just view the contents of the tar file use -t option 
$ tar -tvf compression.tar
  • Instead of doing tar first and then gzip next, we can combine both of them using the option -z
$ tar -cvzf compression.tar.gz compression
compression/
compression/temp/
compression/temp/sample2.txt
compression/sample1.txt
  •  We can de-compress .tar.gz agin in a single command using the option -z with -x
$ tar -xvzf compression.tar.gz
 3. zip and unzip: compressing and archiving
zip command can be used for archiving as well as compressing the contents of the directory or the file.
Usage:
zip [options] output.zip <files to be zipped or directory>
Example:
$ zip sample1.zip sample1.txt
//will create sample1.zip file
  • Use -r option to recursively zip the contents of the directory
$ zip -r compression.zip compression
// will create compression.zip file
  • To un-compress the file use unzip command 
$ unzip compression.zip
// will uncompress the compression.zip file

Linux File Handling Commands

1. cat : Concatenate Files

cat command is used to display the contents of a small file on terminal
Usage:
cat <file name>
Example:

$ cat sample3.txt
Unix (officially trademarked as UNIX, sometimes...


cat when supplied with more than one file will concatenate the files without any header information
Usage: 
$ cat sample3.txt sample4.txt


2. tac : concatenate files in reverse

tac command is used to display the contents of a small file in reverse order on terminal
Usage:
tac <file name>
Example:

$ tac sample3.txt
/*displays sample3.txt in reverse order*/


tac when supplied with more than one file will concatenate the reverse contents of files without any header information
Usage:

$ tac sample3.txt sample4.txt

3. more, less : paging output

more and less commands are used to view large files one page at a time
Usage:
more <file name>
less <file name>
Example:

$ more sample1.txt
/*sample1.txt will be displayed one page at a time */
$ less sample1.txt
/*sample1.txt will be displayed one page at a time */








less is the standard pager for linux and in general less is more powerful than more


4. wc : statistic of file

wc command is used to count lines, words and characters, depending on the option used.
Usage:
wc [options] [file name]
Example:

$ wc sample1.txt
65 2776 17333 sample1.txt
  • Which means sample1.txt file has 65 lines, 2776 words, and 17333 characters
  • you can just print number of lines, number of words or number of charcters by using following options:
    • -l : Number of lines
    • -w : Number of words
    • -c : Number of characters
5. cmp: comparing two files
cmp command is used to compare two files whether they are identical or not
Usage:
cmp <file1> <file2>
Example:
$ cmp sample1.txt sample2.txt
sample1.txt sample2.txt differ: byte 1, line 1
$ cmp sample1.txt sample1_copy.txt
$ /*No output prompt returns back*/
  • The two files are compared byte by byte and the location of the first mismatch is printed on the screen.
  • If two files are identical, then it doesnot print anything on the screen.
6. comm : what is common?
comm command displays what is common between both the files
Usage:
comm <file1> <file2>
Example:
$ comm sample5.txt sample6.txt
  • The input files to comm command should be sorted alphabetically.







Linux file system

1. Standard directory structure
  • / - the topmost
  • /dev - all the devices are accessible as files
  • /var - "variable" data such as mails, log files, databases
  • /usr - almost all the packages installed
  • /etc - con?guration files
  • /home - home directories for all the users
  • /root - home directory of the privileged user root
  • /mnt - used to mount other directories/partitions
2. File Attributes
  • To see the file attributes type ls -l on your terminal
ls -l
total 156-rw-r--r--  1 root root   1053 Sep 22  2010 anaconda-ks.cfg-rw-r--r--  1 root root 127004 Sep 22  2010 CHECKSUMSdrwxr-xr-x  2 root root   4096 Aug  3 00:02 Desktop-rw-r--r--  1 root root    100 Jun  6 22:14 svn-checkout-command
    •  It has 10 characters, first character is d if its directory and - if its file.
    • Next 9 characters are divided into three groups with a set of 3 characters each
    • First 3 characters - Owner of the file or directory
    • Next 3 characters - Group
    • Last 3 characters - Others
    • r - Read i.e. File or directory is readable
    • w - Write i.e. File or directory is writable
    • x - Execute i.e. File or directory is executable
    • -rw-r–r– means it has read, write but not execute permissions for the owner of the file, only read permissions for the group and only read permissions for others.
    • The third column of the command ls -l tells about the owner of the file, next column tells to which group it belongs
3. Changing the File attributes
chmod Changing the permissions of the file
Usage: 
$ chmod o+x Testing.java
$ chmod 655 Testing.java

4. Changing ownership
chown command is used for changing the ownership and also group of the file
Usage:
$ chown guest Testing.java
$ chown guest:guest Testing.java

5. File system commands
  • Deleting Files - rm
  • Copying and moving files - cp, mv
  • Creating directories - mkdir
  • Deleting Empty Directory - rmdir
$ rm Testing.java
//deletes the file Testing.java
$ cp Testing.java Copy.java
//creates the copy of Testing.java
$ mv Testing.java Test.java
//renames the file Testing.java to Test.java
$ mkdir newDir
//Creates directory newDir
$ rmdir newDir
//deletes directory newDir newDir should be empty.

General Purpose utilities commands in Linux


1. Calender
cal: Command to see calender for any specific month or a complete year
Usage: 
$ cal [ [month] year]
Example:
$ cal april 2009
Output:
April 2009
Su Mo Tu We Th Fr Sa
 1  2  3  4
5   6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30

2. Date
date: displays the current date
Usage:
$ date
Output:
Tue Apr 21 21:33:49 IST 2009
user$ date +"%D %H:%M:%S"
04/21/09 21:35:02
Options:
d - The da of the month (1-31)
y - The last two digits of the year
H,M,S - Hour Minute and second respectively
D - the date in mm/dd/yy

3. echo
echo: Print message on the terminal
Usage:
echo ""
Example:
$ echo "Welcome to the workshop"
Output:
Welcome to the workshop

4. printf
printf: Print the formatted message on the terminal
Usage:
printf ""
Example:
$ printf "the amount is %d\n" 100
Output:
the amount is 100

5. Calculator
bc: A text based calculator
Usage:
$ bc
2*10+20-9+4/2 [Input]
33 [Output]
[ctrl+d] [Quit]

xcalc is graphical based calculator

6. script
script command records your session and stores it in a file
Usage:
$ script
Script started, file is typescript
$ echo "this is a sample script"
this is a sample script
$ [ctrl+d]
Script done, file is typescript

By default if you dont specify any file name the contents will be stored in file name typescipt
Usage:
$ cat typescript
Script started on Tuesday 21 April 2009 10:07:00
$ echo "this is a sample script"
this is a sample script
$
Script done on Tuesday 21 April 2009 10:07:34 PM

7. passwd
passwd command allows you to change your password
Usage:
$ passwd
Changing password for user123.
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

8. WHO: Who are the users?
who command tells you the users currently logged on to the system
Usage:
$ who
root     :0           Sep  4 22:47
root     pts/1        Sep  4 22:48 (:0.0)

9. man - The reference Manual
man displays the documentation for a command
Usage:
man
Example:
$ man ls
ls - list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
DESCRIPTION
List information about the FILEs (the
none of -cftuvSUX nor --sort.