Sie sind auf Seite 1von 12

Linux commands Copyright_Cybrog_TIBO 1 Basic Commands 1.

ls -l for listing the files as well as directories those are kept in the particular working directory syntax [root@TIBO root]#ls -l 2.ls -la same as 'ls -l'but by this command we can also see the hiden files. syntax [root@TIBO root]#ls la 3.ls -li same as 'ls -la' but it will also shows us the inode number of each and every file syntax [root@TIBO root]#ls li 4.ls by this command we can see only file name nothing else syntax [root@TIBO root]#ls 5.clear it will clear the screen(short cut ctl+l) syntax [root@TIBO root]#clear 6.exit to end a current session as well current terminal logging syntax [root@TIBO root]exit 7.touch to create a new empty file syntax [root@TIBO root]#touch 8.cd to change the working/present directory syntax [root@TIBO root]#cd /home/mango where '/home/mango' is the desired directory to be change from '/root' 9.cat to view the contents of a file and it is also used for creating a new file with some contents syntax [root@TIBO root]#cat <file name> to view file contents [root@TIBO root]#cat > newfilename enter,then you can write something in the file and then to save the file contents press clt+d then enter 10.mkdir to make a new directory syntax [root@TIBO root]#mkdir newdirname you can also create a directory at your desired path without changing your present working directory syntax [root@TIBO root]#mkdir /home/mango/newdirname 11.rm to remove a empty file syntax [root@TIBO root]#rm filename Linux commands 2 12.rmdir to remove a empty directory syntax [root@TIBO root]#rmdir directoryname 13.rm [-i/-r/-f] to remove a directory with its subdirectories as well as its

files that is to remove a directory which already contains some files in it syntax [root@TIBO root]#rm -i directory/filename -i stands for interactively -r stands for recursively -f stands for forcefully 14.cp to copy something in a destination file or directory syntax [root@TIBO root]#cp sourcepath destinationpath example: [root@TIBO root]#cp /home/mango/webmin.rpm /root/abcd in this example the webmin.rpm file will be copied in /root/abcd directory 15.mv to move one file or directory from one place to another place, it is also used for renaming adirectory or file syntax [root@TIBO root]#mv source destination [root@TIBO root]#mv oldfilename newfilename [to change the file name] 16.man to view the mannual page of commands for syntax syntax [root@TIBO root]#man commandname 17.info to view the information about any command syntax [root@TIBO root]#mkdir info 18.--help to view the help doccuments of a command syntax [root@TIBO root]#commandname --help 19.dir to view the subdirectories and filesn under the directory syntax [root@TIBO root]#dir 20.su - to become a super user syntax [mango@TIBO mango]$su output wil be [root@TIBO root#] 1.who by this command you can see the user name and their ip addresses who have loged in on your server syntax [root@TIBO root]#who 22.whoami this command shows your current logged in terminal user name Linux commands 3 syntax [root@TIBO root]#whoami 23.who am i this command shows you the logged in terminal number and user name and more detailed information Page 2Basic Commands syntax [root@TIBO root]#who am i 24.pwd to view the present working directory

syntax [root@TIBO root]#pwd 25.rpm -ivh to intall a rpm package syntax [root@TIBO root]#rpm -ivh packagename.rpm rpm stands for 'redhat package manager' -i stands for install -v stands for verbose mode -h stands for with hash sign(#) 26.rpm -q to querry about any rpm package syntax [root@TIBO root]#rpm -q packagename 27.rpm -e to uninstall a rpm package synatx [root@TIBO root]#rpm -e package 28.find / -name to find any file or directory in linux file system syntax [root@TIBO root]#find / -name filename 29.su username to switch from one user to another users home directory syntax [root@TIBO root]#su mango output will be [mango@TIBO root]#cd [mango@TIBO mango]# 30.su - username to switch from one user to another user users home directory directly syntax [root@TIBO root]#su - mango 31.useradd to create a new user synatx [root@TIBO root]#useradd username 32.passwd to give a password of a user syntax Linux commands 4 [root@TIBO root]#passwd TIBO output will be give a password for user TIBO:(here you have to type a password for TIBO user) confirm password:(again type the same password) 33.userdel to remove a user from linux syntax [root@TIBO root]#userdel TIBO 34.groupadd to add a new group syntax [root@TIBO root]#groupadd groupname 35.gruopdel to delete a group syntax [root@TIBO root]#groupdel groupname 36.chown to change the ownership of a file or directory syntax

