How to check if a file is older than 1 week and then move to archive folder - unix

How to check if a file is older than 1 week and then move to archive folder.Using some if condition we should be checking the file is exists which is 1 week older.Then try to move the same to archive

This should work:
find <path> -atime +1w -exec mv {} <dest_dir> \;
Argument -atime controls last access time, you may want to use ctime (creation timestamp) or mtime (last modification timestamp). +1w means more than one week.
Argument exec let find execute some command on the files that meet the criterion. {} is a place to be filled with the path of the files found.
Read the manual for more criteria.

Related

View files saved after specific date in unix

I try to open a group of files which were saved after a specific date using the following command
View /*/*log | grep 'Aug 30'
But I get a message as
Vim: warning : output is not to a terminal
And nothing happens.
Any suggestions???
You are effectively telling view to open all the log files. You then tell it to send its output not to the screen as normal, but to another command called grep.
You probably want to use find to generate a list of files and then tell view to open them. So, to find files changed yesterday (1 day ago), you could use:
find /wherever/the/logs/are -name \*.log -mtime -1
Now, you want to edit those files, so pass the list to view:
view $(find /wherever/the/logs/are -name \*.log -mtime -1)

Avoid Unix PATH duplicating with "set -f"

I've seen a script that modifies the Unix $PATH, and in order to avoid duplicating items, it uses the following technic:
set path = ($path:q /some/new/path)
set path = ($path:q /another/directory)
set -f path = ($path:q)
I don't understand how is working...
Documentation to the "-f" flag says :
Disable file name generation
which doesn't make any sense to me. And what's this strange ":q"?
Thanks!
EDIT:
This Super User Question helped me understand that ":q" is a modifier.
And tcsh man explains it:
When the `:q' modifier is applied to
a substitution the variable will expand to multiple words
with each word separated by a blank and quoted to prevent
later command or filename substitution
Second Edit:
Actually, it seems that "-f" alone does the magic:
~$ set days = (Sunday Monday Tuesday Monday Sunday)
~$ echo $days
Sunday Monday Tuesday Monday Sunday
~$ set -f days = ($days)
~$ echo $days
Sunday Monday Tuesday
Still, I don't understand how is result of "Disable file name generation".
Disabling file name generation usually needed if we encounter file names that contain *, ?, {} etc. Care should be taken while handling these files so that we don't process file name as a wildcard pattern. Crete a file named stack* as vim stack*, later we shouldn't delete this file since all other files start with stack also gets deleted. Alternate way to delete the file is using quoting as rm "stack*". If required it is possible to enable file name generation by set +f in the shell.
Your confusion arises from the fact that you're reading the ksh manual, but you're using the tcsh shell. Tcsh syntax is very different from the vastly more common POSIX shell syntax.
The command set is built-in to the shell, so when you run set in tcsh you get an entirely different command to set run from ksh.
From man tcsh:
set [-r] [-f|-l] name=(wordlist) ... (+)
...
If -f or -l are specified, set only unique words keeping their
order. -f prefers the first occurrence of a word, and -l the
last.

finding files in unix based on creation/modification date

How to find files on a unix server which were created/modified in previous month?
for ex. If the current month is Jul then the list of files which were created/modified in Jun should get displayed.
One way is to execute that command.
ls -laR | grep monthName where montName could be Jan,Feb,Mar, and so on ... (remember to change working directory to directory that you're interested in. Also notice that this method is recursive so all sub-directories will be inspected
With this you also retrieve all file permission and so on...
I'm sure that will be better ways (if them jump into my mind, I'll edit this post) but since I'm in coffee break, this is the faster that I find.
In order to find files modified in the previous month, you will need to use find with a set range, for example:
cd / (if you are wanting to start from the root)
find . -type f -mtime +26d -mtime -56d -print
You should adjust your range to include the dates that you wish to include.
All the best to you!
monthToFind=`date -d "1 months ago" "+%Y-%m"`
find . -printf "%TY-%Tm %p\n" | egrep "^$monthToFind " | sed "s/^$monthToFind //g"
This will be slower than using a time range in find. But the time range is hard to determine, and quickly becomes invalid, possibly even while the command is executing.
Unfortunately this will miss files modified last month when they were also modified this month. I don't know of a way to determine these files.

How to find the files that are created in the last hour in unix

How to find the files that are created in the last hour in unix
If the dir to search is srch_dir then either
$ find srch_dir -cmin -60 # change time
or
$ find srch_dir -mmin -60 # modification time
or
$ find srch_dir -amin -60 # access time
shows files whose metadata has been changed (ctime), the file contents itself have been modified (mtime), or accessed (atime) in the last hour, respectively.
ctime is non-unintuitive and warrants further explanation:
ctime:
Unlike mtime, which is only related to the contents inside a file, changed timestamp indicates the last time some metadata of a file was changed. ctime refers to the last time when a file’s metadata.
For example, if permission settings of a file were modified, ctime will indicate it.
UNIX filesystems (generally) don't store creation times. Instead, there are only access time, (data) modification time, and (inode) change time.
That being said, find has -atime -mtime -ctime predicates:
$ man 1 find
...
-ctime n
The primary shall evaluate as true if the time of last change of
file status information subtracted from the initialization time,
divided by 86400 (with any remainder discarded), is n.
...
Thus find -ctime 0 finds everything for which the inode has changed (e.g. includes file creation, but also counts link count and permissions and filesize change) less than an hour ago.
check out this link and then help yourself out.
the basic code is
#create a temp. file
echo "hi " > t.tmp
# set the file time to 2 hours ago
touch -t 200405121120 t.tmp
# then check for files
find /admin//dump -type f -newer t.tmp -print -exec ls -lt {} \; | pg
find ./ -cTime -1 -type f
OR
find ./ -cmin -60 -type f
sudo find / -Bmin 60
From the man page:
-Bmin n
True if the difference between the time of a file's inode creation and
the time find was started, rounded up to the next full minute, is n
minutes.
Obviously, you may want to set up a bit differently, but this primary seems the best solution for searching for any file created in the last N minutes.
Check out this link for more details.
To find files which are created in last one hour in current directory, you can use -amin
find . -amin -60 -type f
This will find files which are created with in last 1 hour.

Locating most recently updated file recursively in UNIX

For a website I'm working on I want to be able to automatically update the "This page was last modified:" section in the footer as I'm doing my nightly git commit. Essentially I plan on writing a shell script to run at midnight each night which will do all of my general server maintenance. Most of these tasks I already know how to automate, but I have a file (footer.php) which is included in every page and displays the date the site was last updated. I want to be able to recursively look through my website and check the timestamp on every file, then if any of these were edited after the date in footer.php I want to update this date.
All I need is a UNIX command that will recursively iterate through my files and return ONLY the date of the last modification. I don't need file names or what changes were made, I just need to know a single day (and hopefully time) that the most recently updated file was changed.
I know using "ls -l" and "cut" I could iterate through every folder to do this, but I was hoping for a quicker-running and easier command. Preferably a single-line shell command (possibly with a -R parameter)
The find outputs all the access times in Unix format, then sort and take the biggest.
Converting into whatever date format is wanted is left as an exercise for the reader:
find /path -type f -iname "*.php" -printf "%T#" | sort -n | tail -1
GNU find
find /path -type -f -iname "*.php" -printf "%T+"
check the find man page to play with other -printf specifiers.
You might want to look at a inotify script that updates the footer every time any other file is modified, instead of looking all through the file system for new updates.

Resources