You mean like this, just prepending the current directory to each filename?
ls -l | awk -v PWD=$PWD '{printf("%s %s/%s\n", $5, PWD, $9); }'
(the -v
just imports the $PWD
shell variable into the awk script).
Or something else?
OK, apparently what you want is
$ cd /some/path/to/somewhere
$ <insert command here>
somewhere/file1 size1
somewhere/file2 size2
...
Is that correct?
If so, the change you need is this:
ls -l | awk -v PWD=$(basename $PWD) '{printf("%s/%s %s\n", PWD, $9, $5); }'
If you have an older shell, the $()
may not work, in which case try:
ls -l | awk -v PWD=`basename $PWD` '{printf("%s/%s %s\n", PWD, $9, $5); }'
instead. I don't have immediate access to any shell that doesn't support $()
, but I can't think where else your Illegal variable name
error would come from, when this works for me.
If it still doesn't work, please describe your platform, shell, version of awk etc. in your question - the comment thread is getting pretty long and I'm running out of guesses :-)
With zsh
, using an associative array whose keys are the dates and values the NUL-delimited list of files last modified on that date:
zmodload -F zsh/stat b:zstat
typeset -A files
for file (./*) {
zstat -LA date -F %b_%d +mtime $file &&
files[$date]+=$file$'\0'
}
for date (${(k)files})
echo tar zcvf $date.tar.gz ${(0)files[$date]}
Remove the echo
when happy.
Note that the month name abbreviations (%b
strftime format) will be in the current locale's language (Oct
on an English system, Okt
on a German one, etc.). To always make it English names regardless of the user's locale, force the locale to C
with LC_ALL=C zstat...
.
With GNU tools, you could do the equivalent with:
find . ! -name . -prune ! -name '.*' -printf '%Tb_%Td:%p\0' |
awk -v RS='\0' -F : -v q=\' '
function quote(s) {
gsub(q, q "\\" q q, s)
return q s q
}
{
date=$1
sub(/[^:]*:/, "", $0)
files[date] = files[date] " " quote($0)
}
END {
for (date in files)
print "tar zcvf " quote(date ".tar.gz") files[date]
}'
Pipe to sh
when happy.
Best Answer
You can use the
find
command to find all files that have been modified after a certain number of days.For example, to find all files in the current directory that have been modified since yesterday (24 hours ago) use:
Note that to find files modified before 24 hours ago, you have to use
-mtime +1
instead of-mtime -1
.