Alphabetical subfolders and extract data from the file
Solved
billyV33
Posted messages
24
Status
Member
-
lEprofSonDkon Posted messages 227 Status Member -
lEprofSonDkon Posted messages 227 Status Member -
Hello,
I have a big problem! I want to sort the names of the subdirectories in a parent directory in alphabetical order so I can finally read each file located in each subdirectory to extract just the first 3 bytes of each file.
I can retrieve the first 3 bytes of each file using find and head:
$ find . -type f -exec head -c3 {} \; | sort
My problem is that before doing a head, I need to sort the names of the subdirectories.
$ find . -type d -print | sort
I can display the names of the subdirectories in alphabetical order, but I can't go down to the files by looping to extract the 3 bytes with head… WHY!!!
find . -type f -name "*" -print | cut -d/ -f2 | sort | head -c3
I've tried many possible combinations but without success.
find . -type f -name "*" -print | cut -d/ -f2 | sort | -exec head -c3 {} \;
Here, head only shows the 3 bytes of the first file… I’ve tried with xargs, -exec {} \;… nothing works…
find . -type f -name "*" -print | sort | head -c3
I've been trying to find a way to do this for almost a week, but I really don’t understand!
Finally, I wanted to sort the names of the subdirectories in alphabetical order and gradually extract the 3 bytes from each file contained within.
Not sure I’m being clear, but well…
Thank you for helping me!!!
Configuration: Linux / Firefox 60.0
I have a big problem! I want to sort the names of the subdirectories in a parent directory in alphabetical order so I can finally read each file located in each subdirectory to extract just the first 3 bytes of each file.
I can retrieve the first 3 bytes of each file using find and head:
$ find . -type f -exec head -c3 {} \; | sort
My problem is that before doing a head, I need to sort the names of the subdirectories.
$ find . -type d -print | sort
I can display the names of the subdirectories in alphabetical order, but I can't go down to the files by looping to extract the 3 bytes with head… WHY!!!
find . -type f -name "*" -print | cut -d/ -f2 | sort | head -c3
I've tried many possible combinations but without success.
find . -type f -name "*" -print | cut -d/ -f2 | sort | -exec head -c3 {} \;
Here, head only shows the 3 bytes of the first file… I’ve tried with xargs, -exec {} \;… nothing works…
find . -type f -name "*" -print | sort | head -c3
I've been trying to find a way to do this for almost a week, but I really don’t understand!
Finally, I wanted to sort the names of the subdirectories in alphabetical order and gradually extract the 3 bytes from each file contained within.
Not sure I’m being clear, but well…
Thank you for helping me!!!
Configuration: Linux / Firefox 60.0
5 answers
Hi,
it might be complicated, but if you insist on using
Or you can use the shell, which sorts files alphabetically:
If you need to go deeper into
it might be complicated, but if you insist on using
find, you need to add NULL characters (
\0) to the output of
find, then read its input into
sortusing the same NULL character as a separator, and then pass that output to
xargsto process each file. Phew! That's all.
$ find "$repSrc" -type f -print0 | sort -z | xargs -0 -I {} cut -c3 {} >fichier.out Or you can use the shell, which sorts files alphabetically:
for d in "$repSrc"/*/ do for f in "$d"/* do test -f "$f" && cut -3 "$f" done done >fichier.out/!\ not tested.
If you need to go deeper into
$repSrc, strictly in bash:
shopt -s globstar nullglob for f in "$repSrc"/**/* do test -f "$f" && cut -c 3 "$f" done >fichier.outto be checked...
Hello lEprofSonDkon!
find ./dossier/ -type f -print0 | sort -z | xargs -0 -I {} head -c3 {} > fichier.out
It seems that sort is not sorting the subdirectories in alphabetical order
dossier/
/sous-dos3/ text in the file:
fileA --> your help!
/sous-dos1/
fileB --> Thank you
/sous-dos2/
fileC --> for
so a first sort to have sous-dos1 sous-dos2 sous-dos3 and read each of their files to have
Thank you for your help! :)
But I just learned that you can chain two cmds with xargs! WOW!
it's almost what I'm looking for but still not my sentence in order
I put head -c3 instead of cut to get the 3 bytes of the files which works very well
$ find ./artefacts/ -type f -print0 | sort -z | xargs -0 head -c3 {} ;
head: cannot open '{}' for reading: No such file or directory
I have the subdirectories in good alphabetical order.. I don't quite understand the error though!
But I'm wondering if I shouldn't have the files in alphabetical order...
it will take me a moment to confirm but I'll get back to you as soon as possible!
Thank you very much lEprofSonDkon!!!
:):)
find ./dossier/ -type f -print0 | sort -z | xargs -0 -I {} head -c3 {} > fichier.out
It seems that sort is not sorting the subdirectories in alphabetical order
dossier/
/sous-dos3/ text in the file:
fileA --> your help!
/sous-dos1/
fileB --> Thank you
/sous-dos2/
fileC --> for
so a first sort to have sous-dos1 sous-dos2 sous-dos3 and read each of their files to have
Thank you for your help! :)
But I just learned that you can chain two cmds with xargs! WOW!
it's almost what I'm looking for but still not my sentence in order
I put head -c3 instead of cut to get the 3 bytes of the files which works very well
$ find ./artefacts/ -type f -print0 | sort -z | xargs -0 head -c3 {} ;
head: cannot open '{}' for reading: No such file or directory
I have the subdirectories in good alphabetical order.. I don't quite understand the error though!
But I'm wondering if I shouldn't have the files in alphabetical order...
it will take me a moment to confirm but I'll get back to you as soon as possible!
Thank you very much lEprofSonDkon!!!
:):)
When processing the files, and if there is only one file in each sub-folder:
you need to adapt it to your "configuration".
If I use the hierarchy given as an example, with
Without the complete structure, I don't believe I can help further.
find folder/ -mindepth 1 -type d -printf '%p\0' | sort -z | xargs -0 -I {} bash -c 'cut -b 3 "$1"/*' _ {} you need to adapt it to your "configuration".
If I use the hierarchy given as an example, with
bash -c 'echo -n "$(<"$1"/*)"' _ {}; echo, I do get the sentence correctly. Without the complete structure, I don't believe I can help further.
Yes, I understand that :)
I haven't seen the bash like that in the command line yet, but your examples help me a lot.
Ultimately, after breaking it down outside the terminal
since I found the answer to the puzzle but I can't figure out how to process it
I just realized that it's the files that I need to sort alphabetically
Sorry about that! But I had asked and was told it was the folders... so...
Now I need to list the files in alphabetical order that are in the subfolders and extract the 3 bytes
With your explanations and examples, I'm going to get back to work!! :)
Thank you very much!!!
To answer your riddle :):)
tree
.
├── parentFolder folder
│ ├── alasta subfolder
│ │ └── admi file
│ ├── alat
│ │ └── ac
│ ├── apupy
│ │ └── taig
│ ├── aukiol
│ │ └── desac
│ ├── epat
│ │ └── neige
│ ├── ero
│ │ └── lecaille
│ ├── etu
│ │ └── reem
│ .... and so on...
I haven't seen the bash like that in the command line yet, but your examples help me a lot.
Ultimately, after breaking it down outside the terminal
since I found the answer to the puzzle but I can't figure out how to process it
I just realized that it's the files that I need to sort alphabetically
Sorry about that! But I had asked and was told it was the folders... so...
Now I need to list the files in alphabetical order that are in the subfolders and extract the 3 bytes
With your explanations and examples, I'm going to get back to work!! :)
Thank you very much!!!
To answer your riddle :):)
tree
.
├── parentFolder folder
│ ├── alasta subfolder
│ │ └── admi file
│ ├── alat
│ │ └── ac
│ ├── apupy
│ │ └── taig
│ ├── aukiol
│ │ └── desac
│ ├── epat
│ │ └── neige
│ ├── ero
│ │ └── lecaille
│ ├── etu
│ │ └── reem
│ .... and so on...
GENIUS!!! Thank you thank you!
really! the order of the files turns out wonderfully!! Thanks again!!!
The line is a bit complex for me but I understand it quite well
however what I don't get is how to add my head -c3 at the end!
to get the 3 bytes
it's really a mystery to me.. I don't understand the logic
why can't I just add a simple sequence after xargs -0 printf '%s\n'?
I should be able to continue by adding
{} head -c3 {};
or
| xargs -0 printf '%s\n' {} | xargs head -c3
right?
but no!
it's not processing the head but I don't understand why sorry...
really! the order of the files turns out wonderfully!! Thanks again!!!
The line is a bit complex for me but I understand it quite well
however what I don't get is how to add my head -c3 at the end!
to get the 3 bytes
it's really a mystery to me.. I don't understand the logic
why can't I just add a simple sequence after xargs -0 printf '%s\n'?
I should be able to continue by adding
{} head -c3 {};
or
| xargs -0 printf '%s\n' {} | xargs head -c3
right?
but no!
it's not processing the head but I don't understand why sorry...
Good evening lEprofSonDkon
I don’t quite understand your response
I’ve tried several replacements but since I don’t understand the why, I can’t find a solution mamma mia! I think I’m going to submit my assignment incomplete... the only thing I managed to find is the prophecy but not how to do it in cmd line
find ./artefacts/ -type f -printf '%h#%f\0' | sort -z -t '#' -k2 | xargs -0 head -c3
head: unable to open './artefacts/verisessa#accusons' for reading: No such file or directory
Thank you very much for your help lEprofSonDkon :)
I’ve got to go to bed now... I’m working tomorrow!
maybe the night will unlock a neuron for me ... you never know!
I’ll come back tomorrow if you’re still available to help me! :):)
Thank you very much and have a good evening
I don’t quite understand your response
I’ve tried several replacements but since I don’t understand the why, I can’t find a solution mamma mia! I think I’m going to submit my assignment incomplete... the only thing I managed to find is the prophecy but not how to do it in cmd line
find ./artefacts/ -type f -printf '%h#%f\0' | sort -z -t '#' -k2 | xargs -0 head -c3
head: unable to open './artefacts/verisessa#accusons' for reading: No such file or directory
Thank you very much for your help lEprofSonDkon :)
I’ve got to go to bed now... I’m working tomorrow!
maybe the night will unlock a neuron for me ... you never know!
I’ll come back tomorrow if you’re still available to help me! :):)
Thank you very much and have a good evening
Hello lEprofSonDkon !!
I'm back!
Thank you INFINITELY!
Of course not! Haha! But I won't return the command as it is!
Now that I've managed to the end thanks to your help and not just finding the answer but also understanding why it wasn't working between sort and head!!!
Plus, with all the examples you gave me, I learned some very interesting tricks! Thank you!!
But if I don't find it before the deadline, I'm not worried about 4 points! I don't mind saying that I got help by explaining my previous steps, explaining the command you gave me to show that I understand what's going on and not just copying and pasting blindly!
I finished by adding a grep -vh to eliminate the ">==<" from the output and sent it to a tmp file... for sure, I could use the redirection... now that I think about it... to continue the command with tr
I'll test after I was just too happy to see mission accomplished!!!
And the tr -d '\n' to eliminate the end-of-line breaks. This allowed me to have the complete words... it was a clever trick! I couldn't simply put them on one line otherwise I just had the 3 bytes with a space in between each. Which gave me some weird stuff! :)
find ./artefacts/ -type f -printf '%h#%f\0' | sort -z -t '#' -k2 | sed 's/#/\//g' | xargs -0 head -c3 | grep -vh ">==<" > prophetietmp.txt
tr -d '\n' < prophetietmp.txt > prophetie.txt
It's better to understand than to have a 100% blindly!!
Thanks again so much for this help
It was a pleasure!!
Have a good day!
I will mark the question as Resolved! :):):)
I'm back!
Thank you INFINITELY!
Of course not! Haha! But I won't return the command as it is!
Now that I've managed to the end thanks to your help and not just finding the answer but also understanding why it wasn't working between sort and head!!!
Plus, with all the examples you gave me, I learned some very interesting tricks! Thank you!!
But if I don't find it before the deadline, I'm not worried about 4 points! I don't mind saying that I got help by explaining my previous steps, explaining the command you gave me to show that I understand what's going on and not just copying and pasting blindly!
I finished by adding a grep -vh to eliminate the ">==<" from the output and sent it to a tmp file... for sure, I could use the redirection... now that I think about it... to continue the command with tr
I'll test after I was just too happy to see mission accomplished!!!
And the tr -d '\n' to eliminate the end-of-line breaks. This allowed me to have the complete words... it was a clever trick! I couldn't simply put them on one line otherwise I just had the 3 bytes with a space in between each. Which gave me some weird stuff! :)
find ./artefacts/ -type f -printf '%h#%f\0' | sort -z -t '#' -k2 | sed 's/#/\//g' | xargs -0 head -c3 | grep -vh ">==<" > prophetietmp.txt
tr -d '\n' < prophetietmp.txt > prophetie.txt
It's better to understand than to have a 100% blindly!!
Thanks again so much for this help
It was a pleasure!!
Have a good day!
I will mark the question as Resolved! :):):)