Skip to content

Instantly share code, notes, and snippets.

@samukasmk
Created May 15, 2015 15:05
Show Gist options
  • Save samukasmk/b25be34322ec1c033152 to your computer and use it in GitHub Desktop.
Save samukasmk/b25be34322ec1c033152 to your computer and use it in GitHub Desktop.
Exemplo de find no linux com datas
###### Exemplos de uso de data com find:
# http://www.unix.com/shell-programming-and-scripting/39909-delete-file-older-than-n-days.html
"Yes, the 'find' has three options for number of days:
1) +n --> More than 'n' days ago.
2) -n --> Less than 'n' days ago.
3) n ---> Exactly 'n' days ago.
Thus, if you specify '+1', it means more than 1 day ago."
###### Consultando o dia de hoje (para servir de exemplo)
~/tmp ᐅ date
Sex Mai 15 11:55:24 BRT 2015
###### Criando arquivos com datas passadas de:
# hoje
~/tmp ᐅ touch hoje
~/tmp ᐅ ls -la hoje
-rw-rw-r-- 1 samuel samuel 0 Mai 15 11:50 hoje
# 1 dia atras
~/tmp ᐅ touch -t 201505140000 ontem
~/tmp ᐅ ls -la ontem
-rw-rw-r-- 1 samuel samuel 0 Mai 14 00:00 ontem
# 2 dias atras
~/tmp ᐅ touch -t 201505130000 antes_ontem
~/tmp ᐅ ls -la antes_ontem
-rw-rw-r-- 1 samuel samuel 0 Mai 13 00:00 antes_ontem
# mes passado
~/tmp ᐅ touch -t 201504010505 mes_passado
~/tmp ᐅ ls -la mes_passado
-rw-rw-r-- 1 samuel samuel 0 Abr 1 05:05 mes_passado
# ano passado
~/tmp ᐅ touch -t 201401010505 ano_passado
~/tmp ᐅ ls -la ano_passado
-rw-rw-r-- 1 samuel samuel 0 Jan 1 2014 ano_passado
# amanha
~/tmp ᐅ touch -t 201505160110 amanha
~/tmp ᐅ ls -la amanha
-rw-rw-r-- 1 samuel samuel 0 Mai 16 2015 amanha
###### Pesquisando por datas
# de ontem para traz
~/tmp ᐅ find . -type f -mtime +0
./ontem
./mes_passado
./antes_ontem
./ano_passado
# de antes de ontem para traz
find . -type f -mtime +1
./mes_passado
./antes_ontem
./ano_passado
# apenas de hoje
~/tmp ᐅ find . -type f -mtime 0
./hoje
# de hoje para o futuro
~/tmp ᐅ find . -type f -mtime -1
./hoje
./amanha
# de ontem para amanha
~/tmp ᐅ find . -type f -mtime -2
./ontem
./hoje
./amanha
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment