Some commands to be mastered in CentOS

Source: Internet
Author: User
Tags ftp commands gz file uppercase letter disk usage

Some commands to be mastered in CentOS
Details:

1. tar

Create a new tar file

$ tar cvf archive_name.tar dirname/

Decompress the tar file

$ tar xvf archive_name.tar

View tar files

$ tar tvf archive_name.tar
2. grep

Search for strings in files (Case Insensitive)

$ grep -i "the" demo_file

Output successfully matched rows and the three rows after the row

$ grep -A 3 -i "example" demo_text

Recursively queries a file containing a specified string in a folder

$ grep -r "ramesh" *
3. find

Searches for files with the specified file name (Case Insensitive)

$ find -iname "MyProgram.c"

Execute a command on the found File

$ find -iname "MyProgram.c" -exec md5sum {} \;

Find all empty files in the home Directory

$ find ~ -empty
4. ssh

Log on to the remote host

$ ssh -l jsmith remotehost.example.com

Debug an ssh client

$ ssh -v -l jsmith remotehost.example.com

Show ssh client version

$ ssh -V
5. sed

After you copy files in the DOS system to Unix/Linux, each line of this file will end with \ r \ n, and sed can easily convert it to a Unix file, use the file ending with \ n

$ sed 's/.$//' filename

Reverse file content and Output

$ Sed-n' 1! G; h; P' filename

Add row numbers for non-empty rows

$ sed '/./=' thegeekstuff.txt | sed 'N; s/\n/ /'
6. awk

Delete duplicate rows

$ awk '!($0 in array) { array[$0]; print}' temp

Print all rows containing the same uid and gid in/etc/passwd.

$ awk -F ':' '$3=$4' /etc/passwd

Print the specified fields in the file.

$ awk '{print $2,$5;}' employee.txt
7. vim

Open the file and jump to the 10th line

$ vim +10 filename.txt

Open the file and jump to the first matched row.

$ vim +/search-term filename.txt

Open a file in read-only mode

$ vim -R /etc/passwd
8. diff

Ignore blank characters during comparison

$ diff -w name_list.txt name_list_new.txt
9. sort

Sort File Content in ascending order

$ sort names.txt

Sort File Content in descending order

$ sort -r names.txt

Sort/etc/passwd by the Third Field

$ sort -t: -k 3n /etc/passwd | more
10. export

Output environment variables matching the string oracle

$ export | grep ORCALEdeclare -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"

Set Global Environment Variables

$ export ORACLE_HOME=/u01/app/oracle/product/10.2.0
11. xargs

Copy all image files to an external drive

$ ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory

Compress and package all jpd files in the system

$ find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz

Download the page corresponding to all URLs listed in the file

$ cat url-list.txt | xargs wget –c
12. ls

Display the file size in a readable manner (MB, GB ...)

$ ls -lh-rw-r----- 1 ramesh team-dev 8.9M Jun 12 15:27 arch-linux.txt.gz

List objects in ascending order of the last modification time

$ ls -ltr

The file type is displayed after the file name.

$ ls -F
13. pwd

Output current working directory

14. cd

Cd-switch between two recently working Directories

Using shopt-s cdspell, you can set to automatically check the spelling of the cd command.

15. gzip

Create a *. gz compressed file

$ gzip test.txt

Decompress the *. gz File

$ gzip -d test.txt.gz

Show compression ratio

$ gzip -l *.gz     compressed        uncompressed  ratio uncompressed_name          23709               97975  75.8% asp-patch-rpms.txt
16. bzip2

Create a *. bz2 compressed file

$ bzip2 test.txt

Decompress the *. bz2 File

bzip2 -d test.txt.bz2
17. uzip

Decompress the *. zip file

$ unzip test.zip

View the content of the *. zip file

$ unzip -l jasper.zipArchive:  jasper.zipLength     Date   Time    Name--------    ----   ----    ----40995  11-30-98 23:50   META-INF/MANIFEST.MF32169  08-25-98 21:07   classes_15964  08-25-98 21:07   classes_names10542  08-25-98 21:07   classes_ncomp
18. shutdown

