12/7/2023 0 Comments Bash find file name spaces![]() ![]() Note the require actual newline between IFS=' and ' in order to assign a newline, given that the $'\n' syntax is not available. supports neither arrays nor process substitution (nor readarray, nor any read options other than -r) you'd have to implement line-by-line processing as follows: while IFS=' instead, read would run in a subshell, which means that the variables it creates will disappear (go out of scope) when the command returns and cannot be used later. To safely expand an array to its elements without any interpretation of its elements, double-quote it: substitution rather than a pipeline is used so as to ensure that readarray / read is executed in the current shell rather than in a subshell.Īs eckes points out in a comment, if you were to try find.While this could also be addressed with a shell option, you now have 2 settings you need to perform ahead of time and restore to their original value thus, using readarray or read as demonstrated above is the simpler choice.Įven if you did manage to collect the file paths correctly in $vari as an array, referencing that array as - without double quotes - would break, because the resulting strings are again subject to word splitting, and also pathname expansion (globbing).While this could be addressed with IFS=$'\n' so as to only split at line boundaries, the resulting tokens are still subject to pathname expansion (globbing), which can inadvertently alter the file paths. ![]() Vari=( `find -name "multi word file.xml"` ),īut that would perform word splitting on the find's output and not properly preserve filenames with spaces. That applies for all the operations, whether you are changing directory with ‘ cd ‘ or trying to access files with ‘ cat ‘ commands. By enclosing the command substitution in (. Why Filename with Spaces is not Recognized by Bash In Bash, if you type multiple words without escape character (\) or quotations, it will consider all the words as arguments.Vari=`find -name "multi word file.xml"` (I've removed the spaces around =, which would result in a syntax error) does not create an array such a command substitution returns the stdout output from the enclosed command as a single string (with trailing newlines stripped). # If your `find` implementation doesn't support `-delete`:įind. Otherwise, let find perform the deletion directly (these solutions also handle filenames with embedded newlines correctly):įind.# won't be handled correctly, but that's rarely a concern. The only caveat is that filenames with *embedded* newlines type d do echo file done If I have just one directory called My Directory, the output is My Directory How do I fix this The echo file is just temporary. # Read matches into array `vari` - safely: no word splitting, no 5 This question already has answers here : Looping through files with spaces in the names duplicate (9 answers) Closed 8 years ago. If you really need to capture all file paths in an array up front (assumes bash, primarily due to use of arrays and process substitution ( ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |