If I have a directory "test" and inside a directory "new" and I want to copy all files from "new" to test(my current directory).
I tried: cp -r new . because I know dot is like current directory but it didn't work.
Assuming you are in "test":
cp -r new/* .
To test:
mkdir test
cd test
mkdir new
touch new/1
touch new/2
touch new/3
cp -r new/* .
Output:
$ ls
1 2 3 new
This will do it:
cp -rf present/directory /desire/directory
Related
I work with high sierra and I have the following shell script
TOMCAT_PATH=/usr/local/Cellar/tomcat/9.0.12/
cd ./webapp/inventory/WEB-INF
rm -r -v classes
mkdir classes
cp -R ../../../classes ./classes
cd ..
jar -cvf inventory.war WEB-INF
cp inventory.war $TOMCAT_PATH/webapps
cd ../..
How can I execute the command
cd ./webapp/inventory/WEB-INF
After this cd the directory doesn't change
The current directory
/Users/carlotavina/Documents/carlota/irvine/javaenterprisedevelopment/curso6-webservices/week2/assignment/HW-2_Setup
In this directory I have subdirectory webapp/inventory. I want to move to this directory
I am trying to copy every HTML file from an src folder to a dist folder. However, I should like to preserve the original folder structure and if the dist folder does not exist I should like to create a new one.
Create the folder if it does not exist:
[ -d _dist/ ] || mkdir _dist/
Copy every file:
cp -R _src/**/*.html _dist/
Together:
[ -d _dist/ ] || mkdir _dist/ && cp -R _src/**/*.html _dist/
However, if I use ** only the files inside a folder will get copied and if I remove the ** only the root files will get copied. Is this even accomplishable?
find _src -type f -name "*.html" -exec cp -t _dist --parents {} ";"
cp -t : target directory
--parents : append parents dir to target
This will omit empty (no html-files) dirs. But if you need them, repeat without -name "*.html" but with -type d.
In the case you don't have a version of bash with the --parents option, cpio is awesome.
[ -d _dist/ ] || cd _src && find . -name '*.html' | cpio -pdm _dist && mv _dist ..
This would recursively copy all html files into _dist while maintaining the directory structures.
↳ GNU cpio manual - GNU Project
I have a folder, A, that contains around 100 subfolders, Asub1,Asub2, etc. I am trying to go into each Asub folder, and copy two files back to the A folder. I have the following code:
for D in `find . -type d`
do
cd $D
cp log_* ../
cp *.pdf ../
cd ../
done
However this result in the following error for each sub folder:
cp: cannot stat `log_*': No such file or directory
cp: cannot stat `*.pdf': No such file or directory
Could someone please tell me where I am going wrong in my code?
A simpler way is to cd into A folder (cd A) and then do:
cp */log_* */*.pdf .
I want to delete a subdirectory which can be in any of the directories using shell script
For eg
The main directory has 3 directories a , b and c and the test folder can be in any of the 3 directories ie a , b, c. so now i want to delete the test directory.So how can we do this
From within main directory:
find . -type d -name 'test' -exec rm -rf {} \;
You have different options to do it but i like use the globstar:
rm -r **/subfolder
Full example:
$ cd /tmp
$ mkdir foo
$ cd foo/
$ mkdir -p bar/zzz
$ mkdir -p bar/aaa
$ mkdir -p bar/bbb
$ mkdir -p xxx/aaa
$ mkdir -p xxx/ccc
$ mkdir -p xxx/ddd
$ rm -r **/aaa
$ ls
bar xxx
You can try to find and then delete it like this:
find . -name test -type d -print0|xargs -0 rm -r --
using find:
find -type d -a -name test
will list all directories with name test, then you can
find -type d -a -name test|xargs rm -r
to remove
If your directories are so similar, you don't need a complex find pipeline, you can use pathname expansion directly:
$ rm -r [abc]/test
Basically I just want to tar all the files in a directory, but not get all the parent directories in the archive.
I've tried -C, but I guess I'm not using it right.
tar -cjf archive.tar.bz2 -C /var/some/log/path ./*
This results in tar trying to add all the files in the CWD. Using the full path as last argument doesn't prevent the dirs from being added.
Seems simple enough, but can't figure it out. Somehow tar does not tar ./* as being relative to -C, although it should change to that dir.
Help appreciated.
The parent directory (/var/some/log) is included, since /var/some/log/path/.. is included when you do ./*. Try just doing
tar -cjf archive.tar.bz2 -C /var/some/log/path .
Test run:
$ find tmp/some_files
tmp/some_files
tmp/some_files/dir1
tmp/some_files/dir1/dir1file
tmp/some_files/hello
tmp/some_files/world
tmp/some_files/dir2
tmp/some_files/dir2/dir2file
$ tar -cvjf archive.tar.bz2 -C tmp/some_files/ .
./
./dir1/
./dir1/dir1file
./hello
./world
./dir2/
./dir2/dir2file
$ cd tmp/unpacked
/tmp/unpacked$ mv /home/aioobe/archive.tar.bz2 .
/tmp/unpacked$ tar -xvjf archive.tar.bz2
./
./dir1/
./dir1/dir1file
./hello
./world
./dir2/
./dir2/dir2file
/tmp/unpacked$ ls
archive.tar.bz2 dir1 dir2 hello world
/tmp/unpacked$
There's a much easier way to do this:
cd down to the directory you wish to be top level, i.e...
cd /var/lib/mysql
Remove parent directories from your tar command
/var/lib/mysql/DATABASE_NAME becomes just DATABASE_NAME
More details can be found in this blog writeup.