cron job to move 10 day old files to different directory - file

I need a script that I can run from a cron job to move files that are 10 days old or older to a different directory. Being a windows sys admin, I have no idea what I'm doing in linux. :( Can someone point me in the right direction?
Thanks in advance.

find /source/directory/* -mtime +10 -exec mv "{}" /my/directory \;
Use 'crontab -e' to open your crontab and set it to run as needed.

Related

find -mtime option on linux listing all files regardless of time?

I'm trying to use the linux find command to show me the files in my current directory that have been modified in the last 24h. To test my command, I piped it to ls as below.
find . -maxdepth 1 -mtime -1 | xargs -I{} ls -lh {}
but this command displays all the files in my directory, some of which were modified last year. What am I missing here? I also tried -atime and -ctime options out of curiosity and got seemingly the same file list.
Many thanks for the advice!
In my case the issues was caused because I had the wrong system time on my raspberry.
This was because systemd-timesyncd.service was no longer running.
After:
sudo systemctl unmask systemd-timesyncd.service
sudo systemctl enable systemd-timesyncd.service
sudo reboot
the system time was correct again. Note that the pi needs to be connected to a network for this to work. And also, of course, past wrong file dates don't get fixed by this (duh).

Default permission and ownership in WSL

I'm currently in the process of switching from an Ubuntu 18.04 VirtualBox to the WSL.
Everything is up and running to have a complete web dev environment.
Unfortunately, the file permission and ownership is kind of a problem for me right now.
--- Disclaimer---
I know that in a server environment this would be pretty bad and wouldn't be done.
This is just for my local development process and this requirement will stay there.
What I want to achieve is, to have a consistent set of permissions and ownerships within my /var/www (and all below).
Every element within /var/www should be owned by my shell user, regardless if file or dir.
Every element should have the group www-dataso that the webserver user have access.
Each file should have permission 0664 and each directory 0775.
every new element should follow these requirements.
What I researched so far:
If I make a new directory/ file from the shell or within VSCode, my user and group are used.
If I make a new directory from the windows explorer, the ownership is root:root. Nevertheless, I also have to figure out how to set default permissions for new objects.
I just want an easy to use way to CRUD my files from anywhere I am on my system.
Is there anyone with an idea for this?
Thanks a lot, Danaq.
Edit: It would also be ok, to set everything to www-data:ww-data and add my shell user to the www-data-group if the permissions are applied like explained above.
According to this, newer versions of the Remote-WSL server, when they get started will execute (if present):
~/.vscode-server/server-env-setup
~/.vscode-server-insiders/server-env-setup
(more on that)
So a solution is to execute:
# this will affect folders created from the editor
mkdir -p ~/.vscode-server/ && echo "umask 002" >> ~/.vscode-server/server-env-setup
mkdir -p ~/.vscode-server-insiders/ && echo "umask 002" >> ~/.vscode-server-insiders/server-env-setup
# this will affect folders created from terminals
echo "umask 002" >> ~/.bashrc
For everyone encountering the same issue and find this question:
Under ~[USER~/.profile I uncommented the umask-property and set it to 002.
This will, according to this guide, set all new created file to 0664 and all new directories to 0775.
I then added the www-data-user to the group of my shell-user with
sudo usermod -a -G www-data [GROUP].
So now, every file I'm creating is owned by and within the group of my shell user. But it can be red by the www-data-user too. This should lead to not always using the
find ./ -type d -exec chmod 775 {} \;
the command to set the right permissions on all directories of a web project after migration for example.
This solution does only help, if the files and directories are created from the WSL-bash. If you are using the explorer or trying to create a file from the VSCode-terminal, the default umask of 022 will be still applied.

Deleting Large number of files from a folder in linux

I am working on a linux production environment where there are about 20 million marker files existing in a folder and these are increasing at the rate of about 10,000 per day.
I need to perform cleanup on this folder and delete all files older than 5 days.
I cannot delete the entire folder as it is an active production environment and is mounted on other servers as well.
I tried using a find command as below
find /dirpath -name "*.fileExtension" -mtime 5 | xargs rm {}
and I also tried
find /dirpath -name "*.fileExtension" -mtime 5 | exec rm {}
but the rate at which these commands delete the files is very slow.
Is there a faster way to perform this activity.

CakePHP 2.1 Permission denied on cron job

I am getting this error when I try to run a CakePHP 2.1 Shell from a cron job:
/bin/sh: /home/[other-folders]/lib/Cake/Console/cake: Permission denied
This is the code I've been using for almost a year with CakePHP 1.3.7 and it worked well. What could the problem be? I've checked the permissions on the cake file, and it is 755, just like the cake file I had in 1.3.7. Thanks!
Are all the directories in the path set to at least +x for the user executing the cron job? If you can't at least execute the directories above cake, you won't be able to execute the program.
Make sure the file path you set in cron is executable:
ls -l /path/to/project/folder/file
chmod +x /path/to/project/folder/file
ls -l /path/to/project/folder/file
So, latest permission will be like -rwxr-xr-x. and things will be working for you. , This solution not only applied to cake but any file you set in cron.
Thats it! Cheers!!

Tomcat is installed with CATALINA_HOME in /usr/share/tomcat6 and CATALINA_BASE in /var/lib/tomcat6

I think it is a good question.
I found it also confusing. I installed tomcat few minutes ago (after a while) and I notice they are two different "webapps" places.
at /usr/share/tomcat6/webapps/default_root/
at /var/lib/tomcat6/webapps/ROOT/
The content of both are the same but none of them are symlinks.
When I started tomcat it says:
Quote:
This is the default Tomcat home page. It can be found on the local filesystem at: /var/lib/tomcat6/webapps/ROOT/index.html
Tomcat6 veterans might be pleased to learn that this system instance of Tomcat is installed with CATALINA_HOME in /usr/share/tomcat6 and CATALINA_BASE in /var/lib/tomcat6
So, based in this information, the /usr/share/... folder was kept for backward compatibility, right?
But according to what you posted libs are being read from the CATALINA_HOME instead of CATALINA_BASE.
Perhaps they still keep that default reference to prevent other systems to stop working after updating?
Good observation!
UPDATE:
I read this at: /usr/share/tomcat6/bin/catalina.sh :
Quote:
# CATALINA_HOME May point at your Catalina "build" directory.
#
# CATALINA_BASE (Optional) Base directory for resolving dynamic portions
# of a Catalina installation. If not present, resolves to
# the same directory that CATALINA_HOME points to.
When we read at: /etc/init.d/tomcat6 :
(at the beginning)
CATALINA_HOME=/usr/share/$NAME
(then...)
# Directory for per-instance configuration files and webapps
CATALINA_BASE=/var/lib/$NAME
However, If you try to start TOMCAT manually (as I did long time ago) with:
sudo /usr/share/tomcat6/bin/startup.sh
It displays:
Quote:
Using CATALINA_BASE: /usr/share/tomcat6
Using CATALINA_HOME: /usr/share/tomcat6
Using CATALINA_TMPDIR: /usr/share/tomcat6/temp
Using JRE_HOME: /usr
touch: cannot touch `/usr/share/tomcat6/logs/catalina.out': No such file or directory
/usr/share/tomcat6/bin/catalina.sh: 357: cannot create /usr/share/tomcat6/logs/catalina.out: Directory nonexistent
So, why here CATALINA_BASE is set to the same as CATALINA_HOME?
/usr/share/tomcat6/logs/ do not exist, but exists in /var/lib/tomcat6/logs/
I see that my logs are writing into /var/lib/... when starting Tomcat from the init.d script. So its better starting it from there.
I'm not an expert on Tomcat but I was having this same problem and I was able to restart the server with the command:
sudo /etc/init.d/tomcat6 restart
CATALINA_HOME -- tells "org.apache.catalina.startup.Bootstrap" where to look for required /lib /bin and other -- which are dependencies to run the server . It is basically your Tomcat installation home directory.
CATALINA_BASE -- expects a certain directory structure to scan for (once started). For example /conf to find server.xml and web.xml which is specific to a web application. as long as you have got that directory structure and content, CATALINA_BASE can be any directory.
I've seen that.
The proper and cute way is to create a "setenv.sh" script into your CATALINA_HOME/bin folder(in your case "/usr/share/tomcat6/bin/setenv.sh").
The contents of your setenv.sh:
#!/bin/sh
export CATALINA_BASE=/new/catalinabase/path
So there is no need to modify catalina.sh by yourself. If setenv.sh is present in bin directory, catalina.sh will execute it automatically.
I can't tell you why CATALINA_HOME and CATALINA_BASE are the same in startup.sh (resp. catalina.sh) - that's really strange.
Of course you can start Tomcat via the init.d script. But sometimes you may want to start it via startup.sh/catalina.sh (e.g. for debugging).
So as a workaround/hack you can correct the problem by adding the following line at the beginning of /usr/share/tomcat6/bin/catalina.sh
export CATALINA_BASE=/var/lib/tomcat6
I could do this by executing this command,
$ service tomcat6 start
which reinitialized my CATALINA_* path and starts the tomcat instance
chmod +x startup.sh
chmod +x shutdown.sh
chmod +x catalina.sh
chmod +x setclasspath.sh
chmod +x bootstrap.jar
chmod +x tomcat-jni.jar
hope it will be work.

Resources