Shut down the system and shut down immediately

$ shutdown -h now

Shutdown in 10 minutes

$ shutdown -h +10

Restart

$ shutdown -r now

Force System check during restart

$ shutdown -Fr now
19. ftp

The usage of ftp commands and sftp commands is similar to that of connecting to the ftp server and downloading multiple files.

$ ftp IP/hostnameftp> mget *.html

Display the file list on the remote host

ftp> mls *.html -/ftptest/features.html/ftptest/index.html/ftptest/othertools.html/ftptest/samplereport.html/ftptest/usage.html
20. crontab

View the crontab entry of a user

$ crontab -u john -l

Set a scheduled task to be executed every ten minutes

*/10 * * * * /home/ramesh/check-disk-space
21. service

The service command is used to run the System V init script, which is usually located in the/etc/init. d file. This command can directly run the script in this folder without adding the path

View service status

$ service ssh status

View All service statuses

$ service --status-all

Restart service

$ service ssh restart
22. ps

The ps command is used to display information about a running process. The ps command has many options. Only a few

View all processes currently running

$ ps -ef | more

Displays the currently running processes in a tree structure. The H option indicates the process hierarchy.

$ ps -efH | more
23. free

This command is used to display the current memory usage of the system, including memory in use, available memory, and swap memory.

By default, free outputs the memory usage in bytes.

$ free             total       used       free     shared    buffers     cachedMem:       3566408    1580220    1986188          0     203988     902960-/+ buffers/cache:     473272    3093136Swap:      4000176          0    4000176

If you want to output memory usage in other units, you need to add an option:-g is GB,-m is MB,-k is KB,-B is byte

$ free -g             total       used       free     shared    buffers     cachedMem:             3          1          1          0          0          0-/+ buffers/cache:          0          2Swap:            3          0          3

If you want to view the summary of all the memory, use the-t option to add a summary row in the output.

$ free -t             total       used       free     shared    buffers     cachedMem:       3566408    1592148    1974260          0     204260     912556-/+ buffers/cache:     475332    3091076Swap:      4000176          0    4000176Total:     7566584    1592148    5974436
24. top

The top command displays the processes that occupy the most resources in the current system (sorted by CPU usage by default). If you want to change the sorting method, click O (uppercase letter O) in the result list) all columns that can be sorted are displayed. You can select the columns you want to sort.

Current Sort Field:  P  for window 1:DefSelect sort field via field letter, type any other key to return  a: PID        = Process Id              v: nDRT       = Dirty Pages count  d: UID        = User Id                 y: WCHAN      = Sleeping in Function  e: USER       = User Name               z: Flags      = Task Flags  ........

If you only want to display the process of a specific user, you can use the-u option.

$ top -u oracle
25. df

Displays the disk usage of the file system. By default, df-k outputs the disk usage in bytes.

$ df -kFilesystem           1K-blocks      Used Available Use% Mounted on/dev/sda1             29530400   3233104  24797232  12% //dev/sda2            120367992  50171596  64082060  44% /home

Use the-h option to display disk usage in a more readable manner

$ df -hFilesystem                  Size   Used  Avail Capacity  iused      ifree %iused  Mounted on/dev/disk0s2               232Gi   84Gi  148Gi    37% 21998562   38864868   36%   /devfs                      187Ki  187Ki    0Bi   100%      648          0  100%   /devmap -hosts                   0Bi    0Bi    0Bi   100%        0          0  100%   /netmap auto_home                0Bi    0Bi    0Bi   100%        0          0  100%   /home/dev/disk0s4               466Gi   45Gi  421Gi    10%   112774  440997174    0%   /Volumes/BOOTCAMP//app@izenesoft.cn/public  2.7Ti  1.3Ti  1.4Ti    48%        0 18446744073709551615    0%   /Volumes/public

