Differences between revisions 3 and 5 (spanning 2 versions)
Revision 3 as of 2008-05-15 18:40:20
Size: 1874
Editor: GreyCat
Comment: #faq73 -> /073
Revision 5 as of 2008-05-31 12:32:46
Size: 2160
Editor: GreyCat
Comment: clean up
Deletions are marked like this. Additions are marked like this.
Line 25: Line 25:
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. 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 or POSIX shell, but should be good in KornShell and [:BASH:].
Line 32: Line 32:
Remember, you need to '''quote all your [:BashFAQ/073:Parameter Expansions] using double
quotes'''. If you don't, the expansion will undergo WordSplitting (see also BashGuide/TheBasics/ArgumentSplitting and BashPitfalls).
Line 33: Line 35:
You could do the same thing for all files (regardless of extension) by using You could do the same thing for all files with spaces in their names (regardless of extension) by using
Line 41: Line 43:
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: Another way to handle filenames recursively involves using the {{{-print0}}} option of {{{find}}} (a GNU/BSD extension), together with bash's {{{-d}}} option for read:
Line 44: Line 46:
# Bash
Line 50: Line 53:
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}}}. 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 line delimiter. Since NUL is not a valid byte in Unix filenames, this is the safest approach besides using {{{find -exec}}}.

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)]:

    find ... -exec command {} \;

or, if you need to handle filenames en masse, with GNU and recent BSD tools:

    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.

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 or POSIX shell, but should be good in KornShell and [:BASH:].

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

Remember, you need to quote all your [:BashFAQ/073:Parameter Expansions] using double quotes. If you don't, the expansion will undergo WordSplitting (see also BashGuide/TheBasics/ArgumentSplitting and BashPitfalls).

You could do the same thing for all files with spaces in their names (regardless of extension) by using

for file in *\ *; do

instead of *.mp3.

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

# Bash
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 line delimiter. Since NUL is not a valid byte in Unix filenames, this is the safest approach besides using find -exec.

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