[root@TIBO root]#chown ownername filename example: [root@TIBO /]#ls -l output drwxrw-rw- 2 root root 4096 Mar 11 12:03 abcd (permission) (own) (group own)(size) (name) [root@TIBO root]#chown TIBO /abcd in this example /abcd directory owner will be change to TIBO user effect [root@TIBO /]#ls -l drwxrw-rw- 2 TIBO root 4096 Mar 11 12:03 abcd 37.chgrp to change the group ownership of a file or directory syntax [root@nettec root]#chgrp newgroupownername filename example [root@TIBO /]#ls -l drwxrw-rw- 2 TIBO root 4096 Mar 11 12:03 abcd [root@TIBO root]#chgrp TIBO /abcd effect [root@TIBO /]#ls -l drwxrw-rw- 2 TIBO TIBO 4096 Mar 11 12:03 abcd 38.chmod to change the permission of a file or directory drwxrw-rw- 2 root root 4096 Mar 11 12:03 abcd (permission) (ownr) (grpownr) (size) (name) IN OCTAL VALUE d stands for directiry READ=4 r stands for read only permission WRITE=2 w stands for write only permission EXECUTE=1 x stands for execute only permission drwxrw-rw- FIRST OCTET FOR DENOTING THE DIRECTORY OR FILE OR LINK FILE ETC. Linux commands 5 SECOND THREE OCTET FOR USER OR OWNER PERMISSION (rwx OR 7 IN OCTAL VALUE) THIRD THREE OCTET FOR GROUP PERMISSION (rw- OR 6 IN OCTAL VALUE) FORTH THREE OCTET FOR OTHERS PERMISSION (rw- OR 6 IN OCTAL VALUE) SYNTAX [root@nettec root]#chmod value fileordirectoryname example [root@TIBO /]#ls -l drwxrw-rw- 2 TIBO root 4096 Mar 11 12:03 abcd [root@TIBO /]#chmod 402 /abcd [root@TIBO /]#ls -l drw-----w- 2 TIBO TIBO 4096 Mar 11 12:03 abcd 40.usermod to modify the user profile Page 4Basic Commands synatx [root@TIBO root]#usermod -parameter groupname username General Purpose utilities:

Calender cal: Command to see calender for any specific month or a complete year cal [ [month] year] $ cal February 2012 February 2012 Su Mo Tu We Th Fr Sa 1234 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 22 23 24 25 26 27 28 29 30 date date: displays the current date $ date Tue Apr 1 1:33:49 IST 2012 savan$ date +"%D %H:%M:%S" 04/1/09 1: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 For more information see man date Calculator bc: A text based calculator $ bc 2*10+20-9+4/2 [Input] 33 [Output] [ctrl+d] [Quit] xcalc is graphical based calculator Linux commands 6 script: Record your session script command records your session and stores it in a file $ 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 $ cat typescript Script started on Tuesday 1 February 2012 10:07:00 $ echo "this is a sample script" this is a sample script $ Script done on Tuesday 1 February 2012 10:07:34 PM passwd: Changing your password