Use the-T option to display the file system type

$ df -TFilesystem    Type   1K-blocks      Used Available Use% Mounted on/dev/sda1     ext4    29530400   3233120  24797216  12% //dev/sda2     ext4   120367992  50171596  64082060  44% /home
26. kill

Kill is used to terminate a process. Generally, we first use ps-ef to find a process to get its process number, and then use kill-9 process number to terminate the process. You can also use killall, pkill, and xkill to terminate the process.

$ ps -ef | grep vimramesh    7243  7222  9 22:43 pts/2    00:00:00 vim$ kill -9 7243
27. rm

Confirm before deleting the file

$ rm -i filename.txt

It is useful to use shell metacharacters in file names. Print and confirm the file name before deleting the file.

$ rm -i file*

Recursively delete all files in a folder and delete the folder

$ rm -r example
28. cp

Copy files 1 to 2, and retain the permissions, owner, and timestamp of the files.

$ cp -p file1 file2

Copy file1 to file2. If file2 exists, the system will prompt whether to overwrite

$ cp -i file1 file2
29. mv

Rename the file name file1 to file2. If file2 exists, the system prompts whether to overwrite

$ mv -i file1 file2

Note that if you use the-f option, no prompt will be prompted.

-V will output the rename process. This option is very convenient when the file name contains wildcards.

$ mv -v file1 file2
30. cat

You can view the content of multiple files at a time. The following command prints the content of file1 and then the content of file2.

$ cat file1 file2

-N command can add a line number before each line

$ cat -n /etc/logrotate.conf/var/log/btmp {missingok3        monthly4        create 0660 root utmp5        rotate 16 }
31. mount

To mount a file system, you must first create a directory and then mount the file system to this directory.

# mkdir /u01# mount /dev/sdb1 /u01

You can also add it to fstab for automatic mounting, so that the file system will be loaded whenever the system is restarted.

/dev/sdb1 /u01 ext2 defaults 0 2
32. chmod

Chmod is used to change the permissions of files and directories.

All permissions (including read, write, and execution) for the owner and group of the specified file)

$ chmod ug+rwx file.txt

Deletes all permissions of the group of the specified object.

$ chmod g-rwx file.txt

Modify directory permissions and recursively Modify permissions for all files and subdirectories under the Directory

$ chmod -R ug+rwx file.txt
33. chown

Chown is used to change the owner and group of a file.

Change the owner of a file to oracle and the group to db.

$ chown oracle:dba dbora.sh

Use the-R option to recursively modify files in directories and directories

$ chown -R oracle:dba /home/oracle
34. passwd

Passwd is used to change the password on the command line. Using this command requires you to enter the old password first, and then enter the new password.

$ passwd

Super Users can use this command to modify the passwords of other users. In this case, no user password is required.

# passwd USERNAME

Passwd can also delete the password of a user. This command can only be operated by the root user. After the password is deleted, the user can log on to the system without entering the password.

# passwd -d USERNAME
35. mkdir

Create a directory named temp under the home Directory

$ mkdir ~/temp

You can use the-p option to create a directory that does not exist on the path.

$ mkdir -p dir1/dir2/dir3/dir4/
36. ifconfig

Ifconfig is used to view and configure network interfaces for Linux systems

View all network interfaces and their statuses

$ ifconfig -a

Use the up and down commands to start or stop an Interface

$ ifconfig eth0 up$ ifconfig eth0 down
37. uname

Uname displays important system information, such as kernel name, host name, kernel version number, and processor type.

$ Uname-a Linux john-laptop 2.6.32-24-generic # 41-Ubuntu SMP Thu Aug 19 01:12:52 UTC 2010 i686 GNU/Linux

38. whereis

If you do not know the location of a command, you can use the whereis command. The following uses whereis to find the location of ls.

$ whereis lsls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz

If you want to find the location of an executable program, but the program is not in the default directory of whereis, you can use the-B option and specify the directory as the parameter of this option. The following Command finds the lsmk command in the/tmp directory

