Mac bash copy all files of one dir to another dir - bash

I have a folder which contains two dirs: build and copy-build.
I need that through bash copy the files that are inside copy-build in build.
If these files already exist it must overwrite them.
I tried with:
cp -r ./SourceFolder ./DestFolder
But the SourceFolder folder is copied into DestFolder, instead I need the SourceFolder files to be copied into DestFolder.
Can you give me a hand?

Assuming there are no subdirectories in SourceDir, you should try
cp ./SourceFolder/* ./DestFolder
See the manual for more info.

Use the -R option instead of -r, and explicitly include a / in the name of the source directory.
cp -R ./SourceFolder/ ./DestFolder
From the description of the -R option:
If the source_file
ends in a /, the contents of the directory are copied rather than
the directory itself.
From the compatibility section of man cp:
Historic versions of the cp utility had a -r option. This implementation
supports that option; however, its use is strongly discouraged, as it
does not correctly copy special files, symbolic links, or fifo's.

Related

Copy whole directory but exclude all folders and subfolders with certain name

I'm not allowed to use rsync on the cluster I'm working on so I need to use cp. I want to copy a large directory including all files and subfolders etc. but without any folders that have the name "outdir".
I tried cp -r -v ./!(outdir) ../target-directory/
but it still copies all folders and contents in deeper directories with the name outdir. It only included the outdir folders in the highest directory.
I also tried cp -r ./*/!(outdir) ../target-directory/ but that one copied all files into the folder without keeping any hirarchy or folders etc.
I also tried certain find commands but it didn't work, but maybe I was just doing something stupid. I'm a beginner with bash so if you could explain your answer and what the flags etc. do that would really be helpfull, I've been trying forever now, on what I think shouldn't be that hard to do.
Instead of cp, you can use tar with option --exclude to control what you want copied or not.
The full command is:
tar --exclude="outdir" -cvpf - . | (cd TARGET_DIRECTORY; tar -xpf -)
So any path that contains the "outdir" pattern will be excluded.
Without the --exclude option, it will copy the entire structure of your current directory under TARGET_DIRECTORY.
You can replace the . in the first tar by your desired source directory.

Copy command in Mac Terminal fails with "No such file or directory"

This should be straightforward, but I'm getting weird results.
I have a folder with subfolders containing ~4000 files. I'm trying to copy just the files of a certain filetype to another folder, while preserving the subfolder hierarchy.
Command:
cp -R /Users/Steve/Desktop/Hardscapes/*.LOB /Users/Steve/Desktop/Temp
fails with the message:
"/Users/Steve/Desktop/Hardscapes/*.LOB: No such file or directory".
I created the command by typing cp -R then dragging the source folder to the terminal window, adding *.LOB after the /, and dragging the destination folder to the terminal window.
Troubleshooting:
replacing *.LOB with *.* gives the same error.
cp -R /Users/Steve/Desktop/Hardscapes/ /Users/Steve/Desktop/Temp copies the entire Hardscapes folder to Temp, with all its subfolders and files.
Thanks for your help and suggestions.
EDIT: The folder Hardscapes contains only other folders. If I run the command above using one of those folders as the source, the contents are copied faithfully. The folder Hardscapes itself contains no .LOB files - they're only in the subfolders.
So maybe that's the issue, cp can't find any files corresponding to Hardscapes/*.LOB? But I thought the -R switch was supposed to tell it to look through all the subfolders.
Next I put a file named Test.LOB in the Hardscapes folder. The command above copies only that file and none of the subfolders. It looks like the -R switch is not doing its job. Do I have the syntax right?
Try this:
rsync -a --prune-empty-dirs --include '*/' --include '*.LOB' --exclude '*' /Users/Steve/Desktop/Hardscapes/ /Users/Steve/Desktop/Temp
As you already mentioned, directory Hardscapes itself contains no .LOB files. That's why your mask /Users/Steve/Desktop/Hardscapes/*.LOB results in matching no files at all.

cp command in macOS doesn't create a new folder if it doesn't exist

cp -R /Desktop/One.java /Desktop/Nine
The folder Nine is not created. Instead, a new file Nine is created without any extension.
If cp is used with -R, then the target (/Desktop/Nine) is created with the same file type as the source (regular file). If the source is a directory (it isn't), the full directory hierarchy will be copied.
In your case, the source is a single regular file, in which case cp -R will act identically as cp (with no -R).
If you want to copy the file /Desktop/One.java into a new directory /Desktop/Nine:
mkdir /Desktop/Nine
cp /Desktop/One.java /Desktop/Nine

Bash: duplicate + rename folder

Suppose I have a folder named my_folder_old in /path/to/folder, how can I create a duplicate named my_folder_new in the same directory?
EDIT
Moreover if my_folder_new already exists, my_folder_old is created inside the first and not substituted. Why is this happening?
Tutorial copy files, folder link: link
Manual cp command : Link
cp -frp /path/to/folder/my_folder_old -T /path/to/folder/my_folder_new
-f, --force
if an existing destination file cannot be opened, remove it
and try again (this option is ignored when the -n option is
also used)
-p same as --preserve=mode,ownership,timestamps
-R, -r, --recursive
copy directories recursively
-T, --no-target-directory
treat DEST as a normal file
Though if my_folder_new already exists, my_folder_old is created inside the first and not substituted. Why is this happening?
The reason why is this happening because, my_folder_new already created. Doing same cp command it will see as new path, /path/to/folder/my_folder_new/
I was dealing with this same issue, was going crazy ahaha, I tried cp -frp but did not work, so, before of going to do cp just remove the existing folder using rm, see below more info about this:
Remove Directory Linux
If a directory or a file within the directory is write-protected, you will be prompted to confirm the deletion. To remove a directory without being prompted, use the -f option:
rm -rf dir1

zip all files and folders recursively in bash

I am working on a project, where compilation of the project involves, zipping up various files and folders and subfolders (html/css/js) selectively. Working on the windows platform, and I could continue to just use the CTRL+A and then SHIFT-click to unselect, but it does get a little tedious. I am working with cygwin, so I was wondering if it is possible to issue a command to zip selected files/folders recursively whilst excluding others, in one command? I already have zip command installed, but I seem to be zipping up the current zip file too and the .svn file too.
I would like this to be incorporated into a shell script if possible, so the simpler the better.
After reading the man pages, I think the solution that I was looking for is as follws:
needs to recurse directories (-r),
needs to exclude certail files/directories (-x)
It works in the current directory, but the . can be replaced with the path of any directory
zip -x directories_to_exclude -r codebase_latest.zip .
I have incorporated this into a short shell script that deletes files, tidy up some code, and then zips up all of the files as needed.
You should read man page of zip command:
-R
--recurse-patterns
Travel the directory structure recursively starting at the current directory; for example:
zip -R foo "*.c"
In this case, all the files matching *.c in the tree starting at the current directory are stored into a zip archive named foo.zip. Note that *.c will match
file.c, a/file.c and a/b/.c. More than one pattern can be listed as separate arguments. Note for PKZIP users: the equivalent command is
pkzip -rP foo *.c
Patterns are relative file paths as they appear in the archive, or will after zipping, and can have optional wildcards in them. For example, given the cur‐
rent directory is foo and under it are directories foo1 and foo2 and in foo1 is the file bar.c,
zip -R foo/*
will zip up foo, foo/foo1, foo/foo1/bar.c, and foo/foo2.
zip -R */bar.c
will zip up foo/foo1/bar.c. See the note for -r on escaping wildcards.
You can also have a look HERE

Resources