Sunteți pe pagina 1din 14

Frequently used Linux/Unix commands.

1. tar :- Create a new tar archive $ tar cvf archive_name.tar dirname/ Extract from an existing tar archive. $ tar xvf archive_name.tar View an existing tar archive. $ tar tvf archive_name.tar 2. grep - Search for a given string in a file (case in-sensitive search). $ grep -i "the" demo_file Print the matched line, along with the 3 lines after it. $ grep -A 3 -i "example" demo_text Search for a given string in all files recursively $ grep -r "ramesh" * 3. find - Find files using file-name ( case in-sensitve find) eg. find -iname "MyCProgram.c" Execute commands on files found by the find command $ find -iname "MyCProgram.c" -exec md5sum {} \; Find all empty files in home directory # find ~ -empty 4. ssh - Login to remote host eg. ssh -l jsmith remotehost.example.com, ssh -p 722 nzp2jobs@ewr-nzmdw-n3.mediamath.com Debug ssh client ssh -v -l jsmith remotehost.example.com Display ssh client version $ ssh -V OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003 5. sed - When you copy a DOS file to Unix, you could find \r\n in the end of each line. This example converts the DOS file format to Unix file format using sed command. eg. $sed 's/.$//' filename, sed -e "s/START_BATCHID/${MIN_BATCHID}/g" -e "s/END_BATCHID/${MAX_BATCHID}/g"

Print file content in reverse order $ sed -n '1!G;h;$p' thegeekstuff.txt Add line number for all non-empty-lines in a file $ sed '/./=' thegeekstuff.txt | sed 'N; s/\n/ /' 6. diff - Ignore white space while comparing. eg. diff -w name_list.txt name_list_new.txt 2c2,3 < John Doe --- > John M Doe > Jason Bourne 7. sort - Sort a file in ascending order. eg. $ sort names.txt Sort a file in descending order $ sort -r names.txt Sort passwd file by 3rd field. $ sort -t: -k 3n /etc/passwd | more 8. export - To view oracle related environment variables. eg. $ export | grep ORACLE declare -x ORACLE_BASE="/u01/app/oracle" declare -x ORACLE_HOME="/u01/app/oracle/product/10.2.0" declare -x ORACLE_SID="med" declare -x ORACLE_TERM="xterm" To export an environment variable: $ export ORACLE_HOME=/u01/app/oracle/product/10.2.0 9. xargs - Copy all images to external hard-drive. eg. # ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory Search all jpg images in the system and archive it. # find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz Download all the URLs mentioned in the url-list.txt file # cat url-list.txt | xargs wget c

10. ls - Display filesize in human readable format (e.g. KB, MB etc.,) eg. $ ls -lh -rw-r----- 1 ramesh team-dev 8.9M Jun 12 15:27 arch-linux.txt.gz Order Files Based on Last Modified Time (In Reverse Order) Using ls -ltr $ ls -ltr Visual Classification of Files With Special Characters Using ls -F $ ls -F To show single entry per line, use -1 option as shown below. ls -1 11. pwd - pwd is Print working directory. What else can be said about the good old pwd who has been printing the current directory name for ages. 12. "cd -" :- Use "cd -" to toggle between the last two directories (like Alt+tab in window). 13. gzip - To create a *.gz compressed file. eg. $ gzip test.txt To uncompress a *.gz file: $ gzip -d test.txt.gz Display compression ratio of the compressed file using gzip -l $ gzip -l *.gz compressed 23709 uncompressed ratio uncompressed_name 97975 75.8% asp-patch-rpms.txt

14. bzip2 - To create a *.bz2 compressed file eg. $ bzip2 test.txt To uncompress a *.bz2 file: bzip2 -d test.txt.bz2 15. unzip - To extract a *.zip compressed file eg.$ unzip test.zip View the contents of *.zip file (Without unzipping it): $ unzip -l jasper.zip

Archive: jasper.zip Length --------40995 32169 15964 10542 Date ---Time ---Name

--------------META-INF/MANIFEST.MF classes_ classes_names classes_ncomp

11-30-2013 23:50 08-25-2013 21:07 08-25-2013 21:07 08-25-2013 21:07

16. shutdown - Shutdown the system and turn the power off immediately. eg. # shutdown -h now Shutdown the system after 10 minutes. # shutdown -h +10 Reboot the system using shutdown command. # shutdown -r now Force the filesystem check during reboot. # shutdown -Fr now 17. ftp command - Both ftp and secure ftp (sftp) has similar commands. To connect to a remote server and download multiple files, do the following eg. $ ftp IP/hostname ftp> mget *.html To view the file names located on the remote server before downloading, mls ftp command as shown below. ftp> mls *.html /ftptest/features.html /ftptest/index.html /ftptest/othertools.html /ftptest/samplereport.html /ftptest/usage.html Use the get command to download file from a remote ftp server as shown below. ftp> get FILENAME Use put command to upload a file to a remote ftp server as shown below

