BASIC
LINUX COMMANDS
alias
Create
an alias, aliases allow a string to be substituted for a word.
Ex:
alias l="ls -l"
alias c=”clear”
Parameters:
Alias –p : print the list
of aliases
Unalias <alias name>
: remove alias
awk or gawk (gnu awk)
Find
and Replace text, database sort/validate/index
The basic function of awk is to search files
for lines (or other units of text) that
contain a pattern. When a line matches, awk performs a specific action on that line.
contain a pattern. When a line matches, awk performs a specific action on that line.
Ex:
This
program prints a sorted list of the login names of all users.
awk -F: '{ print $1 }' /etc/passwd | sort
This
program counts lines in a file.
awk 'END { print NR }' data
This program prints the even numbered lines in the data file. If
you were to use
the expression `NR % 2 == 1' instead, it would print the odd numbered lines.
the expression `NR % 2 == 1' instead, it would print the odd numbered lines.
awk 'NR % 2 == 0' data
Parameter:
-F: FS
--field-separator FS
Use FS for the input field separator (the value of the `FS' predefined variable)
-f PROGRAM-FILE
--file PROGRAM-FILE
Read the `awk' program source from the file PROGRAM-FILE, instead of from
the first command line argument.
Fast extraction of a time range from syslog logfile?
I often have to extract a timeslice from this file. I'd like to write a general-purpose script for this, that I can call like:$ timegrep 22:30-02:00 /logs/something.log
and have it pull out the lines from 22:30, onward across the midnight boundary, until 2am the next day.
- I don't want to have to bother typing the date(s) on the command line, just the times. The program should be smart enough to figure them out.
- The log date format doesn't include the year, so it should guess based on the current year, but nonetheless do the right thing around New Year's Day.
- I want it to be fast -- it should use the fact that the lines are in order to seek around in the file and use a binary search.
How to extract logs between two time stamps.
You can use awk for this:
$ awk -F'[]]|[[]' \
'$0 ~ /^\[/ && $2 >= "2014-04-07 23:00" { p=1 }
$0 ~ /^\[/ && $2 >= "2014-04-08 02:00" { p=0 }
p { print $0 }' log
Where:
-F specifies the characters [ and ] as field separators using a regular expression.
$0 references a complete line.
$2 references the date field.
p is used as boolean variable that guards the actual printing.
$0 ~ /regex/ is true if regex matches $0
>= is used for lexicographically comparing string (equivalent to e.g. strcmp())
Variations
The above command line implements right-open time interval matching. To get closed interval semantics just increment your right date, e.g.:
$ awk -F'[]]|[[]' \
'$0 ~ /^\[/ && $2 >= "2014-04-07 23:00" { p=1 }
$0 ~ /^\[/ && $2 >= "2014-04-08 02:00:01" { p=0 }
p { print $0 }' log
In case you want to match timestamps in another format you have to modify the $0 ~ /^\[/ sub-expression. Note that it used to ignore lines
without any timestamps from print on/off logic.
For example for a timestamp format like YYYY-MM-DD HH24:MI:SS (without [] braces) you could modify the command like this:
$ awk \
'$0 ~ /^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-2][0-9]:[0-5][0-9]:[0-5][0-9]/
{
if ($1" "$2 >= "2014-04-07 23:00") p=1;
if ($1" "$2 >= "2014-04-08 02:00:01") p=0;
}
p { print $0 }' log
(note that also the field separator is changed - to blank/non-blank transition, the default)
cat
Display
the contents of a file (concatenate)
Examples:
Display a file
$ cat myfile.txt
Concatenate two files:
$ cat file1.txt file2.txt >
union.txt
If you need to combine two files but also eliminate duplicates,
this can be
done with sort unique:
done with sort unique:
$ sort -u
file1.txt file2.txt > unique_union.txt
Parameter:
-E, -- show - ends display $ at end of each line
-n, -- number - number all output lines
cal
Display
a calendar
Syntax
cal [-mjy] [[month] year]
cd
Change
Directory - change the current working directory to a specific Folder.
Examples
Move to the sybase folder
$ cd /usr/local/sybase
$ pwd
/usr/local/Sybase
$ pwd
/usr/local/Sybase
Change to another folder
$ cd /var/log
$ pwd
/var/log
$ pwd
/var/log
Quickly get back
$ cd -
$ pwd
/usr/local/Sybase
$ pwd
/usr/local/Sybase
move up one folder
$ cd ..
$ pwd
/usr/local/
$ pwd
/usr/local/
$ cd (Back to your home folder)
Chgrp (Change group ownership)
'chgrp' changes the group ownership of each given File
to Group
(which can be
either a group name or a numeric group id) or to the group of an existing reference file.
either a group name or a numeric group id) or to the group of an existing reference file.
Ex:
$chgrp oracle /usr/database
chmod
Change
access permissions,
Numeric mode:
Read (r) = 4
Write (w) =2
Execute (x) =1
R
|
W
|
X
|
|
0
|
0
|
0
|
0
|
1
|
0
|
0
|
1
|
2
|
0
|
1
|
0
|
3
|
0
|
1
|
1
|
4
|
1
|
0
|
0
|
5
|
1
|
0
|
1
|
6
|
1
|
1
|
0
|
7
|
1
|
1
|
1
|
Ex: chmod 777 xyz
Symbolic Mode:
Owner = u
Group = g
Other = o
All users = a
Examples
Deny execute permission to everyone:
Deny execute permission to everyone:
chmod a-x file
Allow
read permission to everyone:
chmod a+r file
Make
a file readable and writable by the group and others:
chmod go+rw file
Make
a shell script executable by the user/owner
$ chmod u+x myscript.sh
Allow
everyone to read, write, and execute the file and turn on the set group-ID:
chmod =rwx,g+s file
Notes:
When
chmod is applied to a directory:
read
= list files in the directory
write = add new files to the directory
execute = access files in the directory
write = add new files to the directory
execute = access files in the directory
chown
Change owner, change the user and/or group ownership of each given
File to a new Owner.
Chown can also change the ownership of a file to match the
user/group of an existing reference file.
Some examples of how the owner/group can be specified:
OWNER
If only an OWNER (a user name or numeric user id) is given, that user is made the
owner of each given file, and the files' group is not changed.
OWNER.GROUP
OWNER:GROUP
If the OWNER is followed by a colon or dot and a GROUP (a group name or numeric
group id), with no spaces between them, the group ownership of the files is changed
as well (to GROUP).
OWNER.
OWNER:
If a colon or dot but no group name follows OWNER, that user is made the owner
of the files and the group of the files is changed to OWNER's login group.
.GROUP
:GROUP
If the colon or dot and following GROUP are given, but the owner is omitted, only
the group of the files is changed; in this case, `chown' performs the same function
as `chgrp'.
Ex:
chroot
Run
a command with a different root directory
SYNTAX
chroot NEWROOT [COMMAND [ARGS]...]
cmp
Compare
two files, and if they differ, tells the first byte and line number where they
differ.
Ex:
cp
Copy
one or more files to another location
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
Ex:
copy
floppy to home directory
$ cp -f /mnt/floppy/* /home/simon
Parameters:
-f, --force remove existing destinations, never prompt
-i, --interactive prompt before overwrite
-l, --link link files instead of copying
cron
Cron searches /var/spool/cron for crontab files which are named
after accounts
in /etc/passwd; crontabs found are loaded into memory. Cron also searches
for /etc/crontab and the files in the /etc/cron.d/ directory, which are in a
different format.
in /etc/passwd; crontabs found are loaded into memory. Cron also searches
for /etc/crontab and the files in the /etc/cron.d/ directory, which are in a
different format.
Cron then wakes up every minute, examining all stored crontabs, checking
each command to see if it should be run in the current minute.
each command to see if it should be run in the current minute.
Modifying a cron job
'crontab -e'
crontab (cron table)
Syntax: crontab [ -u user ] { -l | -r | -e }
-l List
-r Remove
-e Edit
Crontab is the program used to
install, deinstall or list the tables used to drive
the cron daemon in Vixie Cron.
the cron daemon in Vixie Cron.
Each line in the cron table follows the following format: 7 fields
left to right
Field
|
Meaning
|
1
|
Minute (0-59)
|
2
|
Hour (2-24)
|
3
|
Day of month (1-31)
|
4
|
Month (1-12, Jan, Feb, ...)
|
5
|
Day of week (0-6) 0=Sunday,
1=Monday ...
or Sun, Mon, Tue, Wed, Thur, Fri |
6
|
User that the command will run as
|
7
|
Command to execute
|
Example
Run
/usr/bin/somecommand at 12.59 every day & supress
output (redirect to null)
59 12 * * * simon /usr/bin/somecommand >> /dev/null 2>&1
59 12 * * * simon /usr/bin/somecommand >> /dev/null 2>&1
Df ( Disk Free - display free disk space.
With no arguments, `df' reports the space used and available on
all currently
mounted filesystems (of all types).
mounted filesystems (of all types).
echo
Display message on screen.
Ex: Echo can
also display in color by using Escape sequences for foreground (30..37)
and background (40..47) colours.
and background (40..47) colours.
dig (domain information groper)
A flexible tool for interrogating (examining) DNS name servers. It
performs DNS lookups and displays the answers that are returned from the name
server(s) that were queried. Most DNS administrators use dig to troubleshoot
DNS problems because of its flexibility, ease of use and clarity of output.
Other lookup tools tend to have less functionality than dig.
dir
Briefly
list directory contents
du
Disk
Usage - report the amount of disk space used by the specified files and for each
subdirectory
Parameters:
`-c'
`--total'
This can be used to find out the total disk usage of a given set of files or directories.
`-h'
`--human-readable'
Ex: List the
total files sizes for everything 1 directory (or less) below the currrent
directory ( . )
fdisk
Partition
table manipulator for Linux
Syntax
fdisk [-u] device
fdisk -l [-u] device ...
fdisk -s partition ...
fdisk -v
fsck
Filesystem
consistency check and interactive repair. Journaling file systems avoid the
need to run fsck.
grep
Search
file(s) for specific text.
Grep stands for: Global Regular Expression
Print.
SYNTAX
grep <options> "Search String" [filename]
grep <options> [-e pattern] [file...]
grep <options> [-f file] [file...]
A simple example:
$grep "Needle in a Haystack" /etc/*
Ifconfig
Interface configurator - display your ip address,
network interfaces, transferred and received data information, configure a
network interface
ifup / ifdown
Bring
a network interface up or down
Examples
Bring
up all the interfaces defined with auto in /etc/network/interfaces
ifup -a
Bring
up interface eth0
ifup eth0
Bring
down all interfaces that are currently up.
ifdown -a
alias
Create an alias, aliases allow a string to be substituted for a word when it is used as the first word of asimple command
Syntax
alias [-p] [name[=value] ...]
unalias [-a] [name ... ]
Key
-p Print the current values
-a Remove All aliases
unalias
may be used to remove each name from the list of defined aliases
Create an
alias 'ls' that will actually run 'ls -F'$ alias ls='ls -F'
$ ls
$ unalias ls
Make an alias permanent
Use your favorite text editor to create a file called ~/.bash_aliases, and type the alias commands into
the file.
.bash_aliases will run at login (or you can just execute it with ..bash_aliases )
awk or gawk (gnu awk)
Find and Replace text, database sort/validate/index.The basic function of awk is to search files for lines (or other units of text) that contain a pattern. When a line matches, awk performs a specific action on that line.
e.g. Display lines from my_file containing the string "123" or "abc" or "some text":
awk '/123/ { print $0 }
/abc/ { print $0 }
/some text/ { print $0 }' my_file
ExamplesThis program prints the length of the longest input line:
awk '{ if (length($0) > max) max = length($0) }
END { print max }' data
This program
prints every line that has at least one field. This is an easy way to delete
blank lines from a file (or rather, tocreate a new file similar to the old file but from which the blank lines have been deleted)
awk 'NF > 0' data
This program
prints seven random numbers from zero to 100, inclusive. awk 'BEGIN { for (i = 1; i <= 7; i++)
print int(101 * rand()) }'
This program
prints the total number of bytes used by FILES. ls -lg FILES | awk '{ x += $5 } ; END { print "total bytes: " x }'
This program
prints a sorted list of the login names of all users. awk -F: '{ print $1 }' /etc/passwd | sort
This program
counts lines in a file. awk 'END { print NR }' data
This program
prints the even numbered lines in the data file. If you were to use theexpression `NR % 2 == 1' instead, it would print the odd numbered lines.
awk 'NR % 2 == 0' data
cal
Syntax
cal [-mjy] [[month] year]
-m Display monday as the first day of the week.
-j Display julian dates (days one-based, numbered from January 1).
-y Display a calendar for the current year.
A single parameter specifies the 4 digit year (1 - 9999) to be displayed.
Two parameters denote the Month (1 - 12) and Year (1 - 9999).
If arguments are not specified, the current month is displayed.
cat
Display the contents of a file (concatenate)Syntax
cat [Options] [File]...
Concatenate FILE(s),
or standard input, to standard outputExamples:
Display a file
$ cat myfile.txt
Concatenate two files:
$ cat file1.txt file2.txt > union.txt
If you need to combine two files but also eliminate duplicates, this can be done with sort unique:
$ sort -u file1.txt file2.txt > unique_union.txt
Put the contents of a file into a variable
$ my_variable=`cat $myfile.txt`
cd
Change Directory - change the current working directory to a specific Folder.Syntax
cd [Options] [Directory]
ExamplesMove to the sybase folder
$ cd /usr/local/sybase
$ pwd
/usr/local/sybase
Change to another folder
$ cd /var/log
$ pwd
/var/log
Quickly get back
$ cd -
$ pwd
/usr/local/sybase
move up one folder
$ cd ..
$ pwd
/usr/local/
$ cd (Back to your home folder)
chgrp
Change group ownership'chgrp' changes the group ownership of each given File to Group (which can be either a group name or a numeric group id) or to the group of an existing reference file.
Example
Make Oracle the owner of the database directory
$chgrp oracle /usr/database
chmod
Change access permissions, change mode.chmod changes the permissions of each given file according to mode, which can be either an octal number representing the bit pattern for the new permissions or a symbolic representation of changes to make, (+-= rwxXstugoa)
From one to four octal digits
Any omitted digits are assumed to be leading zeros.
The first digit = selects attributes for the set user ID (4) and set group ID (2) and save text image (1)S
The second digit = permissions for the user who owns the file: read (4), write (2), and execute (1)
The third digit = permissions for other users in the file's group: read (4), write (2), and execute (1)
The fourth digit = permissions for other users NOT in the file's group: read (4), write (2), and execute (1)
The octal (0-7) value is calculated by adding up the values for each digit
User (rwx) = 4+2+1 = 7
Group(rx) = 4+1 = 5
World (rx) = 4+1 = 5
chmode mode = 0755
Symbolic Mode
The letters 'rwxXstugo' select the new permissions for the affected users:
Read (r),
Write (w),
Execute (or access for directories) (x),
Execute only if the file is a directory or already has execute permission for some user (X),
Set user or group ID on execution (s),
Save program text on swap device (t),
The permissions that the user who owns the file currently has for it (u),
The permissions that other users in the file's group have for it (g),
Permissions that other users not in the file's group have for it (o).
Examples
Deny execute permission to everyone:
chmod a-x file
Allow read permission to everyone:
chmod a+r file
Make a file readable and writable by the group and others:
chmod go+rw file
Make a shell script executable by the user/owner
$ chmod u+x myscript.sh
Allow everyone to read, write, and execute the file and turn on the set group-ID:
chmod =rwx,g+s file
Notes:
When chmod is applied to a directory:
read = list files in the directory
write = add new files to the directory
execute = access files in the directory
chmod never changes the permissions of symbolic links. This is not a problem since the permissions of symbolic links are never used. However, for each symbolic link listed on the command line, chmod changes the permissions of the pointed-to file. In contrast, chmod ignores symbolic links encountered during recursive directory traversals.
chown
Change owner, change the user and/or group ownership of each given File to a new Owner.Chown can also change the ownership of a file to match the user/group of an existing reference file.
chroot
Run a command with a different root directory'chroot' runs a command with a specified root directory.
On many systems, 'chroot' changes the root to the directory NEWROOT (which must exist) and then runs COMMAND with optional ARGS. only the super-user can do this.
chkconfig
Update and query runlevel information for system servicescp
Copy one or more files to another locationCopy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
cron
daemon to execute scheduled commandsSyntax
cron
Cron searches
/var/spool/cron for crontab files
which are named after accounts in /etc/passwd; crontabs found are loaded into
memory. Cron also searches for /etc/crontab and the files in the /etc/cron.d/
directory, which are in a different formatcrontab (cron table)
Schedule a command to run at a later timeSYNTAX
crontab [ -u user ] file
crontab [ -u user ] { -l | -r | -e }
Key
-l List - display the current crontab entries.
-r Remove the current crontab.
-e Edit the current crontab using the editor specified by the
VISUAL or EDITOR environment variables.
After you exit from the editor, the modified crontab will be installed automatically.
cron table
follows the following format: 7 fields left to right
Field
|
Meaning
|
1
|
Minute (0-59)
|
2
|
Hour (2-24)
|
3
|
Day of month (1-31)
|
4
|
Month (1-12, Jan, Feb, ...)
|
5
|
Day of week (0-6) 0=Sunday,
1=Monday ...
or Sun, Mon, Tue, Wed, Thur, Fri |
6
|
User that the command will run as
|
7
|
Command to execute
|
Run /usr/bin/somecommand at 12.59 every day and supress the output (redirect to null)
59 12 * * * simon /usr/bin/somecommand >> /dev/null 2>&1
dig (domain information groper)
A flexible tool for interrogating DNS name servers. It performs DNS lookups and displays the answers that are returned from the name server(s) that were queried. Most DNS administrators use dig to troubleshoot DNS problems because of its flexibility, ease of use and clarity of output. Other lookup tools tend to have less functionality than dig.Modifying a cron job
To edit a users crontab entry, log into your system for that particular user and type crontab -e.
The default editor for the 'crontab -e' command is vi.
Change the default editor by running: export VISUAL='editor'
cron checks each minute to see if its spool directory's modtime (or the modtime on /etc/crontab) has changed, and if it has, cron will then examine the modtime on all crontabs and reload those which have changed. Thus cron need not be restarted whenever a crontab file is modfied. Note that the crontab command updates the modtime of the spool directory whenever it changes a crontab.
date
Display or change the date.date [option] [MMDDhhmm[[CC]YY][.ss]]
dd
Convert and copy a file, write disk headers, boot records, create a boot floppy. dd can makes an exact clone of an (unmounted) disk, this will include all blank space so the output destination must be at least as large as the input.Syntax
dd [Options]
Examples:Clone one hard drive onto another
$ dd if=/dev/sda of=/dev/sdb
Clone a hard drive to an image file
$ dd if=/dev/hda of=/image.img
Clone a hard drive to a zipped image file in 100Mb blocks
$ dd if=/dev/hda bs=100M | gzip -c > /image.img
Create a boot floppy:
$ dd if=boot.img of=/dev/fd0 bs=1440
df
Disk Free - display free disk space.With no arguments, `df' reports the space used and available on all currently mounted filesystems (of all types). Otherwise, `df' reports on the filesystem containing each argument file.
SYNTAX
df [option]... [file]...OPTIONS
`-i'
`--inodes'
`-h'
`--human-readable'
diff
Display the differences between two files, or each corresponding file in two directories.Each set of differences is called a "diff" or "patch". For files that are identical, diff normally produces no output; for binary (non-text) files, diff normally reports only that they are different.
Syntax
diff [options] from-file to-file
-lines Show lines lines of context. This option is obsolete.
-a Treat all files as text and compare them line-by-
line, even if they do not seem to be text.
-b Ignore changes in amount of white space.
-B Ignore changes that just insert or delete blank
lines.
Example$ diff -q <(sort file1.txt | uniq) <(sort file2.txt | uniq)
The command above will return 0 if file1.txt = file2.txt and will return 1 if file1.txt ≠ file2.txt
Examples
dig ss64.com
dig ss64.com SIG
dig ss64.com SIG
dir
Briefly list directory contentsSYNTAX
`dir' (also installed as `d')
du
Disk Usage - report the amount of disk space used by the specified files and for each subdirectory.Syntax
du [options]... [file]...
simon@testserver]$ du -hc --max-depth=1 .
400M ./data1
1.0G ./data2
1.3G .
1.3G total
400M ./data1
1.0G ./data2
1.3G .
1.3G total
echo
Display message on screen, writes each given STRING to standard outputgroups
Print group names a user is in.Syntax
groups [username]...
hostname
Print or set system nameSYNTAX
hostname [name]
ifconfig
Interface configurator - display your ip address, network interfaces, transferred and received data information, configure a network interface.ifup / ifdown
Bring a network interface up or downExamples
Bring up all the interfaces defined with auto in /etc/network/interfaces
ifup -a
Bring up interface eth0
ifup eth0
Bring down all interfaces that are currently up.
ifdown -a
kill
Stop a process from running, either via a signal or forced termination.ln
Make links between files, by default, it makes hard links; with the `-s' option, it makes symbolic (or "soft") links.Parameters:
-s
--symbolic
Make symbolic links instead of hard links. This option merely
produces an error message on systems that do not support symbolic
links.
Examples
$ ln file1.txt link1
$ rm file1.txt #The file cannot be deleted until the link is removed.
$ ln -s /some/name # create a link ./name pointing to /some/name
$ ln -s /some/name mylink2 # or give the link a name
$ ln -s /home/simon/demo /home/jules/mylink3 #Create mylink3 pointing to demo
$ ln -s item1 item2 .. # creates links ../item1 and ../item2 pointing to ./item1 and ./item2
If you delete a file for which a symbolic link
still exists, the rm will succeed but the symbolic link would remain and any
attempt to reference it will return a 'file not found' error. lpc
line printer control programSYNTAX
lpc [command [argument ...]]
DESCRIPTION
Lpc is used by the system administrator to control the operation of the
line printer system. For each line printer configured in /etc/printcap,
lpc may be used to:
· Disable or enable a printer,
· Disable or enable a printer's spooling queue,
· Rearrange the order of jobs in a spooling queue,
·
Find the status of printers
lprm
Remove jobs from the line printer spooling queuels
List information about files.mv
Move or rename files or directoriesOPTIONS
-b
--backup
Make a backup of each file that would otherwise be overwritten or removed.
nslookup
Query Internet name serversNslookup has two modes: interactive and non-interactive.
Interactive mode allows the user to query name servers for information about various hosts and domains or to print a list of hosts in a domain.
Non-interactive mode is used to print just the name and requested information for a host or domain.
open
Open a file in its default application.Example
Open all the text files in the current directory using your default text editor:
open *.txt
passwd
Modify a user password.OPTIONS
-d, --delete delete the password for the named account (root only)
-l, --lock lock the named account (root only)
-u, --unlock unlock the named account (root only)
paste
Merge lines of files, write to standard output lines consisting of sequentially corresponding lines of each given file, separated by a TAB character.
ping :-
Test a network connection. When using ping for fault isolation, it should first be run on the local host, to verify that the local network interface is up and running. Then, hosts and gateways further and further away should be `pinged'.
Show Processor :-
[oracle@sonu ~]$ cat /proc/cpuinfo |grep processor |wc -l
2
[oracle@sonu ~]$ cat /proc/cpuinfo |grep processor
processor : 0
processor : 1
Selinux status :-
[root@sonu oracle]# /usr/sbin/sestatus SELinux status: disabled [root@sonu oracle]# vi /etc/sysconfig/selinux
No comments:
Post a Comment