Summary of common Linux Shell commands

Source: Internet
Author: User
Tags linux shell commands cron script

1. find
Find pathname-options [-print-exec-OK]
Let's take a look at the parameters of this command:
Pathname find command to find the directory path. For example, use "." To represent the current directory, and use "/" to represent the root directory of the system.
-The print find command outputs matching files to the standard output.
-Execute the exec find command to execute the shell command given by this parameter on the matching file. The corresponding command is in the form of 'command' {}\;. Note the space between {} And \;, and there is no space between the two,
Note that there must be a semicolon ending.
0)-OK and-exec play the same role, but execute the shell command given by this parameter in a safer mode. A prompt is displayed before each command is executed, let the user determine whether to execute
Find. -name "datafile"-ctime-1-exec ls-l {} \; find the file named datafile *, create the files that are actually within one day, and then display their details.
Find.-name "datafile"-ctime-1-exec rm-f {}\; find the file named datafile *, create the files within one day, and delete them.

Find. -name "datafile"-ctime-1-OK ls-l {}\; the only difference between the two examples and above is that-OK will prompt the user when each file is executed, more secure.
Find.-name "datafile"-ctime-1-OK rm-f {}\;

1) find.-name is based on the file name, but the file name is case sensitive.
Find.-name "datafile *"

2) find.-iname is based on the file name, but the file name is case insensitive.
Find.-iname "datafile *"

3) find.-maxdepth 2-name fred finds out that the file name is fred, where the depth of the find search directory is 2 (from the current directory), where the current directory is regarded as the first layer.

4) find.-perm 644-maxdepth 3-name "datafile *" (indicates a file with a permission of 644, a directory with a depth of 3 and a name of datafile)

5) find.-path "./rw"-prune-o-name "datafile *" lists all files whose names are not./rw and Their subdirectories are datafile.
Find.-path "./dir *" lists all directories that match the dir * and their directories.
Find. \ (-path ". /d1 "-o-path ". /d2 "\)-prune-o-name" datafile * "lists all non. /d1, d2, and their subdirectories are named datafile.

6) find.-user ydev: find all files whose owner is ydev.
Find .! -User ydev: Find all files whose owner is not ydev. Note that! And-user.

7) find.-nouser: find all files without a primary user. In other words, the primary user may have been deleted.

8) find.-group ydev to find all files whose owner user group is ydev.

9) find.-nogroup: find all files that do not belong to the main user group. In other words, the main user group may have been deleted.

