Use xarg with wildcards

Use xarg with wildcards

Note to myself: Using xargs with wildcards needs to be wrapped in a shell command.

When trying to move all unpublished blog posts and their thumbnails to an archive folder I used the following commands:

mkdir -p content_archive/post
cd content/post
grep -l -i "published.*false" * | cut -d'.' -f1 | xargs -i'{}' sh -c 'git mv {}.* ../../content_archive/post'

Explanation:

  1. mkdir -p content_archive/post - create the archive directory and create parent directories if they don’t exist (-p)
  2. cd content/post - enter the blog post directory
  3. grep -l -i "published.*false" * - get a list of unpublished filenames 3.1 -l - only print filenames, no content 3.2 -i - search case-insensitive 3.3 "published.*false" - regex to search a line that contains published and false 3.4 * - search all entries in the current directory
  4. cut -d'.' -f1 - remove file extension as we also want to move thumbnails 4.1 -d'.' - use . as separator for cutting 4.2 -f1 - only print field no 1
  5. xargs -i'{}' sh -c 'git mv {}.* ../../content_archive/post' 5.1 -i'{}' - replace {} with each entry that cut prints 5.2 sh -c - execute following shell command 5.3 git mv {}.* ../../content_archive/post - move files with various extensions to archive directory

Thanks to user Scott from https://superuser.com/a/519019 the hint with sh -c.