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

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.

Related

Shell cp: cannot stat no such file or directory

I was trying to use cp to copy files from one directory to another by globing
for files in index/*
do
file=$(echo $files|cut -d'/' -f2)
cp -r "$files" ".target/file"
done
However, cp will give this warning if the directory is empty. I tried 2>/dev/null to mute this message but it did not work. I wonder how I could fix it.
What about this: (not tested)
find /index -maxdepth 1 -type f -exec cp {} .target/ \;
-maxdepth 1 : only look in this directory
-type f : only take the files
-exec cp {} .target/ \; : execute a "file copy" action

problem in copying find results in another directory

I'm trying to execute this command to copy the latest file that exist in the courant directory to another one .
find . -mtime -1 -exec cp -r {} /media/96DB-120D/bck \;
but after copying the recent files , I find the other content of the folder that does not respond to the condition -mtime -1 .
If any one had an idea about how to fix it to just copy the result of find command and thanks.
The find command probably includes the directory and then cp copies all the files in the directory. Add -type f to only have find report actual files.
Try the -p option of cp command which will preserve the timestamp of the copied file:
find . -mtime -1 -exec cp -pr {} /media/96DB-120D/bck \;
I think this is the best solution :
find . -mtime -1 -type f -exec cp --parents {} /media/960DB-120D/db \;

create a new directory under all directories using single command : Linux

Suppse if I have directories
dir_1/ dir_2/ dir_3/
How can I create a directory of same name under all these directories using a single command?
Here is one command for you:
If your want the sub dir have the same name as the parent dir:
for i in ./dir_*; do mkdir -p "${i}/${i}"; done
If you want the sub dir share the same new name.
for i in ./dir_*; do mkdir -p "${i}/new_dir_name"; done
You should use Brace Expansion
mkdir dir_{1..3}/newDir
Only work if you know the names of the dirs in advance, of course.
Doesn’t work with ’sh’ though.
You can use this find:
find . -maxdepth 1 -type d -name 'dir_*' -exec mkdir {}/{} \;
Test:
$ ls
dir_1 dir_2 dir_3 file1 file2 file3
$ find . -maxdepth 1 -type d -name 'dir_*' -exec mkdir {}/{} \;
$ ls dir_1/
dir_1
Using find you could do:
find . -type d -maxdepth 1 -execdir mkdir -p "{}/{}" \;
This will create directory/directory if it doesn't already exist.

MOVING Files and place them into folders accordingly to text file

I need to move files from ORIGIN and place them to DESTINATION accordingly to the information contained in text file "toto.txt"
I do NOT know how to code the part which says:
place these files accordingly with the information contained in toto.txt which states
the sub-folder structure on DESTINATION folder"
toto.txt conatins the folder structure of ORIGIN and the files must be moved accordingly to DESTINATION but with the original folder structure location.
# My working Paths
MY_DIR1="/media/nss/MBVOL1/TEST/ORIGIN"
MY_DIR2="/media/nss/MBVOL1/TEST/DESTINATION"
# Flag files older than 1 day and list their name\full path to “TOTO” text file
echo "REPORT Created"
cd $MY_DIR1 && find . -mindepth 0 -maxdepth 40 -mtime +1 -type f > toto.txt
cp $MY_DIR1/toto.txt /$MY_DIR2
# Flag files older than 1 day then MOVE file to “DESTINATION” Folder
echo "FILES Moved"
find $MY_DIR1 -mindepth 0 -maxdepth 400 -type f -mtime +14 -exec mv '{}' $MY_DIR2 \;
Try this:
cd "$MY_DIR1"
# Duplicate directory structure
find . -type d -exec mkdir -p "$MY_DIR2"/{} \;
# move files older than 1 day
find . -type f -mtime +1 -exec mv {} "$MY_DIR2"/{} \;
You can combine them into one command:
find . -type d -exec mkdir -p "$MY_DIR2"/{} \; -o -type f -mtime +1 -exec mv {} "$MY_DIR2"/{} \;
Use something like this...
cat ${MY_DIR2}/toto.txt | while read FILE ; do
mv -v "${MY_DIR1}/${FILE}" "${MY_DIR2}"
done

Delete script only deleting files not folders

I have a script that I run every week via a cron that delete loads of temp files on my web servers but I've just notice that the script is not removing the folders which are still taking up space.
Whats the best to amend the script to delete the folders as well?
find /my/path/ -type d –ctime +5 –exec rm -r {} \;
#!/bin/bash
# Deletes temp files older then 5 days
DOCS='/var/www/user/docs/temp'
SCRATCH='/var/www/user/temp/scratch'
if [ -d "$DOCS" ]; then
find $DOCSJS -type f -mtime +5 -exec rm -rf {} \; > /tmp/docs.txt
fi
if [ -d "$SCRATCH" ]; then
find $SCRATCH -type f -mtime +5 -exec rm -rf {} \; > /tmp/docs.txt
fi
My script actually has -rf as a flag to delete all folders.
One point to note is that the folders aboves are symbolic links to /mnt/sdb1/docs/js and /mnt/sdb1/temp/scratch - would the script follow the symbolic link or should I hard code this to the mount?
change this
if [ -d "$SCRATCH" ]; then
find $SCRATCH -type f -mtime +5 -exec rm -rf {} \; > /tmp/docs.txt
fi
into
if [ -d "$SCRATCH" ]; then
find $SCRATCH -type d -mtime +5 -exec rm -rf {} \; > /tmp/docs.txt
fi
you only search for files using the -type f flag; use -type d for directories
You need to pass additional options to rm commands in your scripts. Actually you need rm -rf. At all I'd recommend to go man 1 rm.
change this:
find $DOCSJS -type f -mtime +5 -exec rm
to:
find $DOCSJS -type f -mtime +5 -exec rm -Rf
Currently you cannot delete directories as the output of find is limited to files. You should drop this option so that both old files and directories are passed to rm -rf
find $DIR -mtime +5 -exec rm -rf {} \;
This will preserve old directories which have new files in them (but it will remove the old files), but get rid of files and directories where both are old.

Resources