Portál AbcLinuxu, 12. května 2025 02:48
find -name *.txt
tyto soubory chci premistit nebo zkopirovat do adresare zaloha
zkousel jsem
find -name *.txt|cp "sem nevim co napsat" zaloha
dale jsem zkousel find -name *.txt > f1
a pak cp < f1 zaloha
diky
cp `find . -name \*.txt` zaloha/ find . -name \*.txt -exec cp {} zaloha/ \; find . -name \*.txt | xargs -ixxx cp xxx zaloha/ \;Každý má své výhody a nevýhody. Samozřejmě je potřeba ještě ošetřit problémové znaky v názvech souborů.
find -name '*.txt' -exec ls {} \;To s hvezdickou musi byt v uvozovkach/apostrofech, jinak to expanduje shell. Vice viz 'man find'.
#!/usr/bin/env python import os, sys, stat, time, re, pwd dir = '/tmpsamba' file_age = 10 * 24 * 3600 #Delete files older than this number of seconds epochsec = time.time() #Current time def is_file_old_enough(path, anewer): if not os.path.isfile(path): print "Error, " + path + " is not a file" return False time_threshold = epochsec - anewer if time_threshold < os.stat(path).st_atime: # print 'File ' + fullpath + ' was recently accessed' return False else: return True def is_path_old_enough(path, anewer): if os.path.islink(path): return True # We can always consider links to be old # enough to be deleted if not os.path.isdir(path): #If the tested path is not a directory, ... return is_file_old_enough(path, anewer) for root, dirs, files in os.walk(path, topdown=False): for name in files: fullpath = os.path.join(root, name) if os.path.islink(fullpath): continue #This should skip links if not is_file_old_enough(fullpath, anewer): return False return True def rm_rf_path(path_to_rm): if not re.search('^/tmpsamba/', path_to_rm): # Just to make sure we are not # deteting something valuable print "Refused to delete " + path_to_rm return stat_result = os.lstat(path_to_rm) #do not follow symbolic links uid = stat_result[stat.ST_UID] name = pwd.getpwuid(uid).pw_name print "Deleting " + path_to_rm, '('+name+')' os.system("rm -rf '" + path_to_rm + "'") return items = os.listdir(dir) for i in items: fullpath = os.path.join(dir, i) if is_path_old_enough(fullpath, file_age): rm_rf_path(fullpath)
find -type d -o -type f -ctime -10 -printf '%h\n'Tudíž ostatní smažeme, doplníme zbytek roury (uniq bohužel nemá volbu -z):
sort -z <(find -type d -o -type f -ctime -10 -printf '%h\000') <(find -type d -print0) \ | tr '\000' '\n' | uniq -u | xargs rm -rZbudou ke smazání ještě prázdné adresáře:
find -depth -mindepth 1 -type -d -empty -exec rmdir {} \;Teď ještě vypsat, komu to patřilo, což bohužel doposud pěkné roury znepřehlední, a tak to nechám na čtenáři...
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.