ftp> put filename Downloading multiple files with mget command mget is for fetching multiple files from ftp server. You can use globs to download multiple files. For example, *.html will download all html files. The glob expansion are done on the remote server. So, it depends on the operating system of the remote server. eg. ftp> mget *.html Fetching /ftptest/features.html to features.html /ftptest/features.html 100% 2256 2.2KB/s 00:01

Fetching /ftptest/index.html to index.html /ftptest/index.html 100% 2886 2.8KB/s 00:01

Fetching /ftptest/othertools.html to othertools.html /ftptest/othertools.html 100% 2282 2.2KB/s 00:01

Fetching /ftptest/samplereport.html to samplereport.html /ftptest/samplereport.html 100% 15KB 7.3KB/s 00:02

Fetching /ftptest/usage.html to usage.html /ftptest/usage.html 100% 2340 2.3KB/s 00:01

To view the file names before downloading, you can also use mls command as shown below ftp> mls *.html /ftptest/features.html /ftptest/index.html /ftptest/othertools.html /ftptest/samplereport.html /ftptest/usage.html Uploading multiple files at a time. ftp> mput *.html 18. crontab command - Used for job scheduling. View crontab entry for a specific user # crontab -u john -l Schedule a cron job every 10 minutes. */10 * * * * /home/ramesh/check-disk-space.sh

--Cron Job everyday during working hours. eg. 00 09-18 * * * /home/trng/anuj/test_extract_ora.sh Description of entry :00 0th Minute (Top of the hour) 09-18 9 am, 10 am,11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm * Every day * Every month * Every day of the week --Cron Job every weekday during working hours. This example checks the status of the database every weekday (i.e excluding Sat and Sun) during the working hours 9 a.m 6 p.m. eg. 00 09-18 * * 1-5 /home/ramesh/bin/check-db-status Description :00 0th Minute (Top of the hour) 09-18 9 am, 10 am,11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm * Every day * Every month 1-5 -Mon, Tue, Wed, Thu and Fri (Every Weekday) --How to view crontab crontab l OR cat crontab --How to edit crontab crontab e OR vi crontab --How to remove crontab ##USE with very Carefully (crontab -r) --Crontab HowTo: View Other Linux Users Crontabs entries root@dev-db# crontab -u sathiya -l @monthly /home/sathiya/monthly-backup 00 09-18 * * * /home/sathiya/check-db-status --There are special cases in which instead of the above 5 fields you can use @ followed by a keyword such as reboot, midnight, yearly, hourly.

Table: Cron special keywords and its meaning Keyword @yearly @daily @hourly @reboot Equivalent 0011* 00*** 0**** Run at startup.

--Schedule a Job For First Minute of Every Year using @yearly This will execute the system annual maintenance using annual-maintenance shell script at 00:00 on Jan 1st for every year. @yearly /home/anuj/scripts/annual-maintenance.sh --Schedule a Cron Job Beginning of Every Month using @monthly This will execute the shell script tape-backup at 00:00 on 1st of every month. @monthly /home/ramesh/suse/bin/tape-backup.sh --Installing Crontab From a Cron File 19. ps command - ps command is used to display information about the processes that are running in the system To view current running processes $ ps -ef | more To view specific process. $ ps -ef | process_name 20. free command - This command is used to display the free, used, swap memory available in the system. $ free Mem: total 3566408 used 1580220 473272 0 free shared buffers 0 cached 902960

1986188 3093136

203988

-/+ buffers/cache: Swap: 4000176

4000176

--If you want to quickly check how many GB of RAM your system has use the -g option. -b option displays in bytes, -k in kilo bytes, -m in mega bytes. $ free -g Mem: 3 total 1 0 used 1 2 free 0 shared 0 buffers 0 cached

-/+ buffers/cache:

Swap:

--If you want to see a total memory ( including the swap), use the -t switch, which will display a total line as shown below. $ free -t Mem: total 3566408 used 1592148 475332 0 1592148 free 1974260 3091076 4000176 5974436 shared 0 buffers 204260 cached 912556

-/+ buffers/cache: Swap: Total: 4000176 7566584

