Shell Script to create destination directory and paste the file - shell

This is what I am trying to do. I have a json file in which I have set of files like test1.txt,
test2.txt,
new/destination/test3.txt,
test4.txt
But when the file is in directory format, I am getting error.
files=["test.txt",
"test2.txt",
"new/destination/test3.txt",
"test4.txt"
]```
Inside a Groovy script I have a sh:
In a for loop I am trying to copy those files to different directory,
`for( file in files) {
sh ("cp -pr source_directory/"+file + " destination_directory/"+file +"| true")
echo 'file to copy ' + file
}`
Result I am looking for :
** destination_directory/test1.txt
destination_directory/test2.txt
destination_directory/new/destination/test3.txt **
I know cp command wont create new directory, is there any way I can solve this?

Related

shell script - before copy files adding a Word to the end

Im trying to copy xml-files from a directory to an other directory.
XML File now = OV_1e4f58d6.xml
After Copy = OV_1e4f58d6_Regression.xml
I have tried with this code but im getting the error "cp: cannot stat ‘–suffix=_REGRESSION’: No such file or directory"
cp --backup=simple –suffix=_Regression /u80/OMS/inputXML/BSL_KUPO_D1/*.xml /u80/OMS/DocumentService/importBSL
Thank you for Help

Bash Mac Terminal to Organize Filestructure

I have 12,000+ files that need to be organized. All the folders are included, however the files are in a flattened filestructure right now.
My folders and files are all named with the path that they should be in. For example, in one directory I have a folder named \textures and another folder named \textures\actors\bear however there is no \textures\actors folder. I am struggling at developing a macro that will take these folders and put them in the correct place that each folder and filename suggests it should be in. I would like to be able to automatically sort these into textures and inside that would be actors and inside that would be bear. However, there are over 12,000 files so I am looking for an automated process that will determine all of this and just do it if possible.
Is there a script that will look at every file or folder name and detect which folder the file or folder should be in in the directory and automatically move them there as well as create any folders that do not exist within the path given when needed?
Thanks
Given a directory structure like this:
$ ls /tmp/stacktest
\textures
\textures\actors\bear
fur.png
\textures\actors\bear\fur2.png
The python script below will turn it into this:
$ ls /tmp/stackdest
textures/actors/bear
fur.png
fur2.png
Python script:
from os import walk
import os
# TODO - Change these to correct locations
dir_path = "/tmp/stacktest"
dest_path = "/tmp/stackdest"
for (dirpath, dirnames, filenames) in walk(dir_path):
# Called for all files, recu`enter code here`rsively
for f in filenames:
# Get the full path to the original file in the file system
file_path = os.path.join(dirpath, f)
# Get the relative path, starting at the root dir
relative_path = os.path.relpath(file_path, dir_path)
# Replace \ with / to make a real file system path
new_rel_path = relative_path.replace("\\", "/")
# Remove a starting "/" if it exists, as it messes with os.path.join
if new_rel_path[0] == "/":
new_rel_path = new_rel_path[1:]
# Prepend the dest path
final_path = os.path.join(dest_path, new_rel_path)
# Make the parent directory
parent_dir = os.path.dirname(final_path)
mkdir_cmd = "mkdir -p '" + parent_dir + "'"
print("Executing: ", mkdir_cmd)
os.system(mkdir_cmd)
# Copy the file to the final path
cp_cmd = "cp '" + file_path + "' '" + final_path + "'"
print("Executing: ", cp_cmd)
os.system(cp_cmd)
The script reads all the files and folders in dir_path and creates a new directory structure under dest_path. Make sure you don't put dest_path inside dir_path.

How to copy files from 1 folder to another where folders names are dynamic using shell script

I am trying to copy files which are in different folder. Folders name change dynamically during run time. So i tried to save it in a variable and tried to copy . Its throwing error. Could u please help me to resolve
current_path : '/tmp/build/bf45481f'
brokerutils='/resource-app/ServiceBrokers/BrokerCommonUtils/*'
broker='/resource-app/ServiceBrokers/AuthServiceBroker_New/'
path_source="$current_path$brokerutils"
path_dest="$current_path$broker"
cp -R "${path_source}${path_dest}"
Path_dest:/tmp/build/bf45481f/resource-app/ServiceBrokers/AuthServiceBroker_New/
Path_source:/tmp/build/bf45481f/resource-app/ServiceBrokers/BrokerCommonUtils/BrokerRoutes.js
/tmp/build/bf45481f/resource-app/ServiceBrokers/BrokerCommonUtils/DALAdapter.js
/tmp/build/bf45481f/resource-app/ServiceBrokers/BrokerCommonUtils/gistdb.js
/tmp/build/bf45481f/resource-app/ServiceBrokers/BrokerCommonUtils/middleLayer
Error : cp: missing destination file operand after
'/tmp/build/bf45481f/resource-app/ServiceBrokers/BrokerCommonUtils/*/tmp/build/bf45481f/resource-app/ServiceBrokers/AuthServiceBroker_New/'
Simple Example for copying files from 1 folder to another where folders names are dynamic using shell script
!/bin/bash
echo "Enter sourc path:"
read sourc
echo "Enter dest path:"
read dest
path_source="$sourc"
path_dest="$dest"
cp -rf $path_source $path_dest
I think there is at less one error : you miss a space between ${path_source} and ${path_dest}.
Try with cp -R "${path_source} ${path_dest}"

copying files into named directories in bash

I have a directory structure like so
/dir01/dir02/files
I want to copy the first file in dir02 onto a separate drive and into a directory with the same name as dir01
I wrote the following script
while [ "${*}" != "" ] ; do
INPUT="${1}"
FOLDER="${INPUT}"/*DPX
TARGET_FOLDER="/Users/user/Desktop/folder"/$(basename "${INPUT}")
for file in "${FOLDER}"; do
echo cp "$file" "${TARGET_FOLDER}"
break 1
done
done
Here INPUT is dir01 , FOLDER is dir02 and TARGET_FOLDER is the new directory with the same name as dir02 I want the file to copy to.
When I run the script it looks for a folder named *DPX in the INPUT path, which doesn't exist. There are many folders in the INPUT directory named *DPX and I want it to pull the first file from all of them.
Try replacing your for with:
for file in "$INPUT"/*DPX/*
Notes:
Your version is looking for a file called *DPX because ${FOLDER} is quoted on the for line.
for f in "$dir" will execute the for loop once, with f=$dir. To look for files under $dir, you need another /*.
Also, you want a shift before the last done.

Get file names with complete path in a single txt file using shell script || windows

Resources:
So I have multiple folders ordered/sorted by date (could be any other parameter as well)
inside each folder there are sub folders named as numbers and inside each folder there is a "single" test.css file (extension is constant name can be different i.e all files are .css).
so for example sample folder structure can be something like this
- project/05.09.2010/00/test.css
- project/05.09.2010/01/test.css
- project/05.09.2010/03/test.css
- project/05.09.2010/09/test.css
- project/07.10.2013/01/test.css
- project/07.10.2013/05/test.css
Requirement: I want to loop through all folders in order of date and folder number and get list of .css file name in a txt file.
for e.g for above case my output should be:
file 'project/05.09.2010/00/test.css'
file 'project/05.09.2010/01/test.css'
file 'project/05.09.2010/03/test.css'
file 'project/05.09.2010/09/test.css'
file 'project/07.10.2013/01/test.css'
file 'project/07.10.2013/05/test.css'
I will be performing certain task on those css files later in the same script.
I need code to run in windows batch file.
I am new to shell scripting and have no idea how to approach this scenario.
As #Stephan stated, dir /s /b "*.css" will work. However, it does not output it into a text file. This should work:
cd "C:\parent directory"
rem set parent directory to the parent directory
dir /s /b "*.css" > "%userprofile%\Desktop\cssfiles.txt
This will find all of the .css files and put their names into a text file on your desktop.

Resources