The linux command argument list is too long and the find usage

Source: Internet
Author: User

1. When deleting a large batch of files in a directory, when using RM-RF or RM * will prompt the parameter list too long by modifying the command to: Find. -name "*" | Xargs rm-rf ' * ', successfully deleted 2, need to put a directory of all files mv to another directory, if the current directory file is particularly many, simple use of the MV Source directory directory will prompt the parameter list too long by modifying the command as: LS | Xargs-t-I {} mv {}... /matchres/can move all files in the current directory to the ".. /matchres "Under 3, another CP command will also exist this situation, the solution is the same as the MV."

The difference between Find-print and-print0:

-print adds a carriage return line character after each output, and-print0 does not.

Examples of find common usages in Linux

Find Path-option [-print] [-exec-ok command] {} \;

Parameters for the Find command:

The directory path that the Pathname:find command looks for. For example, use. To represent the current directory, and/to represent the system root directory.
The-print:find command outputs the matched file to standard output.
The-exec:find command executes the shell command given by the parameter to the matching file. The corresponding command is in the form of ' command ' {} \;, note the space between {} and \;
-ok: The same as-exec, except that the shell command given by the parameter is executed in a more secure mode, prompting the user to determine whether to execute before executing each command.

#-print output of the found file to standard output
#-exec command {} \; -the command operation for the file to be found, with spaces between {} and \;
#-ok and-exec the same, but before the operation to consult the user-mtime-n +n #按文件更改时间来查找文件,-n means n days or less, +n refers to n days ago

Xargs
most Linux commands produce output: A list of files, a list of strings, and so on. But what if you want to use one of the other commands and take the output of the previous command as a parameter? For example, the file command displays the types of files (executables, ASCII text, and so on); You can process the output so that it displays only the file names, and now you want to pass these names to the Ls-l command to see the timestamp. The Xargs command is used to do the work. It allows you to perform some other commands on the output. Remember the following syntax from the 1th part:

File-lz * | grep ASCII | Cut-d ":"-F1 | Xargs ls-ltr

