FreeBSD copy directories with skip - freebsd

I need to copy folders by this command:
cp -R /usr/www/data/www.domaun.com/WWW/ /usr/www/data/new.domain.com/
But skip directories:
audio
b
f
img
tmp
and also skip files larger than 3 MB.
Thank you !

You could use rsync, e.g.
rsync -a --max-size=3M \
/usr/www/data/www.domain.com/WWW/ /usr/www/data/new.domain.com/ \
--exclude audio/ --exclude b/ --exclude f/ --exclude img/ --exclude tmp/

Related

rsync files/folders from a list in an input file and exclude from an exclude file

I have two files with a list of folders. I want to sync the folders, with relative paths, to the destination excluding those from the exclude file.
$ cat include.txt
/home/user
/etc
/data/app
/boot
$ cat exclude.txt
/data/app/temp
/etc/aide
I have tried using --include-from and --files-from but can't seem to figure out it.
This seems to sync the folders, but not the files:
rsync -av --files-from=include.txt / /destination
Ultimately I want to sync to /destination and have the folder structure look like:
/destination/home/user
/destination/home/user/...
/destination/etc
/destination/etc/...
/destination/data/app
/destination/data/app/...
/destination/boot
/destination/boot/...
Just add -r and --exclude-from options and You should be good to go:
rsync -av -r --files-from=./include.txt --exclude-from=./exclude.txt / /destination/

create a tar.gz of a directory and clean up the directory except a file?

I would like to know if tar (1.15.1, Linux system) already has something like this before creating a script for it.
I would like to create a dir.tar.gz of a given dir/ that contains a specific dir/myfile.gz. I would like the command to do 2 specific things:
Preserve dir/myfile.gz
Delete everything else in dir/ after the dir.tar.gz is complete.
For example:
tree -if .
.
./dir
./dir/dir2
./dir/dir2/file3
./dir/file1
./dir/file2
./dir/myfile.gz
2 directories, 4 files
I would like the result of the tar command to be:
tree -if
.
./dir
./dir/myfile.gz
./dir.tar.gz
1 directory, 2 files
I have read about the --delete and --extract options, but I am not sure how to apply them in one single command. Any ideas?
This should work:
tar -zcvf dir.tar.gz --exclude="*.gz" ./dir/. && find ./dir/. ! -name "*.gz" -exec rm -rf {} +
Test:
$ tree -if .
.
./dir
./dir/dir2
./dir/dir2/file3
./dir/file1
./dir/file2
./dir/myfile.gz
2 directories, 4 files
$ tar -zcvf dir.tar.gz --exclude="*.gz" ./dir/. && find ./dir/. ! -name "*.gz" -exec rm -rf {} +
./dir/./
./dir/./file1
./dir/./dir2/
./dir/./dir2/file3
./dir/./file2
rm: cannot remove `.' or `..'
$ tree -if .
.
./dir
./dir/myfile.gz
./dir.tar.gz
1 directory, 2 files
You may also be interested in tardy which is a tar post-processor (filter), so you can operate (even on the fly) on your tar archive and remove the offending file.

Why does `rsync <...> /path/to/someDir /path/to/otherDir` leave me with `/path/to/otherDir/someDir`, not syncing files from `someDir` into `otherDir`?

Why is my rsync doing that? It's basically just making a copy of the someDir folder inside otherDir. If I run the command again after making changes in /path/to/someDir, rsync will sync all files from /path/to/someDir to /path/to/otherDir/someDir. How do I get all the files inside /path/to/someDir synced to /path/to/otherDir.
This is what the command looks like that I'm excuting:
rsync --stats --compress --recursive --times --perms --links --delete --exclude ".git" --exclude "wp-content/upload" --exclude "wp-content/uploads" --exclude "wp-content/gallery" /path/to/someDir /path/to/otherDir
rsync is one of the few commands that make a distinction between /your/path and /your/path/
When you don't use the trailing backslash you are referring to the directory, while when you use it you are referring to the contents of the directory.
Try
rsync --stats --compress --recursive --times --perms --links --delete --exclude ".git" --exclude "wp-content/upload" --exclude "wp-content/uploads" --exclude "wp-content/gallery" /path/to/someDir/ /path/to/otherDir
That extra trailing slash in /path/to/someDir/ will make the contents of it available in /path/to/otherDir.
BTW: Don't be tempted to use /path/to/someDir/* as was suggested, that will give you problems when you have many files and it won't copy files with names beginning with ..
The /path/to/someDir refers to the folder, someDir, not the files inside.
If you want instead to copy the files out of /path/to/someDir, try this:
rsync... /path/to/someDir/ /path/to/otherDir