passwd command allows you to change your password savan:/workshop$ passwd Changing password for Rejna. (current) UNIX password: Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully WHO: Who are the users? who command tells you the users currently logged on to the system savan:$ who Rejna pts/0 2012-04-15 11:58 (:10.129.41.3) Debo pts/1 2012-04-15 16:09 (:10.129.20.5) Dintu pts/2 2012-04-13 14:39 (:10.129.45.20) anil pts/3 2012-04-13 16:32 (:10.129.23.45) man - The reference Manual man displays the documentation for a command usage: man <command name> ls - list directory contents SYNOPSIS ls [OPTION]... [FILE]... DESCRIPTION List information about the FILEs (the none of -cftuvSUX nor --sort. Linux File System: Linux file system Standard directory structure / - the topmost /dev - all the devices are accessible as files /var - variable data such as mails, log files, databases Linux commands 7 /usr - almost all the packages installed /etc - configuration files /home - home directories for all the users /root - home directory of the privileged user root /mnt - used to mount other directories/partitions. File Attributes To see the file attributes type ls -l on your terminal savan:$ ls -l $<$permissions$>$ $<$owner$>$ $<$group$>$ drwxr-xr-x 2 Rejna Rejna 144 2012-04-02 -rw-r--r-- 1 Rejna Rejna 1548 2012-03-29 drwxr-xr-x 2 Rejna Rejna 48 2012-03-19 -rw-r--r-- 1 Rejna Rejna 3570 2012-03-23 The file Testing.java has the following permissions -rw-rr 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-rr 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 -rw-r--r-- 1 Rejna Rejna 3570 2012-03- The file Testing.java has the owner as Rejna and also belongs to a group called Rejna Changing the File attributes chmod Changing the permissions of the file savan:$ chmod o+x Testing.java savan:$ ls -l Testing.java -rw-r--r-x 1 Rejna Rejna 3570 2012-03-23 10:savan:$ chmod 655 Testing.java savan:$ ls -l Testing.java -rw-r-xr-x 1 Rejna Rejna 3570 2012-03-23 10: Changing ownership chown command is used for changing the ownership and also group of the file savan:$ chown guest Testing.java savan:$ ls -l Testing.java -rw-r-xr-x 1 geust Rejna 3570 2012-03-23 10:52 savan:$ chown guest:guest Testing.java savan:$ ls -l Testing.java -rw-r-xr-x 1 geust guest 3570 2012-03-23 10:52 Testing Linux commands 8 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 File Handling Commands:

cat : Concatenate Files cat command is used to display the contents of a small file on terminal usage: cat <file name> $ 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 $ cat sample3.txt sample4.txt /*contents of sameple3.txt*/ /*Followed by contents of sample4.txt without any 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> $ 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 $ tac sample3.txt sample4.txt /*print sample3.txt in reverse order*/ /*print sample4.txt in reverse order without any more, less : paging output more and less commands are used to view large files one page at a time usage: more <file name> usage: less <file name> $ more sample1.txt /*sample1.txt will be displayed one page at a time */ $ less sample1.txt /*sample1.txt will be displayed one page Linux commands 9 at a time */ less is the standard pager for linux and in general less is more powerful than more wc : statistic of file wc command is used to count lines, words and characters, depending on the option used. usage: wc [options] [file name] $ 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 cmp: comparing two files cmp command is used to compare two files whether they are identical or not usage: cmp <file1> <file2> 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 $ 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*/ comm : what is common? comm command displays what is common between both the files usage: comm <file1> <file2> The input files to comm command should be sorted alphabetically $ comm sample5.txt sample6.txt anil bhadro dintu rejna savan singhvi sumit Column 1 gives the names which are present in sample5.txt but not in sample6.txt Column 2 gives the names which are not present in sample5.txt but present in sample6.txt Column 3 gives the names which are present in both the Files Linux commands 10 Compressing and Archiving Files: 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 $ 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> $ 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 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] <output file.tar> <file1 or dir> . . . The following are the options: -c Create an archive -x Extract files from archive -t Display files in archive -f arch Name the archive arch $ 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 $ tar -tvf compression.tar drwxr-xr-x Rejna/Rejna 0 2012-04-22 11:29 drwxr-xr-x Rejna/Rejna 0 2012-04-22 11:29 -rw-r--r-- Rejna/Rejna 17663 2012-04-22 11:22 -rw-r--r-- Rejna/Rejna 17333 2012-04-22 11:22 Linux commands 11 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 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> $ 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 Simple Filters: Filters Filters are commands which accept data from standard input, manupulate it and write the results to standard output head command displays the top of the file, when used without any option it will display first 10 lines of the file $ head sample1.txt /*display first 10 lines*/ Similarly tail command displays the end of the file. By default it will display last 10 lines of the file $ tail sample1.txt /*display last 10 lines*/ tail or head with -n followed by a number will display that many number of lines from last and from first respectively $ head -n 20 sample1.txt /* will display first 20 lines*/ $ tail -n 15 sample1.txt /* will display last 15 lines */ cut : cutting columns cut command can be used to cut the columns from a file with -c option usage: cut -c [numbers delemited by comma or range] <file name> $ cut -c 1,2,3-5 students.txt 1 ani 2 das Linux commands 12 3 shu 4 sin cut : cutting fields With -f option you can cut the feilds delemited by some character $ cut -d" " -f1,4 students.txt 1 Mtech 2 Btech 3 Mtech -d option is used to specify the delimiter and -f option used

to specify the feild number paste : pasting side by side paste command will paste the contents of the file side by side $ paste cutlist1.txt cutlist2.txt 1 Mtech 1 anil H1 2 Btech 2 dintu H4 3 Mtech 3 TIBO H7 4 Mtech 4 singhvi H12 5 Btech 5 sumit H13 sort : ordering a file sort re-orders lines in ASCII collating sequenceswhitespaces first, then numerals, uppercase and finally lowercase you can sort the file based on a field by using -t and -k option. $ sort -t" " -k 2 students.txt /* sorts the file based on the second field using the delimiter as space*/ grep : searching for a pattern grep scans its input for a pattern, and can display the selected pattern, the line numbers or the filename where the pattern occurs. usage: grep options pattern filename(s)

Das könnte Ihnen auch gefallen