10) find.-mtime-3 [+ 3] find the file that modified the data within 3 days [outside.
Find.-mmin-3 [+ 3] find the file that modified data within 3 minutes [out.
Find.-atime-3 [+ 3]: find the file that was accessed within 3 days [outside.
Find.-amin-3 [+ 3] find the file that was accessed within 3 minutes [out.
Find.-ctime-3 [+ 3]: find the file whose modification status is within 3 days [outside.
Find.-cmin-3 [+ 3] find the file that modified the state within 3 minutes [out.

11) find.-newer eldest_file! -Newer newest_file: Find out the file change time (between eldest_file and newest_file.
Find.-newer file: find all files updated later than the change time of file.
Find .! -Newer file: Find all files older than the file change time.

12) find.-type d. find the file whose file type is directory.
Find .! -Type d: Find a non-directory file.
B-block device files.
D-directory.
C-character device file.
P-MPs queue file.
L-Symbolic Link file.
F-common file.

13) find. -size [+/-] 100 [c/k/M/G] indicates that the file length is equal to [greater than/less than] 100 [Bytes/k/M/G] file.

14) find.-empty: find All empty files or empty directories.

15) find.-type f | xargs grep "ABC"
The difference between xargs and-exec is that-exec may start a new process to execute the-exec operation for each searched file, while xargs is completed in a process, higher efficiency.

2. crontab:
The file format is as follows (each column is separated by spaces ):
1st columns minute 1 ~ 59
2nd columns hour 1 ~ 23 (0 indicates midnight)
1-3rd ~ 31
4th columns Month 1 ~ 12
5th columns in a week 0 ~ 6 (0 indicates Sunday)
6th columns of commands to run

Command to be run by hour, day, month, and week

30 21 ***/apps/bin/cleanup. sh
The preceding example shows cleanup. sh in the/apps/bin directory at every night.
45 4, 10, 22 **/apps/bin/backup. sh
The example above indicates the backup. sh in the/apps/bin directory at on May 1, 10, and 22.
10 1 ** 6, 0/bin/find-name "core"-exec rm {}\;
The preceding example shows that a find command is run every Saturday and Sunday.
0, 30 18-23 ***/apps/bin/dbcheck. sh
The preceding example shows running dbcheck. sh in the/apps/bin directory every 30 minutes between and every day.
0 23 ** 6/apps/bin/qtrend. sh
The preceding example shows qtrend. sh in the/apps/bin directory run at every Saturday.

-U user name.
-E: edit the crontab file.
-L list the content in the crontab file.
-R: Delete the crontab file.
The system automatically saves the cron script named <username> in the/var/spool/cron/directory.
Cron is a scheduled task. When a task is started, it generally restarts a new SHELL. Therefore, when you need to use the information in the login configuration file, especially the environment variable, it is very troublesome.
Generally, this problem can be solved as follows:
0 2 *** (su-USERNAME-c "export LANG = en_US;/home/oracle/yb2.5.1/apps/admin/1.sh";)>/tmp/1.log 2> & 1
If you want to execute multiple statements, use semicolons to separate them. Note: The preceding statements must be executed under the root account.

3. nohup:
Nohup command &
If you are running a process and you think the process will not end when you exit the account, you can use the nohup command. This command can continue running the corresponding process after you exit the account.
Nohup means no hang up ).

4. cut:
1) The general cut format is: cut [options] file1 file2
-C list specifies the number of characters to cut.
-F field specifies the number of cut fields.
-D specifies the domain separator different from the space and tab key.
-C is used to specify the cut range, as shown below:
-C1, 5-7 Cut 1st characters, then 5th to 7th characters.
-C2-cut 2nd to the last character
-The C-5 cut from the beginning to 5th characters
-C1-50 cut the first 50 characters.
-F format is the same as-c format.
-F1, 5 cut 1st domain, 5th domain.
-F1, 10-12 cut the 1st domain, 10th domain to 12th domain.
2) usage:
Cut-d:-f3 cut_test.txt (based on ":" As the separator, and data in field 3 is returned at the same time) * field starts from 0.
Cut-d:-f1, 3 cut_test.txt (based on ":" As the separator, data in both field 1 and 3 is returned)
Cut-d:-c1, 5-10 cut_test.txt (returns the 1st and 5-10 characters)

5. sort:
1) sort the file content. The default Delimiter is space. If you want to customize the delimiter, select-t, such as-t:
2) After the Delimiter is used, the first field is 0, and the awk is 1.
3) The usage is as follows:
Sort-t: sort_test.txt (sorting based on the first field by default, the delimiter between fields is ":")
Sort-t:-r sort_test.txt (the first field is sorted in reverse order by default, and the delimiter between fields is ":")
Sort-t: + 1 sort_test.txt (sort based on the second field. The separator between fields is ":")
Sort + 3n sort_test.txt (sort based on the third field, where the n option prompts "numeric" sorting)
Sort -usort_test.txt (removes duplicate rows in the file and sorts the rows based on the entire row)
Sort-o output_file-t: + 1.2 [n] sort_text.txt (based on the second field and starts from the second character of the field. Here, n is also used for "numeric" sorting, output the result to output_file)
Sort-t:-m + 0 filename1 filename2 (merge two files and sort them based on the first field)

6. pgrep and pkill:

Find and kill the specified process. Their options and parameters are identical. Here we only introduce pgrep.
/> Sleep 100 &
1000
/> Sleep 100 &
1001

/> Pgrep sleep
1000
1001
/> Pgrep-d: sleep #-d defines the delimiter between multiple processes. If not, use newline.
1000:1001
/> Pgrep-n sleep #-n indicates that if the program has multiple processes, find the latest one.
1001
/> Pgrep-osleep #-o indicates that if the program has multiple processes, find the oldest one.
1000
/> Pgrep-G root, oracle sleep #-G indicates that the process's group id is considered in the group list after-G.
1000
1001
/> Pgrep-u root, oracle sleep #-u indicates that the process's effetive user id is considered in the group list after-u.
1000
1001
/> Pgrep-U root, oracle sleep #-U indicates that the process's real user id is considered in the group list after-u.
1000
1001
/> Pgrep-x sleep #-x indicates that the process name must be completely matched. The above examples can be partially matched.
1000
1001
/> Pgrep-x lupus

