


Don't use pdf=$(.) on null separated inputs.the filter all lines after %comment including %comment itself.output each filename with file= on the beginning.filter all lines before the %comment and remove the line %comment itself from the file.

assuming output file name is stored in variable "$out".That applies for all the operations, whether you are changing directory with ‘ cd ‘ or trying to access files with ‘ cat ‘ commands. You can adjust pattern and sed as needed. 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. If you are looking for a filter for leading space(s) use -name ' ', or for any space than -name ' '. Minor note: the pattern -name ' 'looks for file with a trailing space(s). # I don't know what you expect to do with newlines in filenames, but I guess you don't have thoseĬat "$tmp" | sed -z 's/^/file=/' | sed 's/\x0/\n/g' You can use bash substitution to extract the file name, and eliminate leading spaces. Just that I couldn't make stat output fractional part of creation date.Īs for the second part, we need to save the null delimetered list to a file find downloads. You could do xargs -0 stat -printf "%Y %n\0" which would basically move your script forward, as ls isn't meant to be used for scripts. We could also do reverse sort and use head, no difference. Get the count of the newest files, in this example 3 newest files, by using tail.Remove the first field from the stream, ie.Sort the null separated stream using space as field separator using only first field using numerical sort.for each file print it's modifition date %T with the format specifier that means seconds since epoch with fractional part, then print space, filename and separate with \0.The PATH is the folder 2 that I have my pdfs moved to.įind downloads -name "*.pdf" -printf %p\0" | Sorry for being vague on the third point as I thought I might be able to figure that out after achieving the first and second goals.īasically, it is a text file that have a line start with "%comment" near the end and I will need to insert the filenames before that line in the format "file=PATH".
BASH FIND FILE NAME SPACES HOW TO
I am not sure how to avoid these words in the same filename being treated separately. What I get from the above array will have "with" and "Space" in separate array entries. Suppose a file has name "Filename with Space". However, the following array cannot handle files with spaces in filename: pdfs=($(find -name "$DOWNLOADS/*.pdf" -print0 | xargs -0 ls -1 -t | head -n$NUM)) I tried to make an array of filenames and then move them and do text output in a loop. (In my actual usage, I will use sed to put the file names in a specific position of an existing file.)
BASH FIND FILE NAME SPACES PDF
I need to write a Bash script that achieve the following goals:ġ) move the newest n pdf files from folder 1 to folder 2 Ģ) correctly handles files that could have spaces in file names ģ) output each file name in a specific position in a text file.
