These commands didn't work because wildcard patterns omit dot files (files whose name begins with the character .
) unless the dot appears explicitly in the pattern. So *.un~
matches yourfile.txt.un~
but not .myfile.txt.un~
, whereas .*.un~
does match .myfile.txt.un~
.
You should be able to use find(1)
for this (find
wildcard matching doesn't treat dot files specially):
find / -name "*.un~" -not -path "~/.tmp/*" -delete
That tells find to search /
for all files matching *.un~
that aren't in ~/.tmp
and delete them. If you take off -delete
it will just output a list, so you can check and make sure it's not going to delete the wrong things. You also might want to throw -mount
in there to stop it from searching other filesystems you have mounted
If I understand correctly, you want to fire up one instance flac … | lame …
for each input line, and interpolate the input into the arguments to both commands.
Since you need xargs
to start a pipeline, you need to make it start a program that's capable of creating pipelines, i.e. a shell.
inotifywait -m -r -q -e moved_to --format "%w%f" ~/test |
xargs -l sh -c 'flac -cd "$0" - | lame -b 320 - "/media/1tb/$0.mp3"'
Alternatively, have the calling shell read lines one by one and run the pipeline.
inotifywait -m -r -q -e moved_to --format "%w%f" ~/test |
while IFS= read -r file; do
flac -cd "$file" - | lame -b 320 - "/media/1tb/$file.mp3"
done
Note that the format %w%f
produces an absolute path, to which you're prepending /media/1tb
and appending .mp3
. If you want to strip off the directory part of the file in the lame
command, change $file
to ${file##*/}
. If you want to strip off the extension, change $file
to ${file%.*}
. If you want to do both, you'll have to do it in two steps. If you want to reproduce the directory hierarchy under /media/1tb
, you can use mkdir -p
.
cd ~/test
inotifywait -m -r -q -e moved_to --format "%w%f" . |
while IFS= read -r file; do
[ -f "$file" ] || continue; # skip directories and other special files
dir=${file%/*}; file=${file##*/}
mkdir -p "/media/1tb/$dir"
flac -cd "$dir/$file" - | lame -b 320 - "/media/1tb/$dir/${file#.*}.mp3"
done
Best Answer
No need for any fancy stuff. Simply escape the
?
so that it's not considered part of the glob:This works for
!
too:Or in one fell swoop:
Update
Just noticed that you were suggesting to
grep
the output ofls
. I wanted to bring your attention to the fact that you shouldn't parse the output of ls