I have a bunch of files which all have a first line being a date. All of them start with the same first 2 letters being "ff". I need to create a script which would use that first line in all files and sort them in directories creating these directories as well.
For example 1 file contains that:
I need to extract the full date moving all files into directories for year then sub directories for month and another sub for day. So far I have this script which only moves files by year. I was told that I could use -p function, but really I have been using UNIX for 3 days so got confused now.
for file in $(ls ff*)
but usefor file ff*
instead. 2. Put your$variables
into double quotes. For examplemkdir "$test"
instead of justmkdir $test
. You will get an error if you try to create a directory that already exists. You can protect against that, or tellmkdir
not to worry if it already exists withmkdir -p "$test"
. 3. In your first screenshot I can see only one file with a name starting withff
. 4. If the file starts withff
(line 7) it can't begin with a year (line 9).. Are you sure your requirements are correct?for file in ff*
I guess. And you should placehead
beforecut
for better performance.head
when you're processing part of a single filename?$(cut -d"-" -f1 $file | head -1)
I'd use$(head -n 1 $file | cut -d"-" -f1)
to avoid cutting all lines of the file (don't know how big the files are, maybe they are many gigabytes....). OR will it the unix magic stop the function anyways afterhead -1
? If so, I' m definately wrong! But this for sure: better usehead -n1
instead of deprecatedhead -1
.