Copying file one location to another [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 3 years ago.
Improve this question
below is mentioned code in shell script
SOURCE="/my/folder"
DESTINATION="/my/destination"
cp -r "$SOURCE/subdir/"* "$DESTINATION/another_sub/"
code is giving me an error
/my/folder: no such a file or directory
what is wrong with this code. I checked for folders also if I put directly on the terminal it is working but using shell script it is not working

/my/folder: no such a file or directory
This error indicates that it can't find that folder from root /.
Try
SOURCE="./my/folder"
DESTINATION="./my/destination"
if the script is run in the same directory as the folders. Or use
SOURCE="~/my/folder"
DESTINATION="~/my/destination"
if the folders are located off your user's home folder.

How to Copy a file from one Source location to another Destination location in Linux Operating System.
Follow below code & command as per your Source to the Destination location.
ubuntu#staging-docker:~/copyfile$ ls
copy.sh destination_dir source_dir
ubuntu#staging-docker:~/copyfile$ tree
.
├── copy.sh
├── destination_dir
└── source_dir
└── source_file.txt
2 directories, 2 files
ubuntu#staging-docker:~/copyfile$ cat copy.sh
#!/bin/bash
source_location='/home/ubuntu/copyfile/source_dir/source_file.txt'
Destination_location='/home/ubuntu/copyfile/destination_dir'
`cp -r $source_location $Destination_location`
ubuntu#staging-docker:~/copyfile$ sh copy.sh
ubuntu#staging-docker:~/copyfile$ tree
.
├── copy.sh
├── destination_dir
│   └── source_file.txt
└── source_dir
└── source_file.txt
2 directories, 3 files
ubuntu#staging-docker:~/copyfile$
ubuntu#staging-docker:~/copyfile$ mkdir source_dir
ubuntu#staging-docker:~/copyfile$ mkdir destination_dir
ubuntu#staging-docker:~/copyfile$ ls
destination_dir source_dir
ubuntu#staging-docker:~/copyfile$ cat > source_dir/source_file.txt
This is a source file
ubuntu#staging-docker:~/copyfile$ tree
.
├── destination_dir
└── source_dir
└── source_file.txt
2 directories, 1 file
ubuntu#staging-docker:~/copyfile$ cp source_dir/source_file.txt destination_dir/.
ubuntu#staging-docker:~/copyfile$ tree
.
├── destination_dir
│   └── source_file.txt
└── source_dir
└── source_file.txt
2 directories, 2 files
ubuntu#staging-docker:~/copyfile$

Related

Script to move files from sub directories to root folder [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I have the following directory and sub directories structure:
eva tree
.
├── 1061
│   └── 2022
│   └── 09
│   └── 21
│   └── a0e51f58-5057-4002-b4c4-d3fb870e9b3a.json
├── 1769
│   └── 2022
│   └── 08
│   └── 30
│   └── e36d8e21-5184-489f-89b5-eb1fd5eba5f6.json
├── 1991
│   └── 2022
│   └── 09
│   └── 16
│   └── 1d0a4162-7e66-44c8-8b61-f3bc5dbdb107.json
I need to move all .json files to root folder eva.
Expected output:
.
│a0e51f58-5057-4002-b4c4-d3fb870e9b3a.json
│e36d8e21-5184-489f-89b5-eb1fd5eba5f6.json
│1d0a4162-7e66-44c8-8b61-f3bc5dbdb107.json
How can I do it using bash?
You can use find . -type f -name '*.json' -exec mv {} . \;
find . searches across the current directory;
-type f all objects of type f (aka file);
-name '*.json' file names that ends with the .json;
-exec [COMMAND]\; for each file found, run [COMMAND];
mv {} ., the curly brackets {} represent the found file, the command mves it to the current directory (.)

Copy multiple directories to another multiple directories from Linux shell [duplicate]

This question already has answers here:
How to copy a file to multiple directories using the gnu cp command
(22 answers)
Closed 9 months ago.
I want to copy several directories to another directories. How do I do it from the shell command prompt? for example:
Project
├── directory1
│ └── files1
├── directory2
│ └── files2
└── directory3
└── files3
to :
Project
├── directory1
│ └── files1
├── directory2
│ └── files2
├── directory3
│ └── files3
├── directory1.copy
│ └── files1
├── directory2.copy
│ └── files2
└── directory3.copy
└── files3
tried this:
mkdir directory{1..3}.copy
cp -r directory{1..3} directory{1..3}.copy
but all directories (and files inside) copy in directory3.copy
Indeed, cp just accepts all arguments before the first as sources and the last one as destination. If you want to copy to multiple places, you need a loop.
for dir in ./*/. # or for dir in directory{1..3}
do
cp -r "$dir" "$dir.copy"
done

Is it possible to compress files without keeping the structure in Linux? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
I have the following directory structure:
A0
├── A1
│   ├── A1_B1
│   │   ├── A1_B1_1.docx
│   │   ├── A1_B2_2.pptx
│   ├── A1_B2
│   │   └── A1_B2_C1
│   │   ├── A1_B2_C1_D1
│   │   │   ├── A1_B2_C1_D1_1.docx
│   │   │   └── A1_B2_C1_D1_2.docx
│   │   └── A1_B2_C1.xlsx
├── A2
└── A0.txt
I want to create a .7z file that will contain only the files. I don't want to keep the folders. I have tried this answer and this answer but they don't work in Linux.
Is it possible to do it with 7z or I should extract files to a single directory first and then compress.
If for some reason the answers you reference to don't work try this instead.
Create a directory
mkdir flat_dir
Link all files from the desired folder recursively in flat_dir, for me the desired folder was cpptest.
for full_path_file in $(find ../cpptest -type f)
do
echo "$full_path_file"
filename=$(echo "$full_path_file" | rev | cut -d '/' -f 1 | rev)
echo "$filename"
ln -s -T "$full_path_file" "$filename"
done
Zip the files
7z a test.zip -l ~/flat_dir

How to extract all .tgz files in subdirectories? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 3 years ago.
Improve this question
I have a list of *tgz files in a ./main/ directory such as
$ tree -L 3
.
├── 191009-Grace_587_8G_R2
│   └── Grace_587_8G_R2
│   └── output.tgz
├── 191009-Grace_V0G_R2
│   └── Grace_V0G_R2
│   └── output.tgz
├── 191009-Grace_V8G_R2
│   └── Grace_V8G_R2
│   └── output.tgz
├── 191014-Grace_587_0G_R2
│   └── Grace_587_0G_R2
│   └── output.tgz
├── 191014-Grace_587_8G_R2
│   └── Grace_587_8G_R2
│   └── output.tgz
├── 191014-Grace_V0G_R2
│   └── Grace_V0G_R2
│   └── output.tgz
└── 191014-Grace_V8G_R2
└── Grace_V8G_R2
└── output.tgz
I am wondering how to extract them all together to the directory containing them.
Using tar's -C / --directory option:
-C, --directory=DIR
Change to DIR before performing any operations. This option is order-sensitive, i.e. it affects all options that follow.
for i in *-Grace_*/Grace_*/output.tgz; do
tar xzf "$i" --directory="${i%/*}"
done
The parameter expansion ${i%/*} removes the filename from the path (like the dirname command). To extract the files to the main directory, remove the --directory option.
Using find with the -execdir option:
find . -type f -name 'output.tgz' -execdir tar xfz {} +

How to find files which contain specific string in directory using command "find"? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
This is my directory structure.
dirOut
├── dirIn1
│   ├── temp1
│   └── temp2
├── dirIn2
└── dirIn3
├── temp1
├── temp2
├── temp3
└── temp4
dir is directory and temp is file.
I want to find files which contain specific string "Hello".
How do I use command "find" to find.
Use grep, not a find, when you find files base on the content.
grep -lr Hello .
-l : Normally grep print matching lines, with -l option, it just print the matched filenames.
-r : recursively find files under the directory.
find dirOut -type f -exec grep -l Hello {} +
The -l option tells grep to just list the filename if it finds a match, rather than showing all the matching lines.
You could also do it using the -R option to grep to search a directory recursively, rather than using find.
grep -R -l Hello dirOut
find . -type f -name \* -exec grep -l "hello" {} \;
Execute this command while in dirOut.

Resources