Linux Basic Commands
Linux Basic Commands
com/2009/07/linux-ls-command-examples/--> basic linux command http://publib.boulder.ibm.com/infocenter/aix/v6r1/index.jsp?topic=%2Fcom.ibm.aix.cmds%2Fdoc %2Faixcmds3%2Fls.htm--> basic Linux command Basic Linux Commands:
CAT
ex:
$cat>amulraj.txt apple Orange press (ctrl+d) You can display the content of the file using cat cammand. Ex: $cat amulraj its shows the content of the amulraj file
touchex:
$touch filename
mkdirEx:
$mkdir amulraj
rmdir
Ex:
$rm amulraj
chdirEx:
lsEx:
3. Display All Information About Files/Directories Using ls -l $ ls -l -rw-r----- 1 ramesh team-dev 9275204 Jun 13 15:27 mthesaur.txt.gz
Just try: $ ls -l --> $ ls -lh --> $ ls -l /etc --> $ ls -ld /etc --> $ ls -lt--> $ ls -ltr--> $ ls -a--> $ ls -A--> ls -q--> $ ls --color=auto-->
copySyn:
cp cp cp cp cp
SOURCE DEST SOURCE DIRECTORY SOURCE1 SOURCE2 SOURCE3 SOURCEn DIRECTORY [OPTION] SOURCE DEST [OPTION] SOURCE DIRECTORY
To copy a file in your current directory into another directory, enter Ex: $ cp filename /tmp $ ls /tmp/filename $ cd /tmp
$ ls $ rm filename To copy all the files in a directory to a new directory, enter: $ cp * /home/tom/backup To copy a directory, including all its files and subdirectories, to another directory, enter (copy directories recursively): $ cp -R * /home/tom/backup
mvsyn: ex:
mv sourceDir destDir
Rename A File
Type the mv command to rename foo.txt to bar.txt:
mv foo.txt bar.txt ls
Rename A Directory
Type the following command to rename a directory in the current directory
mv oldDir newDir mv letters letters.old
Examples
1. To rename a file, enter:
mv appendix apndx.a
This command renames appendix to apndx.a. If a file named apndx.a already exists, its old contents are replaced with those of appendix. 2. To move a directory, enter:
mv book manual
This command moves all files and directories under book to the directory named manual, if manual exists. Otherwise, the directory book is renamed manual. 3. To move a file to another directory and give it a new name, enter:
mv intro manual/chap1
This command moves intro to manual/chap1. The name intro is removed from the current directory, and the same file appears as chap1 in the directory manual. 4. To move a file to another directory, keeping the same name, enter:
mv chap3 manual
This command moves chap3 to manual/chap3 Note: Examples 1 and 3 name two files, example 2 names two existing directories, and example 4 names a file and a directory. 5. To move several files into another directory, enter:
mv chap4 jim/chap5 /home/manual
This command moves the chap4 file to the /home/manual/chap4 file directory and the jim/chap5 file to the /home/manual/chap5 file. 6. To use the mv command with pattern-matching characters, enter:
mv manual/* .
This command moves all files in the manual directory into the current directory . (period), retaining the names they had in manual. This move also empties manual. You must type a space between the asterisk and the period. Note: Pattern-matching characters expand names of existing files only. For example, the command mv intro man*/chap1 does not work if the file manual/chap1 does not exist.
Head
Examples
OR
head -n 5 test
tail
Examples
1. To display the last 10 lines of the notes file, enter:
tail notes
2. To specify the number of lines to start reading from the end of the notes file, enter: tail -n 20 notes 3. To display the notes file a page at a time, beginning with the 200th byte, enter: tail -c +200 notes | pg 4. To follow the growth of a file, enter: tail -f accounts This displays the last 10 lines of the accounts file. The tail command continues to display lines as they are added to the accounts file. The display continues until you press the Ctrl-C key sequence to stop it.
More
Syn:
Examples
1. To view a file named myfile, enter:
more myfile
4. To view each file with the 100th line at the current position, enter:
more -p 100 file1 file2
Typically, the current position in a more command display is the third line on the screen. In this example, the first line on the screen is the 98th line in the file. 5. To view each file starting with the first line that contains the foo string, enter:
more -p /foo file1 file2
The more command displays the line in the current position, the third line on the screen.
hostname
Syntax
/usr/bin/hostname [ HostName ] [ -s ] The /usr/bin/hostname command displays the name of the current host system. Only users with root user authority can set the host name. The mkdev command and the chdev commands also set the host name permanently. Use the mkdev command when you are defining the TCP/IP instance for the first time. You can use the System application in Web-based System Manager (wsm) to change system characteristics. You could also use the System Management Interface Tool (SMIT) smit mkhostname fast path to run this command.
Flags
-s Trims any domain information from the printed name.
Parameters
HostName Sets the primary name of the host. Note: You must have root user authority to use the HostName parameter.
date-
date--->List the date and time of the server. Below is an example of the output. Thu Feb 8 16:47:32 MST 2001 date -s "11/20/2003 12:48:00" Set the date to the date and time shown. date '+DATE: %m/%d/%y%nTIME:%H:%M:%S' Would list the time and date in the below format. DATE: 02/08/01 TIME:16:44:55
time--->
$time
dc--> clear--->
Enters the desk calculator, below is a short example of what can be done: 1234 - Enter base value of 1234 1234 2 * p - Takes the value of 1234 times 2 and prints value 2468 468 - p - Takes the value of 2468 and subtracts 468 2000 2 / p - Takes the value of 2000 and divides by 2. 1000 v p - Takes the value of 1000 and square roots it. 31 q - Quits the desk calucator pr---> Print the files
bc-->
to do a basic calculation
echo-->
cal
free-$free
gzip$ls
$gunzip test.gz
kill-
-> to stop the current process like end process in task mananger
man wc
ex:
---> we can learn more details about one particular command we can use man
$man cat we can learn more about cat --> to display the no of char and line and word in a file
$cat test | wc
its shows 1 1->line 4-> words 17-> char with null value 4 17
who
DESCRIPTION
-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 -i, --idle add idle time as HOURS:MINUTES, . or old (deprecated, use -u) --login print system login processes (equivalent to SUS -l) -l, --lookup attempt to canonicalize hostnames via DNS (-l is deprecated, use --lookup)
-m only hostname and user associated with stdin -p, --process print active processes spawned by init -q, --count all login names and number of users logged on -r, --runlevel print current runlevel -s, --short print only name, line, and time (default) -t, --time print last system clock change -T, -w, --mesg add user's message status as +, - or ? -u, --users list users logged in
eject dos2unix
dos2unix
--->eject the current device --> dos2unix - DOS/MAC to UNIX text file format converter
Convert and replace a.txt. Convert and replace b.txt. dos2unix a.txt b.txt dos2unix -o a.txt b.txt Convert and replace a.txt in ASCII conversion mode. Convert and replace b.txt in ISO conversion mode. Convert c.txt from Mac to Unix ascii format. dos2unix a.txt -c iso b.txt dos2unix -c ascii a.txt -c iso b.txt dos2unix -c mac a.txt b.txt Convert and replace a.txt while keeping original date stamp. dos2unix -k a.txt dos2unix -k -o a.txt Convert a.txt and write to e.txt. dos2unix -n a.txt e.txt Convert a.txt and write to e.txt, keep date stamp of e.txt same as a.txt. dos2unix -k -n a.txt e.txt
Convert and replace a.txt. Convert b.txt and write to e.txt. dos2unix a.txt -n b.txt e.txt dos2unix -o a.txt -n b.txt e.txt Convert c.txt and write to e.txt. Convert and replace a.txt. Convert and replace b.txt. Convert d.txt and write to f.txt. dos2unix -n c.txt e.txt -o a.txt b.txt -n d.txt f.txt
EXAMPLES
Eject the default device: eject Eject a device or mount point named cdrom: eject cdrom Eject using device name: eject /dev/cdrom Eject using mount point: eject /mnt/cdrom/ Eject 4th IDE device: eject hdd Eject first SCSI device: eject sda Eject using SCSI partition name (e.g. a ZIP drive): eject sda4 Select 5th disc on mult-disc changer: eject -v -c5 /dev/cdrom Turn on auto-eject on a SoundBlaster CD-ROM drive: eject -a on /dev/sbpcd
whoami-
password
Current Password: New Password: Confirm New Password:
passwd - entering just passwd would allow you to change the password. After entering passwd you will receive the following three prompts:
Each of these prompts must be entered and entered correctly for the password to be successfully changed.
IP Display Current Config for all NIC's: ifconfig Display Current Config for eth0: ifconfig eth0 Assign IP: ifconfig eth0 192.168.1.2 Ping: ping -c 3 192.168.1.1 Assign multiple IP's: ifconfig eth0:0 192.168.1.2 Assign second IP: ifconfig eth0:1 192.168.1.3 Disable network card: ifconfig eth0 down Enable network card: ifconfig eth0 up View current routing table: route "or" route -n View arp cache: arp "or" arp -n Assign IP/Subnet: ifconfig eth0 192.168.1.2 netmask 255.255.255.0 Assign Default Gateway: route add default gw 192.168.1.1 Trace Route: traceroute www.whatismyip.com Trace Path: tracepath www.whatismyip.com
DNS Test: host www.whatismyip.com Advanced DNS Test: dig www.whatismyip.com Reverse Lookup: host 66.11.119.69 Advanced Reverse Lookup: dig -x 66.11.119.69 *You MUST be at the ROOT user to make/save any changes. Linux users, your distribution will determine the location of your network config file which will need to be updated and saved in order for the changes to remain in effect after rebooting. Network cards are referred to as eth0, eth1, eth2, etc based on their position on the PCI bus. *Special thanks to Gergely for the Linux commands!
Vi
What is vi?
The default editor that comes with the UNIX operating system is called vi (visual editor). [Alternate editors for UNIX environments include pico and emacs, a product of GNU.] The UNIX vi editor is a full screen editor and has two modes of operation: 1. Command mode commands which cause action to be taken on the file, and 2. Insert mode in which entered text is inserted into the file. In the command mode, every character typed is a command that does something to the text file being edited; a character typed in the command mode may even cause the vi editor to enter the insert mode. In the insert mode, every character typed is added to the text in the file; pressing the <Esc> (Escape) key turns off the Insert mode. While there are a number of vi commands, just a handful of these is usually sufficient for beginning vi users. To assist such users, this Web page contains a sampling of basic vi commands. The most basic and useful commands are marked with an asterisk (* or star) in the tables below. With practice, these commands should become automatic. NOTE: Both UNIX and vi are case-sensitive. Be sure not to use a capital letter in place of a lowercase letter; the results will not be what you expect.
page (or screen) of the file will be displayed; if the file does not exist, then an empty file and screen are created into which you may enter text. * vi filename edit filename starting at line 1
vi -r filename recover filename that was being edited when system crashed To Exit vi Usually the new or modified file is saved when you leave vi. However, it is also possible to quit vi without saving the file. Note: The cursor moves to bottom of screen whenever a colon (:) is typed. This type of command is completed by hitting the <Return> (or <Enter>) key. * :x<Return> :q<Return> quit vi, writing out modified file to file named in original invocation quit (or exit) vi
:wq<Return> quit vi, writing out modified file to file named in original invocation * :q!<Return> quit vi even though latest changes have not been saved for this vi call
* k [or up-arrow] * *
* 0 (zero)
* $ w b
move cursor to end of current line move cursor to beginning of next word move cursor back to beginning of preceding word
:0<Return> or 1G move cursor to first line in file :n<Return> or nG move cursor to line n :$<Return> or G move cursor to last line in file
Screen Manipulation
The following commands allow the vi editor screen (or window) to move up or down several lines and to be refreshed. ^f move forward one screen ^b move backward one screen ^d move down (forward) one half screen ^u move up (back) one half screen ^l redraws the screen ^r redraws the screen, removing deleted lines
I insert text at beginning of current line, until <Esc> hit * a append text after cursor, until <Esc> hit A append text to end of current line, until <Esc> hit * o open and put text in a new line below current line, until <Esc> hit * O open and put text in a new line above current line, until <Esc> hit Changing Text The following commands allow you to modify text. * r R cw cNw C cc Ncc or cNc replace single character under cursor (no <Esc> needed) replace characters, starting with current cursor position, until <Esc> hit change the current word with new text, starting with the character under cursor, until <Esc> hit change N words beginning with character under cursor, until <Esc> hit; e.g., c5w changes 5 words change (replace) the characters in the current line, until <Esc> hit change (replace) the entire current line, stopping when <Esc> is hit change (replace) the next N lines, starting with the current line, stopping when <Esc> is hit
Deleting Text The following commands allow you to delete text. * x Nx dw dNw D * dd Ndd or dNd delete single character under cursor delete N characters, starting with character under cursor delete the single word beginning with character under cursor delete N words beginning with character under cursor; e.g., d5w deletes 5 words delete the remainder of the line, starting with current cursor position delete entire current line delete N lines, beginning with the current line; e.g., 5dd deletes 5 lines
Cutting and Pasting Text The following commands allow you to copy and paste text. yy copy (yank, cut) the current line into the buffer
Nyy or yNy copy (yank, cut) the next N lines, including the current line, into the buffer p put (paste) the line(s) in the buffer into the text after the current line
Other Commands
Searching Text A common occurrence in text editing is to replace one word or phase by another. To locate instances of particular sets of characters (or strings), use the following commands. /string search forward for occurrence of string in text ?string search backward for occurrence of string in text n N move to next occurrence of search string move to next occurrence of search string in opposite direction
Determining Line Numbers Being able to determine the line number of the current line or the total number of lines in the file being edited is sometimes useful. :.= returns line number of current line at bottom of screen := ^g returns the total number of lines at bottom of screen provides the current line number, along with the total number of lines, in the file at the bottom of the screen