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:
mkdir -p content_archive/post
- create the archive directory and create parent directories if they don’t exist (-p
)cd content/post
- enter the blog post directorygrep -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 containspublished
andfalse
3.4*
- search all entries in the current directorycut -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 no1
xargs -i'{}' sh -c 'git mv {}.* ../../content_archive/post'
5.1-i'{}'
- replace{}
with each entry thatcut
prints 5.2sh -c
- execute following shell command 5.3git 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
.