Tag Info

New answers tagged

1

A portable way to prevent find from recursing is to execute the -prune action on directories other than the toplevel directory. find "$dir" \! -name "$(basename -- "$dir")" -type d -prune -o -mmin +3 -type f -print


5

The data passing through the pipe is not written or read to and from the filesystem. When creating the named pipe an inode is allocated so that it can be linked to from the directory you created it in; so that's a form of "modifying the filesystem". Accessing the named pipe will update the inode's access time, writing to the named pipe will update the ...


3

By default, find includes everything in its search: directories, files, and symlinks. find "/path/to/dir" -mmin -30 -not -name ".*" -exec zip -r "testfile.zip" "{}" \+ If /path/to/dir was modified in the last 30 minutes, it will pass all the tests, and zip, since it was given the -r option, will add the directory and everything under it to the archive. ...


0

You would be much better off using tmpwatch tmpwatch recursively removes files which haven't been accessed for a given time. Normally, it's used to clean up directories which are used for temporary holding space such as /tmp.


0

Use the logical not operator ! or -not to exclude the path /var/tmp from the results. Note: -not is not POSIX compliant. find /var/tmp -type d -ctime -1 ! -path /var/tmp -exec rm -rf {} \; Or find /var/tmp -type d -ctime -1 -not -path /var/tmp -exec rm -rf {} \;


2

The first directory find /var/tmp finds is /var/tmp. If you want to skip that one (and use Gnu find) then you can change the command to: find /var/tmp -depth -mindepth 1 -type d -ctime -1 -exec rm -rf {} \; or find /var/tmp -mindepth 1 -type d -ctime -1 -exec rm -rf {} \; -prune Without -depth and -prune error messages may occur because rm -rf ...


1

I'm going to focus on just the removal part of your question. If you have the list of filenames like this: $ cat data.txt 01012000 01022000 01032000 01012014 01022014 01032014 01042014 And you know that the cut off date for 6 months, is say "01022014". You can use sort & sed to determine which files need to be deleted, like so: This will reverse sort ...


2

You could convert the filename into something that can be compared directly (such as the Unix timestamp (number of seconds since the epoch), or to YYYYMMDD, which would be lexicographically sortable), and then check if it's older than six months. For example, a script like (say, at /path/to/compare.sh): #! /bin/bash LAST=$(date -d '6 months ago' +%s) for ...


0

You could use ranged brace expansion: Remove every half year: echo /path/to/directories/01{01..06}{2000..2014} OR remove everything except the last half year: echo /path/to/directories/01{01..12}{2000..2013} echo /path/to/directories/01{01..06}2014 Replace echo with rm -r and update the path.


4

You can try something like: EXT=${FILE#*.} NAME=${FILE%%.*} mv "$FILE" "$NAME$(date --reference "$FILE" '+%Y%m%dT%H%M').$EXT" in a script, if your date supports --reference, which picks up the last modification date of the reference file.


4

I think this could work: for i in *; do fileTime=$(stat -c %Y "$i"); #Get last modification (since EPOCH) formatDate=$(date +%Y%m%dT%H%m -d @"$fileTime"); #Get time in format YYYYMMDDTHHmm mv "${i%%.*}"_"$formatDate"."${i#*.}"; #Appends "formatDate" before extension done Regards.


1

You could use a nice bash conditional execution to see whether the file exists or not... [ -f tmp1.txt ] && echo "Found" || echo "Not found" Alternatively you could use the find command with the -newer flag... find /some/dir -type f -newer $startdate -not -newer $enddate


0

Below python script does the job, days from on which the files should be deleted can be configured with days variable. #!/usr/bin/env python3 import os import re import datetime days=60 delta = datetime.date.today() - datetime.timedelta(days=days) files = [ x for x in os.listdir() if re.search('_\d{8}\.', x)] for file in files: date = ...


2

OK, I have remade this script, and by sorting it backwards it looks like it should work. It compares the year and month to the previous one, and if it is lower it should be the last entry for that month. #!/bin/bash #the tac reverses the listing, so we go from newest to oldest, vital for our logic below FILES=`ls | tac` #create a cutoff date by taking ...


0

You can try this in while loop: #!/bin/bash A=vtm_data_12month_20140301.txt B=`ls vtm_data_12month_20140301.txt | awk -F "_" '{print $4}' | awk -F "." '{print $1}'` C=`date --date="60 days ago" +%Y%m%d` if [ "$B" < "$C" ] then rm -fr $A else echo "$A is not older" fi


0

The following code does not preserve the last file of each month. #! /bin/bash cmp_timestamp=$(date --date="60 days ago" +%Y%m%d) while read filename; do [[ $filename =~ _(20[0-9][0-9][01][0-9][0123][0-9])\. ]] timestamp=${BASH_REMATCH[1]} printf "%-40s : %s\n" "$filename" "${timestamp}" if [ "$timestamp" -lt ...



Top 50 recent answers are included