copying all the .tar and .tar.bz2 file systems from all the sub-directories into another directory - linux

Imagine that i have lots of sub-directory in a sub-directory in a directory I wanted to copy all the .tar and .tar.bz2 extension files from all the sub-directories into another directory.
I used
$find /home/apple/mango -name *.tar -exec cp {} ./kk \;
but it copies only once from a sub directory and stops , it doesn't find files which are in other sub directories or go inside a sub directories and find them.
I want to do it recursively

You may use:
find /home/apple/mango -name '*.tar*' -execdir cp {} /full/path/to/kk \;
Note how name pattern is quoted to avoid shell expansion even before find command executes.
In the absence of quoting *.tar is expanded to some file.tar which is present in current directory and find stop right there because file.tar is not found in sub directories. By quoting glob pattern we make sure that find command gets literal pattern to search the sub directories.

Related

Linux find command explanation

Can someone explain me what does this command do and if I want to try the same thing using git, how should I modify this command?
find . -name CVS -print -exec rm -fr {} \;
This command looks in your current working directory for any directories or files named "CVS" and prints the full path. Then executes a forced recursive removal for each result returned by the find command.
Since there is no filetype present in the name, this command will remove any directory, within your current working directory, named CVS, including all subdirectories and files housed within.

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>/*

How to get a bare, recursive directory listing in Linux, excluding some directories

I need to obtain a recursive directory listing in Linux with only the directory and file name. It needs to include all files including hidden files with the exception of files name “.svn”.
I have tried multiple combinations of the “ls” command and haven’t been able to figure it out. When using “ls –R direname/ grep –v /$” I get a directory heading followed by a colon, which I cannot use.
If I have a directory name test with files and a sub-directory named test2 with files, I need the output to look like the following:
test
test/.filehidden1
test/file2
test/file3.txt
test/test2.log
test/test2/file.hidden1
test/test2/file2.boo
test/test2/file3.boo2
Notice there is no leading forward slash
find . -name .svn -prune -o -print
-prune tells it to not descend into any matching directories.
This should do:
find . ! -path \*.svn\*
This tells find to recursively list all files from . whose pathname does not contains .svn. This is not perfect since it may hide for instance file foo.svnbar.
Something like this:
find DIRNAME ! -name .svn
I need to obtain a recursive directory listing in Linux with only the directory and file name. It needs to include all files including hidden files with the exception of files name “.svn”.
Do you want to get a list of files in a Subversion repository?
This will do just that:
svn ls -R
If not, then you can use find:
find . ! -path '*/.svn/*' ! -name .svn
You mentioned "Notice there is no leading forward slash".
If that's very important, then the find command can be rephrased as:
find * .[^.]* ! -path '.svn/*' ! -name .svn

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.

Resources