Differences between revisions 4 and 7 (spanning 3 versions)
Revision 4 as of 2008-05-31 06:37:22
Size: 2166
Editor: pgas
Comment: add a note about quoting, still not sure about how to explain concisely and simply word splitting...
Revision 7 as of 2008-11-09 01:20:00
Size: 659
Editor: 86-124-78-002
Comment: Nice site! com website brown n bag recipe for pork tenderloin its http://arradn.servik.com/recipe6395.html fudgesicle recipe size, http://italy65.myd.net/recipe7628.html fun and easy snacks recipes,
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[Anchor(faq20)]]
== How can I find and deal with file names containing newlines, spaces or both? ==
The preferred method is still to use [:UsingFind:find(1)]:
Line 5: Line 2:
{{{
    find ... -exec command {} \;
}}}
---- /!\ '''Edit conflict - other version:''' ----
Nice site! com website brown n bag recipe for pork tenderloin its http://arradn.servik.com/recipe6395.html fudgesicle recipe size, http://italy65.myd.net/recipe7628.html fun and easy snacks recipes, Thank! Cool Site! The Best!
----
CategoryCategory
Line 9: Line 7:
or, if you need to handle filenames ''en masse'', with GNU and recent BSD tools: ---- /!\ '''Edit conflict - your version:''' ----
Nice site! com website brown n bag recipe for pork tenderloin its http://arradn.servik.com/recipe6395.html fudgesicle recipe size, http://italy65.myd.net/recipe7628.html fun and easy snacks recipes, Thank! Cool Site! The Best!
----
CategoryCategory
Line 11: Line 12:
{{{
    find ... -print0 | xargs -0 command
}}}

or with POSIX {{{find}}}:

{{{
    find ... -exec command {} +
}}}

Use that unless you really can't.

Another way to deal with files with spaces in their names is to use the shell's filename expansion (["globbing"]). This has the disadvantage of not working recursively (except with zsh's extensions), but if you just need to process all the files in a single directory, it works fantastically well, but you need to '''quote all your [:BashFAQ/073:Parameter Expansions] using double
quotes''' "$file" "${file%.mp3}" ...If you don't use the quotes the expansion will be split into several words, which means
that the command will think you gave it several arguments instead of just one.

This example changes all the *.mp3 files in the current directory to use underscores in their names instead of spaces. It uses [:BashFAQ/073:Parameter Expansions] that will not work in the original BourneShell, but should be good in Korn and Bash.

{{{
for file in *.mp3; do
    mv "$file" "${file// /_}"
done
}}}

You could do the same thing for all files (regardless of extension) by using

{{{
for file in *\ *; do
}}}

instead of *.mp3.

Another way to handle filenames recursively involes using the {{{-print0}}} option of {{{find}}} (a GNU/BSD extension), together with bash's {{{-d}}} option for read:

{{{
unset a i
while read -d $'\0' file; do
  a[i++]="$file" # or however you want to process each file
done < <(find /tmp -type f -print0)
}}}

The preceding example reads all the files under /tmp (recursively) into an array, even if they have newlines or other whitespace in their names, by forcing {{{read}}} to use the NUL byte (\0) as its word delimiter. Since NUL is not a valid byte in Unix filenames, this is the safest approach besides using {{{find -exec}}}.
---- /!\ '''End of edit conflict''' ----


/!\ Edit conflict - other version:


Nice site! com website brown n bag recipe for pork tenderloin its http://arradn.servik.com/recipe6395.html fudgesicle recipe size, http://italy65.myd.net/recipe7628.html fun and easy snacks recipes, Thank! Cool Site! The Best!


CategoryCategory


/!\ Edit conflict - your version:


Nice site! com website brown n bag recipe for pork tenderloin its http://arradn.servik.com/recipe6395.html fudgesicle recipe size, http://italy65.myd.net/recipe7628.html fun and easy snacks recipes, Thank! Cool Site! The Best!


CategoryCategory


/!\ End of edit conflict


BashFAQ/020 (last edited 2024-05-06 09:19:34 by StephaneChazelas)