Copy only files including files in subfolder - shell

I have two root folders with the same structure like that :
Folder1
SubFolder1
File1
File2
SubFolder2
File3
File4
SubFolder3
File5
File6
File7
File8
How can I copy all files in 'Folder1' including files in subfolder of it to another destination name 'Folder2'.
'Folder2' have the same structure with 'Folder1' and all subfolder are already created in 'Folder2'.

A simple
cp -a /path/to/folder1/* /path/to/folder2
will do the trick. The command will check wether a sub folder of folder1 already exists in folder2 (create it if it doesn't), copy the files contained and recursively do that for any sub folders found.
See the cp man page for details (which you can also read locally on the shell by issuing man cp).

Make use of rsync tool
Datasetup: Creating Folder1 and Folder2 with respective subdirs
:~/User> ls -laRt Folder1/
Folder1/:
-rw-r--r-- 1 test test 0 2015-09-29 09:08 f7
-rw-r--r-- 1 test test 0 2015-09-29 09:08 f8
drwxr-xr-x 2 test test 4096 2015-09-29 09:08 sub3
drwxr-xr-x 2 test test 4096 2015-09-29 09:08 sub2
drwxr-xr-x 2 test test 4096 2015-09-29 09:08 sub1
Folder1/sub3:
-rw-r--r-- 1 test test 0 2015-09-29 09:08 f5
-rw-r--r-- 1 test test 0 2015-09-29 09:08 f6
Folder1/sub2:
-rw-r--r-- 1 test test 0 2015-09-29 09:08 f3
-rw-r--r-- 1 test test 0 2015-09-29 09:08 f4
Folder1/sub1:
-rw-r--r-- 1 test test 0 2015-09-29 09:08 f1
-rw-r--r-- 1 test test 0 2015-09-29 09:08 f2
:~/User> ls -laRt Folder2
drwxr-xr-x 2 test test 4096 2015-09-29 09:10 sub1
drwxr-xr-x 2 test test 4096 2015-09-29 09:10 sub2
drwxr-xr-x 2 test test 4096 2015-09-29 09:10 sub3
copy using rsync
:~/User> rsync -avh Folder1/ Folder2/
building file list ... done
./
f7
f8
sub1/
sub1/f1
sub1/f2
sub2/
sub2/f3
sub2/f4
sub3/
sub3/f5
sub3/f6
sent 537 bytes received 220 bytes 1.51K bytes/sec
total size is 0 speedup is 0.00
Verify
:~/User> ls -laRt Folder2
Folder2:
-rw-r--r-- 1 test test 0 2015-09-29 09:08 f7
-rw-r--r-- 1 test test 0 2015-09-29 09:08 f8
drwxr-xr-x 2 test test 4096 2015-09-29 09:08 sub3
drwxr-xr-x 2 test test 4096 2015-09-29 09:08 sub2
drwxr-xr-x 2 test test 4096 2015-09-29 09:08 sub1
Folder2/sub3:
-rw-r--r-- 1 test test 0 2015-09-29 09:08 f5
-rw-r--r-- 1 test test 0 2015-09-29 09:08 f6
Folder2/sub2:
-rw-r--r-- 1 test test 0 2015-09-29 09:08 f3
-rw-r--r-- 1 test test 0 2015-09-29 09:08 f4
Folder2/sub1:
-rw-r--r-- 1 test test 0 2015-09-29 09:08 f1
-rw-r--r-- 1 test test 0 2015-09-29 09:08 f2

Related

SHELL: execution using variable

I write below statements in my .bashrc
alias ls='ls -l'
$LS = 'ls'
I expected $LS execute ls -l in command line prompt because ls already modified to ls -l. but $LS execute ls only ls.
ls execute ls -l because alias as below:
> ls
total 28
-rw-r--r-- 1 chlee chlee 82 8월 30 22:07 '#test.py#'
drwxr-xr-x 2 chlee chlee 4096 8월 21 04:09 exer
-rw-r--r-- 1 chlee chlee 64 8월 30 21:49 test.py
-rw-r--r-- 1 chlee chlee 49 8월 30 21:47 test.py~
drwxr-xr-x 29 chlee chlee 12288 8월 21 02:51 vsdbg
$LS execute only ls as below:
> $LS
'#test.py#' exer test.py test.py~ vsdbg
How can I get the shell to take into account aliases when variable evaluation?
This should work as you expect:
#!/bin/bash
alias ls="ls -l"
LS () {
ls
}
echo trying lower
ls
echo Trying upper
LS
The function is called LS including parses aliases.
# source LS.sh
trying lower
total 4
-rwxr-xr-x 1 root root 89 Sep 19 21:50 LS.sh
Trying upper
total 4
-rwxr-xr-x 1 root root 89 Sep 19 21:50 LS.sh
Jetchisel was pointing you to this in the comments.

Delete A Specific File From Directories Based On FIND Results

I have the following find command that searches all subdirectories and lists those folders that contain a *.RAR AND a *.MKV file.
find -type d -exec sh -c '[ -f "$0"/*.rar ] && [ -f "$0"/*.mkv ]' '{}' \; -print | sort
What I want to do now is to delete the *.MKV file from those directories.
For example, the above command finds FILEA.RAR and FILEB.MKV and lists the directory as DIRECTORY_CHARLIE. I would like to be able to have the above code, also delete the FILEB.MKV file, well delete the found MKV file from each directory that had both file types.
To start, I've created "_TestDir" with the following subfolders:
#useroneserver ~/files/_TestDir $ ls -all
drwxr-xr-x 2 userone userone 50 Feb 27 19:31 Folder01
drwxr-xr-x 2 userone userone 50 Feb 27 19:32 Folder02
drwxr-xr-x 2 userone userone 50 Feb 27 19:32 Folder03
drwxr-xr-x 2 userone userone 50 Feb 27 19:33 Folder04
drwxr-xr-x 2 userone userone 50 Feb 27 19:34 Folder05
Each folder has 2 files, except Folder03, which only has one file.
userone#remoteserver ~/files/_TestDir $ ls Folder01 -all
drwxr-xr-x 2 userone userone 50 Mar 1 20:03 .
drwxr-xr-x 7 userone userone 105 Feb 27 19:30 ..
-rw-r--r-- 1 userone userone 0 Feb 27 19:31 File1.rar
-rw-r--r-- 1 userone userone 0 Feb 27 19:30 FileA.mkv
userone#remoteserver ~/files/_TestDir $ ls Folder02 -all
drwxr-xr-x 2 userone userone 50 Mar 1 20:04 .
drwxr-xr-x 7 userone userone 105 Feb 27 19:30 ..
-rw-r--r-- 1 userone userone 0 Feb 27 19:32 File2.rar
-rw-r--r-- 1 userone userone 0 Feb 27 19:31 FileB.mkv
userone#remoteserver ~/files/_TestDir $ ls Folder03 -all
drwxr-xr-x 2 userone userone 30 Mar 1 20:04 .
drwxr-xr-x 7 userone userone 105 Feb 27 19:30 ..
-rw-r--r-- 1 userone userone 0 Feb 27 19:32 FileC.mkv
userone#remoteserver ~/files/_TestDir $ ls Folder04 -all
drwxr-xr-x 2 userone userone 50 Mar 1 20:04 .
drwxr-xr-x 7 userone userone 105 Feb 27 19:30 ..
-rw-r--r-- 1 userone userone 0 Feb 27 19:33 File4.rar
-rw-r--r-- 1 userone userone 0 Feb 27 19:33 FileD.mkv
userone#remoteserver ~/files/_TestDir $ ls Folder05 -all
drwxr-xr-x 2 userone userone 50 Mar 1 20:05 .
drwxr-xr-x 7 userone userone 105 Feb 27 19:30 ..
-rw-r--r-- 1 userone userone 0 Feb 27 19:34 File5.rar
-rw-r--r-- 1 userone userone 0 Feb 27 19:33 FileE.mkv
When I run the command in the original post, I get this:
userone#remoteserver ~/files/_TestDir $ find -type d -exec sh -c '[ -f "$0"/*.rar ] && [ -f "$0"/*.mkv ]' '{}' \; -print | sort
./Folder01
./Folder02
./Folder04
./Folder05
That is what I expect to see as the results, since folders 1, 2, 4 & 5 have a file of each of the extensions that I am looking for (*.rar & *.mkv), where as folder 3 only has one *.mkv file.
I have not tried to add any delete function since I have no clue where to start.
What I would like to happen is to be able to remove/delete the following
files:
FileA.mkv from Folder01
FileB.mkv from Folder02
FileD.mkv from Folder04
FileE.mkv from Folder05
Nothing gets deleted from Folder03 since it does not have a .RAR AND a .MKV file, it only has the .MKV. Hope this helps clarify.
Thank you for your assistance.
Regards.
You want to use xargs to run rm which will delete the files.
And define the replacement string, using the -I option of xargs.
Using your directory structure, you can do:
find -type d -exec sh -c '[ -f "$0"/*.rar ] && [ -f "$0"/*.mkv ]' '{}' \; -print | sort | xargs -I % sh -c "rm -f %/File?.mkv"
I took your exact find command, and added this:
| xargs -I % sh -c "rm -f %/File?.mkv"
Explanation
when find runs, it will output
./Folder01
./Folder02
./Folder04
./Folder05
Since xargs is used with the -I % option, it will run the command, replacing % with each directory (like in a loop, one by one). You could use another character than %, but avoid wildcard characters.
The command that xargs will run is sh -c "rm -f %/File?.mkv"
It will therefore do the following commands, in succession:
sh -c "rm -f Folder01/File?.mkv"
sh -c "rm -f Folder02/File?.mkv"
sh -c "rm -f Folder04/File?.mkv"
sh -c "rm -f Folder05/File?.mkv"
Obviously, you can adjust as required.

MacOS Command Line - Create folder with files at once

Is it possible to write a command that will create a new directory with name passed as argument 'MyFolder' (for example) and will create four files with the same name (as part):
MyFolder.js
MyFolder.css
MyFolder.test.js
README.md
(using mkdir / touch / echo ...)
Main problem - one line command
This one-liner function should do the work:
$ function mkdir_and_files() { mkdir "${1}"; touch ${1}/${1}.js; touch ${1}/${1}.css; touch ${1}/${1}.test.js; touch ${1}/README.md; }; mkdir_and_files "MyFolder" ;
$ ls -latrh MyFolder/
total 0
drwxrwxrwt 15 root wheel 480B Aug 19 18:58 ..
-rw-r--r-- 1 user wheel 0B Aug 19 18:58 MyFolder.js
-rw-r--r-- 1 user wheel 0B Aug 19 18:58 MyFolder.css
-rw-r--r-- 1 user wheel 0B Aug 19 18:58 MyFolder.test.js
-rw-r--r-- 1 user wheel 0B Aug 19 18:58 README.md
drwxr-xr-x 6 user wheel 192B Aug 19 18:58 .
Try this:
populate_dir() { mkdir "$1"; touch "$1/$1".{js,css,test.js} "$1/README.md"; }
populate_dir MyFolder

BASH / Command Line number of bytes

In this directory when I run
ls -l
it prints the following output:
-rw------- 1 csundl dcsugrad 0 Dec 5 13:51 file3
drwx------ 2 csundl dcsugrad 4096 Dec 5 13:51 Photos
drwx------ 2 csundl dcsugrad 4096 Dec 5 13:51 Pron
drwx------ 2 csunfi dcsugrad 4096 Dec 5 13:51 Spreadsheets
drwx------ 3 csundl dcsugrad 4096 Dec 5 15:12 Stuff
-rwx------ 1 csundl dcsugrad 149 Dec 5 15:08 untitled.sh
The bolded values are the byte sizes (?).
However when I run:
ls -l | wc -c
The total byte size comes up as 340. Why is this?
Thanks.
ls command displays size of the file
but this command:
ls -l | wc -c
counts number of characters in the output of ls command.
To count total size of files in a directory use du command:
du -hs mydir

How to include a library in the path while compiling?

I'm reading this post about go and was trying to compile the source code found here
I downloaded the source code, compiled the first file with make and I can see the object is generated:
$pwd
/Users/oscarryz/code/go/rsc/rosetta/graph
$ls -ltR
total 136
-rw-r--r-- 1 oscarryz staff 61295 Sep 17 16:20 _go_.6
drwxr-xr-x 3 oscarryz staff 102 Sep 17 16:20 _obj
-rw-r--r-- 1 oscarryz staff 126 Sep 17 16:17 Makefile
-rw-r--r-- 1 oscarryz staff 2791 Sep 17 16:17 graph.go
./_obj:
total 0
drwxr-xr-x 3 oscarryz staff 102 Sep 17 16:20 rsc.googlecode.com
./_obj/rsc.googlecode.com:
total 0
drwxr-xr-x 3 oscarryz staff 102 Sep 17 16:20 hg
./_obj/rsc.googlecode.com/hg:
total 0
drwxr-xr-x 3 oscarryz staff 102 Sep 17 16:20 rosetta
./_obj/rsc.googlecode.com/hg/rosetta:
total 136
-rw-r--r-- 1 oscarryz staff 68486 Sep 17 16:20 graph.a
No my question is, how do I refer to that compiled code from the maze directory:
/Users/oscarryz/code/go/rsc/rosetta/maze/maze.go
Whose import declarations are:
import (
"bytes"
"fmt"
"rand"
"time"
"rsc.googlecode.com/hg/rosetta/graph"
)
And right now is failing to compile with the error message:
6g -o _go_.6 maze.go
maze.go:20: can't find import: rsc.googlecode.com/hg/rosetta/graph
make: *** [_go_.6] Error 1
Ok, I found it, wasn't that hard.
6g flags: -I DIR search for packages in DIR
I have to specify the -I option like this:
6g -I ../graph/_obj/ -o _go_.6 maze.go

Resources