$ whereis -u -B /tmp -f lsmklsmk: /tmp/lsmk
39. whatis

Wathis: displays the description of a command.

$ whatis lsls        (1)  - list directory contents$ whatis ifconfigifconfig (8)         - configure a network interface
40. locate

The locate name can display the path of a specified file (or a group of files). It uses the database created by updatedb.

The following command displays all files in the system that contain the crontab string.

$ locate crontab/etc/anacrontab/etc/crontab/usr/bin/crontab/usr/share/doc/cron/examples/crontab2english.pl.gz/usr/share/man/man1/crontab.1.gz/usr/share/man/man5/anacrontab.5.gz/usr/share/man/man5/crontab.5.gz/usr/share/vim/vim72/syntax/crontab.vim
41. man

Displays the man page of a command.

$ man crontab

Some commands may have multiple man pages. Each man page corresponds to a command type.

$ man SECTION-NUMBER commandname

Man pages can be divided into eight command types

    1. USER commands
    2. System Call
    3. C-library functions
    4. Device and Network Interface
    5. File Format
    6. Game and Screensaver
    7. Environment, table, and macro
    8. System administrator commands and background running commands

For example, if we run whatis crontab, you can see that crontab has two command types: 1 and 5, so we can view the man page of command type 5 through the following command.

$ whatis crontabcrontab (1)          - maintain crontab files for individual users (V3)crontab (5)          - tables for driving cron$ man 5 crontab
42. tail

By default, the tail command displays the last 10 lines of text in the file.

$ tail filename.txt

You can use the-n option to specify the number of rows to be displayed.

$ tail -n N filename.txt

You can also use the-f option for real-time viewing. After this command is executed, it will wait. If a new line is added to the end of the file, it will continue to output new lines, this option is useful when you view logs. You can terminate command execution through CTRL-C

$ tail -f log-file
43. less

This name can display the file content without loading the entire file. This command is useful when you view large log files.

$ less huge-log-file.log

When you use the less command to open a file, the following two buttons will help you a lot. They are used to scroll forward and backward.

CTRL+F – forward one windowCTRL+B – backward one window
44. su
The su command is used to switch user accounts. A super user can use this command to switch to any other user without entering the password.
$ su - USERNAME

Run the ls command with another user name. In the following example, john runs the ls command with the user name of AJ. After the command is executed, john's account is returned.

[john@dev-server]$ su - raj -c 'ls'[john@dev-server]$

Log On with the specified user and use the specified shell program instead of the default

$ su -s 'SHELLNAME' USERNAME
45. mysql

Mysql may be the most widely used database in Linux. Even if you have not installed mysql on your server, you can use the mysql client to connect to a remote mysql server.

Enter a password to connect to a remote database.

$ mysql -u root -p -h 192.168.1.2

Connect to local database

$ mysql -u root -p

You can also enter the database password in the command line. You only need to add the password after-p as the parameter. You can directly write the password after p without spaces.

46. yum

Install apache using yum

$ yum install httpd

Update apache

$ yum update httpd

Uninstall/delete apache

$ yum remove httpd
47. rpm

Use rpm to install apache

# rpm -ivh httpd-2.2.3-22.0.1.el5.i386.rpm

Update apache

# rpm -uvh httpd-2.2.3-22.0.1.el5.i386.rpm

Uninstall/delete apache

# rpm -ev httpd
48. ping

Ping a remote host and send only five packets

$ ping -c 5 gmail.com
49. date

Set system date

# date -s "01/31/2010 23:59:53"

When you modify the system time, you need to synchronize the hardware time and system time.

# hwclock –systohc# hwclock --systohc –utc
50. wget

Use wget to download software, music, and video from the Internet

$ wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.1.tar.gz

Download the file and save it with the specified file name

$ wget -O taglist.zip http://www.vim.org/scripts/download_script.php?src_id=7701

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.