/> Pgrep-l sleep #-l not only prints the pid, but also prints the process name
1000 sleep
1001 sleep
/> Pgrep-lf sleep #-f is generally used with-l to print the process parameters
1000 sleep 100
1001 sleep 100

/> Pgrep-f sleep-d, | xargs ps-fp
UID PIDPPIDC STIME TTY TIME CMD
Root 100021380 00:00:00 pts/5 sleep 1000
Root 100121380 00:00:00 pts/5 sleep 1000

7. fuser:
Fuser-m/dev # list all processes that are infected with the/dev device.
Fuser testfile # list the processes that are infected with testfile.
Fuser-u testfile # list the processes pid and userid that are infected with testfile
Fuser-k testfile # Kill the pid of the process that is infected with testfile


8. mount:

How to mount a shared directory under windows under unix
Mount-t smbfs-o username = USERNAME, password = PASSWORD // windowsIp/pub_directory/mountpoint
/> Mkdir-p/mnt/win32
/> Mount-o username = administrator, password = 1234 // 10.1.4.103/Mine/mnt/win32
/> Umount/mnt/win32 # uninstall the mount.


9. netstat:

-A indicates that all statuses are displayed.
-L only displays the listen status. The default value is connected.
-P: display the application name
-N: displays ip, port, and user information.
-T: Only TCP connections are displayed.
/> Netstat-apnt
/> Netstat-lpnt # Use this command to only display the status of the listening port


10. tune2fs:

Tools used to adjust ext2/ext3 file system features

-L View File System Information
/> Tune2fs-l/dev/sda1 # lists all data information related to the disk partition, such as Inode.
/> Tune2fs-l/dev/sda1 | grep-I "block size" # view the size of the current File System
/> Tune2fs-l/dev/sdb1 | grep-I "mount count" # view mount count


11. enable or disable the Linux (iptables) Firewall
It takes effect permanently after restart:
/> Chkconfig iptables on # enable
/> Chkconfig iptables off # disable

Takes effect immediately. After restart, restore:
/> Service iptables start # enable
/> Service iptables stop # disable


12. tar volume compression and merge
Take MB per volume as an Example
/> Tar cvzpf-somedir | split-d-B 500 m # tar volume Compression
/> Cat x *> mytarfile.tar.gz # tar multi-volume merge



13. save man or info information as a text file
/> Man tcsh | col-B> tcsh.txt
/> Info tcsh-o tcsh.txt-s


14. view the number of threads in the running process
/> Ps-eo "args nlwp pid pcpu"


15. Use md5sum to calculate the md5 of the file
/> Md5sum test. c
07af691360175a6808567e2b08a11724test. c

/> Md5sum test. c> hashfile
/> Md5sum-c hashfile # verify whether the md5 value contained in the hashfile matches the corresponding file during command execution. If test. c is modified. This command returns an mismatched warning.


16. display the complete command line parameters of the process in the ps command.
/> Ps auwwx

17. chkconfig:

1) edit the Shell file header of the chkconfig operation.
#! /Bin/bash
#
# Chkconfig: 2345 20 80
# Description: Starts and stops the Redis Server
This annotation header is very important, otherwise the chkconfig command cannot be recognized. Among them, 2345 indicates the init startup level, that is, the service is started in the 4 levels of 2, 3, 4, and 5. 20 indicates the start priority of the script, and 80 indicates the stop priority. You can find more detailed descriptions in the manpage of chkconfig.

2) Compile the content of the Shell file:
Case "$1" in
Start)
# TODO: Execute the startup logic of the service program.
;;
Stop)
# TODO: Execute the stop logic of the service program.
;;
Restart)
;;
Reload)
;;
Condrestart)
;;
Status)
;;
The case conditions listed above are indispensable. If they do not exist, place them as placeholders, as shown in the preceding example.

3). add and delete service programs:
# -- Add indicates adding a new service program.
/> Chkconfig -- add redis_6379
# Check whether deletion or addition is successful
/> Chkconfig | grep redis_6379
Redis_6379 0: off 1: off 2: on 3: on 4: on 5: on 6: off
# -- Del indicates to delete an existing service program.
/> Chkconfig -- del redis_6379

Related Article

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.