Modifying replace string in xargs

前端 未结 8 1539
梦毁少年i
梦毁少年i 2021-01-30 01:43

When I am using xargs sometimes I do not need to explicitly use the replacing string:

find . -name \"*.txt\" | xargs rm -rf

In oth

8条回答
  •  攒了一身酷
    2021-01-30 02:18

    In cases like this, a while loop would be more readable:

    find . -name "*.txt" | while IFS= read -r pathname; do
        base=$(basename "$pathname"); name=${base%.*}; ext=${base##*.}
        mv "$pathname" "foo/${name}.bar.${ext}"
    done
    

    Note that you may find files with the same name in different subdirectories. Are you OK with duplicates being over-written by mv?

提交回复
热议问题