cp -r without hidden files - linux

I have two directories and one is empty.
The first directory has many sub directories with hidden files. When I cp -r content from first directory to the second one, the hidden files gets copied too. Any solutions to escape them?

You can use rsync instead of cp:
rsync -av --exclude=".*" src dest
This excludes hidden files and directories. If you only want to exclude hidden directories, add a slash to the pattern:
rsync -av --exclude=".*/" src dest

You can do
cp -r SRC_DIR/* DEST_DIR
to exclude all .files and .dirs in the SRC_DIR level, but still it would copy any hidden files in the next level of sub-directories.

rsync has "-C" option
http://rsync.samba.org/ftp/rsync/rsync.html
Example:
rsync -vazC dir1 dir2

I came across the same need when I wanted to copy the files contained in a git repo, but excluding the .git folder, while using git bash.
If you don't have access to rsync, you can replicate the behavior of --exclude=".*" by using the find command along with xargs:
find ./src_dir -type f -not -path '*/.*' | xargs cp --parents -t ./dest_dir
To give more details:
find ./src_dir -type f -not -path '*/.*' will find all files in src_dir excluding the ones where the path contain a . at the beginning of a file or folder.
xargs cp --parents -t ./dest_dir will copy the files found to dest_dir, recreating the folder hierarchy thanks to the --parents argument.
Note: This will not copy empty folders. And will effectively exclude all hidden files and folders from being copied.
Link to relevant doc:
https://linux.die.net/man/1/cp
https://linux.die.net/man/1/find

Related

Copy everything except specific files

How can I copy everything (files and directories(even if they are empty)) from one directory to another, except files ".php", and files with name "config.yml".
I need to do this with single command.
I have tried this one
find ./ -type f ! ( -name "*.php" -o -name "config.yml" ) -exec cp --parents -r -t /my/directory/ "{}" +
It works but if the directory have only files ".php", command will skip the directory and do not copy the empty one, but I need the directory even if it will be empty.
Sorry, I haven't enough reputation to put a comment, because I would like to ask you if the use of "find" is a requisite.
If is not, you can do it easily with the rsync command:
rsync -av --exclude=config.yml --exclude="*php" ORIGINFOLDER/ DESTFOLDER
Just change ORIGINFOLDER and DESTFOLDER for your folders name, and take a look at the man to see the meaning of the options.

copying files from etc ending with digit to test1 directory

I'm new to linux and as an exercice I need to copy the "etc" files that end with a digit from home directory to the test1 directory
(with one command).
I tried this but it dosn't work
find /etc -type f -iname "*[3-9]" -exec cp {} ../test1/ \;
this should work for your home directory files ending with digit
mv `ls . |grep -Eo "^.*[0-9]$"` your-directory
lets says in the current directory you have some files like ofjweifhwef9 or kfhiofeh8 ( files ending with digit)
so ls will list them.
this grep expression "^.*[0-9]$"` will find only files ending with digit. ( because in your home directory system wont allow to have a file like this "/etc/somefile123")
and then mv will move those files to your-directory
note :- if grep cannot find the files ending with number you will see an error ofcourse because mv needs 2 operands but since it wasn't there so error.
mv: missing destination file operand after './your-directory'
It is probably because /etc is a link in the system that you're using, and find doesn't seem to consider it a path until you add an extra / at the end. Try this instead:
find /etc/ -type f -iname "*[3-9]" -exec cp {} ../test1/ \;
Notice the /etc/ instead of /etc. I get the same behavior on my Mac where /etc is a link to another directory.
Of course, also make sure that you have files which names end on a digit under the /etc/ directory tree. I have none in my mac. You should get some files when you run:
find /etc/ -type f -iname "*[3-9]"
If you don't, you don't have any files to copy. You may also try: find /etc/ to see all files under the directory tree.
Finally, you may want to add the option: -depth 1 if you only want to copy the files in the /etc/ directory, as opposed to all the files that match in the directory tree under /etc/.

Copy or move all files in a directory regardles of folder depth or number

Lets say i have a folder named Pictures and I want to move or copy all files out of this folder.
However I also want to move and harvest all of the files who are in sub folders so:
Pictures/1.png
Pictures/yolo/2.png
Pictures/yolo/swag/sand/3.png
Pictures/extra/fire/4.png
I want to move or copy all these files to another folder like results so I get:
results/1.png
results/2.png
results/3.png
results/4.png
Only I have no idea in advance what sub folders will be in the Pictures folder.
How can I accomplish this in bash/shell scripts ?
I also appreciate making it file type neutral so any files are harvested from their directories (not only .png like in my example) and I have no idea what the file name will be (I only used 1...4 because i did not have any idea how to name them).
You can do it like this:
find /absolute/path/to/Pictures -type f -name '*.png' -exec mv -i {} /absolute/path/to/results \;
Another option is to use xargs
find /absolute/path/to/Pictures -name '*.png' | xargs -I files mv files /absolute/path/to/results
You can simply copy all files and subdirectories along with their contents using cp's recursive option:
cp -pr <source_path>/* <destination_path>/
But, moving them recursively is a bit tricky, you will need to create tar files of the subdirectories and move them and then untar the tar files in destination path. As this is a complex process, as a workaround, you can copy the files/directories recursively and then delete the files from original path.
cp -pr <source_path>/* <destination_path>/ && rm -rf <source_path>/*

Rsync make flat copy

I'm trying to write a script that copy all the files of one dir (with subdirs) to the root of another dir.
So Imagine I have this file structure:
/
pic.JPG
PIC5.JPG
FOLDER
pic2.JPG
pic3.JPG
FOLDER2
pic4.JPG
I want all the .JPG files from that directory and copy them over to another destination. But I don't want the directory structure, just the files.
This is what I've got:
"sudo rsync -aq --include '*/' --include '*.JPG' --exclude '*\' /source/picturesRoot/ /destination/flatView/
But it also copies the directories :(
I found this link on stackoverflow:
rsync : Recursively sync all files while ignoring the directory structure
I looked at the solution and didn't see much difference with my command, apart from the * and . in the path. I tried it but it didn't work.
I hope somebody can help me, thanks.
This answer cannot work for you because your pictures are not at the same level in directories. There is no option in rsync to skip the creation of directory structure. In the link you gave, it's working because the user explicitly select source files with *.
You can try something with find and rsync. Find will find files and rsync copy them.
Here is a solution :
find /source/picturesRoot -type f -name "*.JPG" -exec rsync -a {} /destination/flatView/ \;
Be careful, if two files have the same name just one will be in destination directory.

Remove only files and not the directory in linux

I want to know how I can remove all the files in a directory say directory1 contains some 100 files. I just want to remove the files and not the directory.
I know that rmdir directory1 will remove directory1 completely. But I want to only remove all the files inside.
Try this:
rm /path/to/directory1/*
by adding the -r option you can additionally remove contained directories and their content recursively.
find /path/to/directory1 -type f | xargs rm -f
This recursively deletes all normal files in the directory.

Resources