21. top command - top command displays the top processes in the system ( by default sorted by cpu usage ). To sort top output by any column, Press O (upper-case O) , which will display all the possible columns that you can sort by as shown below. $ top 22. df command - Displays the file system disk space usage. By default df -k displays output in bytes. $ df -k Filesystem /dev/sda1 /dev/sda2 1K-blocks 29530400 Used Available Use% Mounted on 3233104 24797232 12% /

120367992 50171596 64082060 44% /home

--df -h displays output in human readable form. i.e size will be displayed in GBs. $ df -h Filesystem /dev/sda1 /dev/sda2 Size Used Avail Use% Mounted on 29G 3.1G 115G 48G 24G 12% / 62G 44% /home

--Use -T option to display what type of file system $ df -T Filesystem /dev/hda3 /dev/hda1 tmpfs Type ext3 ext3 tmpfs 1K-blocks Used Available Use% Mounted on

145214968 20936568 116782876 16% / 101086 1024508 10007 0 85860 11% /boot 0% /dev/shm

1024508

23. kill command - Use kill command to terminate a process. First get the process id using ps -ef command, then use kill -9 (forcefully killing) to kill the running Linux process as shown below. You can also use killall, pkill, xkill to terminate a unix process. $ ps -ef | grep vim trng 7243 7222 9 22:43 pts/2 00:00:00 vim

$ kill -9 7243 (Forcefully killing a process) 24. rm command - Removing a file. Get confirmation before removing the file. $ rm -i filename.txt It is very useful while giving shell metacharacters in the file name argument. Print the filename and get confirmation before removing the file. $ rm -i file* Following example recursively removes all files and directories under the example directory. This also removes the example directory itself. $ rm -r example 25. cp command - Copying a file. Copy file1 to file2 preserving the mode, ownership and timestamp. $ cp -p file1 file2 Copy file1 to file2. if file2 exists prompt for confirmation before overwritting it. $ cp -i file1 file2 26. mv command - Moving (or Renaming) a file from one location to another. Rename file1 to file2. if file2 exists prompt for confirmation before overwritting it. $ mv -i file1 file2 Note: mv -f is just the opposite, which will overwrite file2 without prompting. mv -v will print what is happening during file rename, which is useful while specifying shell metacharacters in the file name argument. $ mv -v file1 file2 27. cat command - You can view multiple files at the same time. Following example prints the content of file1 followed by file2 to stdout. eg. $ cat file1 file2 --While displaying the file, following cat -n command will prepend the line number to each line of the output $ cat -n /etc/logrotate.conf 1 /var/log/btmp { 2 3 4 missingok monthly create 0660 root utmp

5 6 }

rotate 1

28. mount command - To mount a file system, you should first create a directory and mount it as shown below. eg. # mkdir /u01 # mount /dev/sdb1 /u01 --You can also add this to the fstab for automatic mounting. i.e Anytime system is restarted, the filesystem will be mounted /dev/sdb1 /u01 ext2 defaults 0 2 29. chmod command - chmod command is used to change the permissions for a file or directory. eg. Give full access to user and group (i.e read, write and execute ) on a specific file. $ chmod ug+rwx file.txt Revoke all access for the group (i.e read, write and execute ) on a specific file. $ chmod g-rwx file.txt Apply the file permissions recursively to all the files in the sub-directories. $ chmod -R ug+rwx file.txt chmod commands examples. 1. Add single permission to a file/directory Changing permission to a single set. + symbol means adding permission. For example, do the following to give execute permission for the user irrespective of anything else: $ chmod u+x filename 2. Add multiple permission to a file/directory Use comma to separate the multiple permission sets as shown below. $ chmod u+r,g+x filename 3. Remove permission from a file/directory Following example removes read and write permission for the user. $ chmod u-rx filename 4. Make permission for a file same as another file (using reference) If you want to change a file permission same as another file, use the reference option as shown below. In this example, file2's permission will be set exactly same as file1's permission. $ chmod --reference=file1 file2 5. Apply the permission to all the files under a directory recursively

