Move files and directories older than specific time with the same folder structure - linux

I want to move all files and directories are located on /etc/ that are older than 90 days to /old-etc directory but with the same structure in the source directory.
Thanks

Try doing this :
find /etc -mtime +90 -type f -exec bash -c 'install -D "$1" "/old-etc/$1" && rm -f "$1"' -- {} \;

Related

Linux project: bash script to archive and remove files

I've been set a mini project to run a bash script to archive and remove files that are older than 'x' number of days. The file will be archived in the /nfs/archive directory and they need to be compressed (TAR) or removed... e.g. '/test.sh 15' would remove files older than 15 days. Moreover, I also need to input some validation checking before removing files...
My code so far:
> #!/bin/bash
>
> #ProjectEssentials:
>
> # TAR: allows you to back up files
> # cronjob: schedule taks
> # command: find . -mtime +('x') -exec rm {} \; this will remove files older than 'x' number of days
>
> find /Users/alimohamed/downloads/nfs/CAMERA -type f -name '*.mov'
> -mtime +10 -exec mv {} /Users/limohamed/downloads/nfs/archive/ \;
>
> # TAR: This will allow for the compression
>
> tar -cvzf doc.tar.gz /Users/alimohamed/downloads/nfs/archive/
>
> # Backup before removing files 'cp filename{,.bak}'? find /Users/alimohamed/downloads/nfs/CAMERA -type f name '*.mov' -mtime +30
> -exec rm {} \; ~
Any help would much appreciated!!
Modified script to fix few typos. Note backup file will have a YYYY-MM-DD, to allow for multiple backups (limited to one backup per day).Using TOP to make script generic - work on any account.
X=15 # Number of days
# Move old files (>=X days) to archive, via work folder
TOP=~/downloads/nfs
mkdir -p "$TOP/work"
find $TOP/CAMERA -type f -name '*.mov' -mtime +"$X" -exec mv {} "$WORK/work" \;
# Create daily backup (note YYYY-MM-DD in file name from work folder
tar -cvzf $TOP/archive/doc.$(date +%Y-%m-%d).tar.gz -C "$TOP/work" .
# Remove all files that were backed-up, If needed
find "$TOP/work" -type f -name '*.mov' -exec rm {} \; ~

How to copy files along with a part of folder structure in unix?

I want to copy all the files from a folder which is more than x days old to a new path.
Source folder structure:
/opt/install/sw1/team/p2.csv
/opt/install/sw2/team/p2.csv
Destination should be:
/work/backup/sw1/team/p2.csv
/work/backup/sw2/team/p2.csv
Will this do:
mkdir /work/backup/sw1/team
find /opt/install/sw1/team -maxdepth 0 -mtime +5 -exec cp '{}' /work/backup/sw1/team \;
mkdir /work/backup/sw2/team
find /opt/install/sw2/team -maxdepth 0 -mtime +5 -exec cp '{}' /work/backup/sw2/team \;
If I understand correctly:
cp -a /opt/install/sw1 /opt/install/sw2
Or for verbose:
cp -av /opt/install/sw1 /opt/install/sw2
-a says to copy everything and preserve permissions. If this is not what you want, then -r might be more appropriate.

Include folder name in renaming a file in linux

I've already used that command to rename the files in multiple directories and change JPG to jpg, so I have consistency.
find . -name '*.jpg' -exec sh -c 'mv "$0" "${0%.JPG}$.jpg"' {} \;
Do you have any idea how to change that to include the folder name in the name of the file
I am executing that in a folder that contains about 2000 folders (SKU's) or products ... and inside every SKU folder, there are 9 images. 1.jpg 2.jpg .... 9.jpg.
So the bottom-line is I have 2000 images with name 1.jpg, 2.jpg ... 9.jpg. I need those files to be unique, for example:
folder-name-1.jpg ... folder-name.2.jpg ... so on, in every folder.
Any help will be appreciated.
For example I can do as follows:
$ find . -iname '*.jpg' | while read fn; do name=$(basename "$fn") ; dir=$(dirname "$fn") ; mv "$fn" "$dir/$(basename "$dir")-$name" ;done
./lib/bukovina/version.jpg ./lib/bukovina/bukovina-version.jpg
./lib/bukovina.jpg ./lib/lib-bukovina.jpg
You can use fine one-liner:
find . -name '*.jpg' -execdir \
bash -c 'd="${PWD##*/}"; [[ "$1" != "$d-"* ]] && mv "$1" "./$d-$1"' - '{}' \;
This command uses safe approach to check whether image name is already not prefixed by the current directory name. You can run it multiple times also and image name won't be renamed after first run.
To get the folder name of a file you can do $(basename $(dirname ${FILE})), where ${FILE} is a path that may be relative but must contain at least one folder before the file name in it. This should not be a problem with find. If it is, just run it from one directory up.
find . -name '*.jpg' -exec sh -c 'mv "$0" "$(basename $(dirname $0))-${0%.JPG}$.jpg"' {} \;
Or, if you have JPEGs in your current directory:
find ../<dirname> -name '*.jpg' -exec sh -c 'mv "$0" "$(basename $(dirname $0))-${0%.JPG}$.jpg"' {} \;

linux commands to search a given folder

I have a folder in ~/Downloads with lots of
files and folders scattered. This consists of various files of different
extensions. I need to copy only the
.pdf files within various directories to ~/pdfs
Use find:
find ~/Downloads -type f -name "*.pdf" -exec cp {} ~/pdfs \;
if ~/pdfs exists in your system use the following command
cd ~/Downloads ; cp -r *.pdf ~/pdfs
if ~/pdfs does not exists in your system use the following command
cd ~/Downloads ; mkdir ~/pdfs ; cp -r *.pdf ~/pdfs
In order to deal with potential file names with spaces, etc., I would recommend this approach:
find ~/Downloads/. -type f -name "*.pdf" -print0 | xargs -0 -I_ cp _ ~/pdfs/.

Create file in Linux and replace content

I have a project in Linux. I want to create a file named index.html in all folders.
So I have used the following command:
find . -type d -exec touch {}/index.html \;
It's working! Now I'm trying to copy the existing file from a given location and it to be automatically replaced into all the folders of my project.
This should actually work exactly in the same way:
find . -type d -exec cp $sourcedir/index.html {}/index.html \;
If I understand your question correctly, what you want is to copy a given file in all the directories.
You can use a similar find command :
find . -type d -exec cp -f /tmp/index.html {} \;
where /tmp/index.html is path to the original file (replace it with your own path).
Also, you don't need to create the files if your final objective is to replace them with the original file.
tar -cvzf index.tar.gz `find . -type f -iname 'index.html'` && scp index.tar.gz USER#SERVER:/your/projec/root/on/SERVER && ssh USER#SERVER "tar -xvzf index.tar.gz"
Or if you're in the proper directory localhost, and rsync is available:
rsync -r --exclude='**' --include='**/index.html' . USER#SERVER:/your/projec/root/on/SERVER
HTH

Resources