Rsync how to include directories but not files?

I have a directory structure and files like this
data/
data/a.txt
data/folder/
data/folder/b.txt
data/folder/folder/
data/folder/folder/c.txt
...
a.txt, b.txt, and c.txt are large files that are computer-generated and renewed frequently. They should NOT be backuped -- but I want to backup the directory structure :
data/
data/folder/
data/folder/folder/
How can I do this with rsync and --exclude-from, without specifying every folder, but something like rsync -a data/* --exclude-from=exclude.rsync "" --onlyfoldersandnotfiles""?
Thanks for help !
rsync -a --include='*/' --exclude='*' source/ destination/
Basically, first include all directories, then exclude all files.
$ rsync -a -f"+ */" -f"- *" source/ destination/
"The two -f arguments mean, respectively, "copy all directories" and then "do not copy anything else"."
Further details: http://psung.blogspot.com/2008/05/copying-directory-trees-with-rsync.html
rsync -a -f"-! */" source/ destination/
Filter rule means "Exclude anything that's not a directory."
If you want to sync everything except one folder but you still want to keep the directory structure of that excluded folder, you should do the following:
$ rsync -a -f"+ /var/log/**/*/" -f"- /var/log/**/*" source/ destination/
See the exclude-list and the rsync command as an example.

BASH copy all files except one

I would like to copy all files out of a dir except for one named Default.png. It seems that there are a number of ways to do this. What seems the most effective to you?
Should be as follows:
cp -r !(Default.png) /dest
If copying to a folder nested in the current folder (called example in the case below) you need to omit that directory also:
cp -r !(Default.png|example) /example
rsync has been my cp/scp replacement for a long time:
rsync -av from/ to/ --exclude=Default.png
-a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
-v, --verbose increase verbosity
Simple, if src/ only contains files:
find src/ ! -name Default.png -exec cp -t dest/ {} +
If src/ has sub-directories, this omits them, but does copy files inside of them:
find src/ -type f ! -name Default.png -exec cp -t dest/ {} +
If src/ has sub-directories, this does not recurse into them:
find src/ -type f -maxdepth 1 ! -name Default.png -exec cp -t dest/ {} +
cp `ls | grep -v Default.png` destdir
I'd just do:
cp srcdir/* destdir/ ; rm destdir/Default.png
unless the files are big. Otherwise use e.g.
find srcdir -type f/ |grep -v Default.png$ |xargs -ILIST cp LIST destdir/
Jan, 2022 Update:
This is the easiest way(It's not complicated).
First, make "temp" folder:
mkdir temp
Second, copy all files from your original folder to "temp" folder:
"-R" flag can copy exactly all files including "Symbolic Links"
cp -R originalFolder/. temp/
Third, remove "Default.png" from "temp" folder:
rm temp/Default.png
Finally, copy all files from "temp" folder to your destination folder:
cp -R temp/. destinationFolder/
In addition, this is the shortest way without "temp" folder:
cp -R originalFolder/!(Default.png) destinationFolder/
Below script worked for me
cp -r `ls -A | grep -v 'skip folder/file name'` destination
# chattr +i /files_to_exclude
# cp source destination
# chattr -i /files_to_exclude
use the shell's expansion parameter with regex
cp /<path>/[^not_to_copy_file]* .
Everything will be copied except for the not_to_copy_file
--
if something is wrong with this. please Specify !

Resources