Use option -R to change the permission recursively as shown below. $ chmod -R 755 directory-name/ 6. Change execute permission only on the directories (files are not affected) On a particular directory if you have multiple sub-directories and files, the following command will assign execute permission only to all the sub-directories in the current directory (not the files in the current directory). $ chmod u+X * 30. passwd command - Change your password from command line using passwd. This will prompt for the old password followed by the new password. $ passwd --Super user can use passwd command to reset others password. This will not prompt for current password of the user. # passwd USERNAME --Remove password for a specific user. Root user can disable password for a specific user. Once the password is disabled, the user can login without entering the password. # passwd -d USERNAME 31. mkdir command - It will create a directory specified by user. $ mkdir ~/temp --Create nested directories using one mkdir command. If any of these directories exist already, it will not display any error. If any of these directories doesnt exist, it will create them. $ mkdir -p dir1/dir2/dir3/dir4/ 32. ifconfig command - Use ifconfig command to view or configure a network interface on the Linux system. View all the interfaces along with status. $ ifconfig -a Start or stop a specific interface using up and down command as shown below. $ ifconfig eth0 up $ ifconfig eth0 down 33. uname command - Uname command displays important information about the system such as Kernel name, Host name, Kernel release number,Processor type etc. Sample uname output from a Linux desktop is shown below. [trng@MM-SERVER anuj]$ uname -a Linux MM-SERVER 2.6.18-8.el5 #1 SMP Fri Jan 26 14:15:21 EST 2007 i686 i686 i386 GNU/Linux

34. whereis command - When you want to find out where a specific Unix command exists (for example, where does ls command exists?), you can execute the following command. eg. [trng@MM-SERVER anuj]$ whereis ls ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz 35. whatis command - Whatis command displays a single line description about a command. eg. [trng@MM-SERVER anuj]$ whatis ls ls ls (1) - list directory contents (1p) - list directory contents

36. locate command - Using locate command you can quickly search for the location of a specific file (or group of files). Locate command uses the database created by updatedb eg. The example below shows all files in the system that contains the word crontab in it. [trng@MM-SERVER anuj]$ locate crontab /etc/anacrontab /etc/crontab /usr/bin/crontab /usr/share/man/man1/crontab.1.gz /usr/share/man/man1p/crontab.1p.gz /usr/share/man/man5/anacrontab.5.gz /usr/share/man/man5/crontab.5.gz /usr/share/vim/vim70/syntax/crontab.vim 37. man command - Display the man page of a specific command eg. $ man crontab --When a man page for a command is located under more than one section, you can view the man page for that command from a specific section as shown below. $ man SECTION-NUMBER commandname Following 8 sections are available in the man page. 1.General commands 2.System calls 3.C library functions 4.Special files (usually devices, those found in /dev) and drivers 5.File formats and conventions 6.Games and screensavers

7.Miscellaneous 8.System administration commands and daemons eg. [trng@MM-SERVER anuj]$ whatis crontab crontab crontab crontab (1) - maintain crontab files for individual users (ISC Cron V4.1) (1p) - schedule periodic background work (5) - tables for driving cron (ISC Cron V4.1)

[trng@MM-SERVER anuj]$ man 1p crontab 38. tail command - To print the last line (By default 10) of a file. eg. Print the last 10 lines of a file by default. $ tail filename.txt --Print N number of lines from the file named filename.txt $ tail -n N filename.txt --View the content of the file in real time using tail -f. This is useful to view the log files, that keeps growing. The command can be terminated using CTRL-C. $ tail -f log-file 39. less command - less is very efficient while viewing huge log files, as it doesnt need to load the full file while opening eg. $ less huge-log-file.log One you open a file using less command, following two keys are very helpful. CTRL+F forward one window CTRL+B backward one window 40. wget command - The quick and effective method to download software, music, video from internet is using wget command. eg. $ wget http://www.google.com/lion-3.2.1.tar.gz Download and store it with a different name. $ wget -O taglist.zip http://www.vim.org/scripts/download_script.php?src_id=7701 41. To know how many lines are there in a file. 1. wc -l file_name 2. sed -n '$=' file_name

---------------------------NOTE :- If, You have a huge size log file 5GB(let it has 200000 row's) & u want to see the log in the middle(10 row in the middle).Then you can use the following. [trng@MM-SERVER anuj]$ cat -n help_cmd.txt| head -20 | tail -10 11 12 13 14 15 16 17 18 19 20 [ arg... ] [[ expression ]] bg [job_spec ...]

alias [-p] [name[=value] ... ]

bind [-lpvsPVS] [-m keymap] [-f fi break [n] builtin [shell-builtin [arg ...]] caller [EXPR] case WORD in [PATTERN [| PATTERN]. cd [-L|-P] [dir] command [-pVv] command [arg ...] compgen [-abcdefgjksuv] [-o option

complete [-abcdefgjksuv] [-pr] [-o continue [n] declare [-afFirtx] [-p] [name[=val dirs [-clpv] [+N] [-N] disown [-h] [-ar] [jobspec ...] echo [-neE] [arg ...]

enable [-pnds] [-a] [-f filename] eval [arg ...]

-----------------------------

S-ar putea să vă placă și