Copying multiple files from one subdirectory to another with wildcards - bash

I'm having issues with wildcard copying on a folder set that I'm working with, and was wondering what the best way to copy these files would be. For instance, my folder structure looks like this:
000
001
001
medium
image1.jpg
original
image1.jpg
thumb
image1.jpg
002
medium
anotherimage2.jpg
original
anotherimage2.jpg
thumb
anotherimage2.jpg
003
medium
someimage3.jpg
original
someimage3.jpg
thumb
someimage3.jpg
002
001
medium
whome_anotherimage_00002.jpg
original
whome_anotherimage_00002.jpg
thumb
whome_anotherimage_00002.jpg
002
medium
crapnotthisagain_067.jpg
original
crapnotthisagain_067.jpg
thumb
crapnotthisagain_067.jpg
What I need to do is have a bash/zsh script, or some command that will recursively move the image from the original folder in each directory to the thumb directory, overwriting the thumb image.
So far, using find or cp is throwing me mixed results. I got closer with
find 000/*/*/original/ -type f -name '*.jpg' -exec cp '{}' 000/*/*/thumb/ ';'
but that only copies the files to the last thumb directory that it finds. Is there a better way to script this?
Update:
Running the following:
find 000000/*/*/original/ -type f -name '*.jpg' -exec cp -t 000000/*/*/thumb/ '{}' +
throws me this on a mac
cp: illegal option -- t
usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file target_file
cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file ... target_directory
cp: illegal option -- t
usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file target_file
cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file ... target_directory
cp: illegal option -- t
usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file target_file
cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file ... target_directory
cp: illegal option -- t
usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file target_file
cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file ... target_directory
cp: illegal option -- t
usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file target_file
cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file ... target_directory
cp: illegal option -- t
usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file target_file
cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file ... target_directory
and on ubuntu: (and doesn't copy anything)
cp: omitting directory ‘000000/001/998/thumb/’
cp: omitting directory ‘000000/001/999/thumb/’

How about looping over the results from find, then use substring replacement to change "original" to "thumb" in the variable:
for i in $(find 000/*/*/original -type f -name '*.jpg'); do cp ${i} ${i/original/thumb}; done

Related

illegal option -- ? when copying

I have just started to work on a Imac macOS HighSierra and when trying to copy some files I keep getting the error:
I just made a test directory to clarify the issue:
The directory test contains:
file1.txt file2.txt folder1
Using the following command in the parent directory:
cp -R test/ .
I get this error:
cp: illegal option -- ?
usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file target_file
cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file ... target_directory
According to manual page for cp, the -R argument means:
If source_file designates a directory, cp copies the directory and the entire subtree connected at that point. If the source_file ends in a /, the contents of the
directory are copied rather than the directory itself.
Any suggestions as to why I get the error ?
I have bash version version 3.2.57(1)-release
----UPDATE---
According to comments to this issue, I provide some additional information:
which cp returns /bin/cp
echo cp -R test/ .returns cp -R test/ .
set | grep cp returns nothing
cp --version and cp --help returns cp: illegal option -- ? usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file target_file cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file ... target_directory
man cp returns the manual page but it doesn't seem to depict anything about the version except BSD February 23, 2005 BSD
For Mac OS, change the cp commands as below examples
Ex 1.
"copy:schemas": "cp --parents schematics/*/schema.json ../../dist/my-lib/",
as
"copy:schemas": "find schematics -type f -name 'schema.json' -exec rsync -R {} ../../dist/my-lib/ \\;",
Ex 2.
copy:files": "cp --parents -p schematics/*/files/** ../../dist/my-lib/",
as
"copy:files": "find schematics/*/files/** -exec rsync -R {} ../../dist/my-lib/ \\;",
Hope this helps 😊.
Does this solve your issue:
cp -R test/* .
or:
cp -R test/* ./
By adding the asterisk, you mention that you are copying files, not a directory.

move all files from folder A to B whenever a certain string is present

I am trying to move files from folder A to folder B whenever a file contains a certain string:
grep -Rli '22/05/2018' ads/ | awk -F "//" '{print $2}' | xargs cp $0 projection/$1
cp: illegal option -- b
usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file target_file
cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file ... target_directory
how to fix this?
Following may help you here.
grep -L -Z -r 'your_string_to_be_checked' . | xargs -0 -I{} mv {} target_directory/
Explanation:
grep -L: Means leave those files which are NOT having that specific string which we are searching for.
-Z: means output should have \0 in file names so space is not being used as delimiter.
-I{} mv {} new_directory/ means replace {} with file names and make it like mv filename new_directory and run it to move the files to new place.
I am using mv here you could use cp too here.
Not sure why you're using awk.
Solution:
grep -lir '22/05/2018' ~/ads/* | xargs cp -t $DEST_FOLDER
Source
Once you have the list of filenames you want to copy you could just use xargs -I:
$ cat listOfFileNames | xargs -I{} cp {} $destFolder
the -I option gives a name to the list of arguments, allowing you to place them wherever you want.

How do I copy a specific folder into the root folder using UNIX?

I am trying to copy the contents of a folder called exampleSite into my current folder.
My command is like this:
cp themes/goa/exampleSite/* . -r
however i'm getting the following error
cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file target_file cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file ... target_directory
Any suggestions?
The correct way to use copy command in unix is -
cp -r themes/goa/exampleSite/* .
where, cp is the <command>
themes/goa/exampleSite/* is <1st arugment> i.e. SOURCE
. is <2nd argument> i.e. DESTINATION
Whenever a command you run, fails unexpectedly immediately due to some syntax error. Please run
man <command>
This will show you the right syntax!
Hope it helps..

The command cp -r on the terminal

How could I use cp -r command to copy more directories? For example, I'd to copy awesome.txt, neat.txt in the folder something with the command cp -r awesome.txt neat.txt something, but I have an error.
Error :
usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file target_file
cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file ... target_directory
Thanks!
There are several ways you could achieve this. The easiest I have seen is to use the following.
cp /home/usr/dir/{file1,file2,file3,file4} /home/usr/destination/
The syntax uses the cp command followed by the path to the directory the desired files are located in with all the files you wish to copy wrapped in brackets and separated by commas.
Make sure to note that there are no spaces between the files. The last part of the command, /home/usr/destination/, is the directory you wish to copy the files into.
or if the all the files have the same prefix but different endings you could do something like this:
cp /home/usr/dir/file{1..4} ./
Where file1,file2,file3 and file4 would be copied.

cp filename* backup/* (Bash)

I want to copy all files that begin with a string to a folder in the same directory. I've tried cp foo* backup/foo* but get
usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file target_file
cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file ... target_directory
How can I copy accomplish this?
cp foo* backup/
Just specify the target directory. This isn't a DOS command window.

Resources