Let's dissect this command string. The first, File-lz *, is used to find a file that is a symbolic link or a compressed one. It passes the output to the next command, grep ASCII, where the command searches for an "ASCII" string and produces the output shown below:
Alert_DBA102.log:ASCII 中文版 Text
Alert_dba102.log.z:ascii text (compress ' d data, bits)
Dba102_asmb_12307.trc. Z:ascii 中文版 text (compress ' d data-bits)
Dba102_asmb_20653.trc. Z:ascii 中文版 text (compress ' d data-bits)

Since we are only interested in file names, we apply the next command cut-d ":"-f1, showing only the first field:
Alert_dba102.log
Alert_dba102.log.z
Dba102_asmb_12307.trc. Z
Dba102_asmb_20653.trc. Z

Now, we want to use the LS-L command to pass the above list as parameters and pass one at a time. The Xargs command allows you to do so. The last part, Xargs ls-ltr, is used to receive the output and execute the ls-ltr command on it, as follows:

Ls-ltr Alert_dba102.log
Ls-ltr alert_dba102.log.z
Ls-ltr DBA102_ASMB_12307.TRC. Z
Ls-ltr DBA102_ASMB_20653.TRC. Z

Therefore, Xargs itself is not of much use, but it is very powerful when combined with other commands.

Here is another example where we want to calculate the number of rows in these files:

$ file * | grep ASCII | Cut-d ":"-F1 | Xargs wc-l
47853 Alert_dba102.log
Dba102_cjq0_14493.trc
29053 DBA102_MMNL_14497.TRC
154 DBA102_RECO_14491.TRC
Dba102_rvwr_14518.trc
77122 Total

(Note: The above tasks can also be used to complete the following command:)

$ Wc-l ' file * | grep ASCII | Cut-d ":"-F1 | grep ASCII | Cut-d ":"-f1 '

The Xargs version is used to illustrate concepts. Linux can accomplish the same task in several ways, using the method that best suits your situation.

With this method, you can quickly rename files in a directory.

$ ls | XARGS-T-i mv {} {}.bak

The-i option tells Xargs to replace {} with the name of each item. The-t option instructs Xargs to print the command before executing.

Another very useful operation is when you use VI to open the file you want to edit:

$ file * | grep ASCII | Cut-d ":"-F1 | Xargs VI

This command uses VI to open the file one by one. This command is handy when you want to search for multiple files and open them for editing.

It also has several options. The most useful may be the-p option, which makes the operation interoperable:

$ file * | grep ASCII | Cut-d ":"-F1 | Xargs-p VI
VI alert_dba102.log dba102_cjq0_14493.trc DBA102_MMNL_14497.TRC dba102_reco_14491.trc dba102_rvwr_14518.trc?...

The xarg here requires you to confirm before running each command. If you press "Y", the command is executed. You will find this option useful when you are doing something that might be corrupted and unrecoverable, such as deleting or overwriting it.

The-t option uses a verbose mode, which displays the command to run, which is a very helpful option during debugging.

What if the output passed to Xargs is empty? Consider the following command:

$ file * | grep ssssss | Cut-d ":"-F1 | Xargs-t wc-l
Wc-l
0
$

Here, there is no matching content after searching for "ssssss", so the input to Xargs is empty, as shown in the second line (as a result of the verbose option we use-t). While this may help, in some cases you may want to stop xargs if you do not have content to work with, and if so, you can use the-r option:
$ file * | grep ssssss | Cut-d ":"-F1 | Xargs-t-R Wc-l
$

If there is no content to run, the command exits.

Suppose you want to delete a file by using the RM command, which will be a parameter to the Xargs command. However, RM can only accept a limited number of parameters. What if your parameter list exceeds this limit? The-n option of Xargs limits the number of arguments to a single command line.

The following shows how to limit the use of only two parameters per command line: Even if you pass five files to Xargs ls-ltr, only two files are passed to ls-ltr at a time.

$ file * | grep ASCII | Cut-d ":"-F1 | Xargs-t-n2 ls-ltr
Ls-ltr Alert_dba102.log DBA102_CJQ0_14493.TRC
-RW-R-----1 Oracle DBA 738 19:18 DBA102_CJQ0_14493.TRC
-rw-r--r--1 Oracle DBA 2410225 05:31 Alert_dba102.log
Ls-ltr DBA102_MMNL_14497.TRC DBA102_RECO_14491.TRC
-RW-R-----1 Oracle DBA 5386163 17:55 DBA102_MMNL_14497.TRC
-RW-R-----1 Oracle DBA 6808 05:21 DBA102_RECO_14491.TRC
Ls-ltr DBA102_RVWR_14518.TRC
-RW-R-----1 Oracle DBA 2087 04:30 DBA102_RVWR_14518.TRC

With this method, you can quickly rename files in a directory.

$ ls | XARGS-T-i mv {} {}.bak

The-i option tells Xargs to replace {} with the name of each item.


I need to change a project with. SVN management to be managed by CVS, so before I import the entire project into CVS, I need to delete the. SVN directory and subdirectories and files under the. SVN directory in all directories and in the Descendants directory. I've been looking for the simplest way, and finally wrote a program to do it. As a result, my colleague told me that I needed only one instruction:find-name '. SVN ' |xargs RM-RF
This lesson is very heavy, I hated a fool for a lifetime, but at that moment found oneself is the biggest fool!

Yes, although we--at least I--use Linux every day, but is it really efficient to use this system? No, there are times when you don't take the time to study it, use some inherently foolish ways and think patterns to use this system.

1. Create a multilevel directory:

The MKIDR instruction is used to create a directory, in fact, we have always done so. But we didn't look at it when we were using it. What additional features are provided by those parameters?

Mkidr-p/share/dragon; Create the share directory under the root directory and create the Dragon directory under the/share directory. mkdir-p Guicmd/{bin,lib,src,share/version,doc/{html,pdf,info,man}}
; Used to create a complex project directory tree.

2.find with Xargs use:

Find-name '. SVN ' |xargs rm-rf; this goes without saying, as I mentioned earlier.
Xargs is much more like a filter, and it is an extremely efficient way to dispose of the contents of the filenames that are passed by the pipeline.

The linux command argument list is too long and the find usage

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.