使用xargs运行多个命令-for循环

时间:2020-05-15 20:19:10

标签: bash find xargs

基于Running multiple commands with xargs中的最高答案,我试图使用find / xargs处理更多文件。为什么第一个文件1.txt在for循环中丢失?

$ ls
1.txt  2.txt  3.txt

$ find . -name "*.txt" -print0 | xargs -0
./1.txt ./2.txt ./3.txt

$ find . -name "*.txt" -print0 | xargs -0 sh -c 'for arg do echo "$arg"; done'
./2.txt
./3.txt

2 个答案:

答案 0 :(得分:0)

您为什么坚持使用class BrandsIndexView(generic.ListView): template_name = "brands/index.html" context_object_name = "latest_brands_list" def get_queryset(self): return Brands.objects.filter(created_on__lte=timezone.now()).order_by('-created_on')[:10] model for category class Category(models.Model): category_name = models.CharField(null=False,max_length=200) category_slug = models.SlugField( max_length=200, db_index=True, unique=True) category_image = models.FileField(upload_to="brand_cat_images",null=True,blank=True,validators=[file_ext]) class Brands(models.Model): brand_name = models.CharField(max_length=200 , null=False,help_text="Enter Brand Name") brand_slug = models.SlugField(max_length=200 , unique=True) brand_logo = models.ImageField(null=False, blank=False, upload_to="brand_logo") brand_description = models.TextField(max_length=250 ,null=True,blank=True) brand_category = models.ForeignKey(Category,on_delete=models.CASCADE ,default="None",related_name="category") ?您也可以执行以下操作。

xargs

由于这是在同一shell中执行的,因此可以在循环中更改变量。否则,您将获得一个无法使用的子外壳。

答案 1 :(得分:0)

当您在脚本example.sh中使用for循环时,调用example.sh var1 var2 var3会将var1放在第一个参数中,而不是example.sh中。
如果要为每个命令处理一个文件,请使用xargs选项-L

find . -name "*.txt" -print0 | xargs -0 -L1 sh -c 'echo "$0"'
# or for a simple case
find . -name "*.txt" -print0 | xargs -0 -L1 echo