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 .
Related
I have a folder named accdb under multiple directories all under one parent directory dist. I want to copy the contents of accdb for all directories while preserving the code structure
I succeeded in making the recursive folder structure with:
cd ~/dist; find . -name "accdb" -type d -exec mkdir -p -- ~/acc_trial/{} \;
But i am failing to copy the contents of accdb. This command just makes the structure until directory accdb.
I tried
find . -name "accdb" -type d -exec mkdir -p -- ~/acc_trial/{} \ && cp -r {} ~/acc_trial/{} \;
I get an error:
find: missing argument to `-exec'
I don't know if this is possible using only a find expression, I'm pretty sure it is not. Besides you must consider that if you have one subfolder named accdb inside one accdb folder you'll probably get an error, that's why in the script that I've made I decided to use rsync:
#!/bin/bash
DEST='/home/corronx/provisional/destination_dir'
#Clean destination directory, PLEASE BE CAREFUL IT MUST BE A REMOVABLE DIRECTORY
rm -rf $DEST/*
FIND='test'
LOOK_PATH='/home/corronx/provisional'
FILES=($(find . -type d -name $FIND))
for ((i=0; i<${#FILES[#]};i++))
do
#Remove first character .
FILES[$i]=${FILES[$i]:1:${#FILES[$i]}}
#Create directories in destination path
mkdir -p $DEST${FILES[$i]}
rsync -aHz --delete ${FILES[$i]:1:${#FILES[$i]}}/ $DEST${FILES[$i]}
echo $i
done
Explanation
First of all I'd recommend using full paths in your script because an rm -rf expression inside an script is pretty dangerous (If you want comment that line and delete destination folder before running script).
DEST= Destination path.
FIND= Subfolder name that your are looking for.
LOOK_PATH= Path where you want to execute find
I create an array called FILES that contain all folders that returns find expression, after that I just create destination directories and run rsync to copy files, I've used rsync because I think it is better in case there is any subdirectory with the same name.
PLEASE BE CAREFUL WITH rm -rf expression, if DEST is not set you'll delete everything in your machine
Can you make a script(bash) for moving all the files with the ending *.512.png to a new folder like res512(will be new branch) (keeping all the subfolders)
for this repo I tried really long but I can't figure it out.
You're not very specific with what you're asking.
If you want to move all files that have the suffix .512.png from within your current directory to a new directory, you can use the following
mkdir res512
cp -r *.512.png res512/
If you want to move all files that have the suffix .512.png from within your directory and all child directories into a new directory, you can use
mkdir res512
for f in $(find -type f -name "*.512.png")
do
cp $f res512/
done
If you want to move all files that have the suffix .512.png including their directory structure into a new directory, you can use
find . -name '*.512.png' -exec cp --parents \{\} res512/ \;
Replace cp with mv if you want to move the files instead of copy them.
I am new to using the command line for operations, so forgive me if this question is obvious. I would like to move all files of a certain type (.aiff) from one directory that contains many sub-directories. The file structure looks like this:
directory
- subdir1
-- sound.aiff
-- other.txt
- subdir2
-- sound2.aiff
-- other2.txt
I've tried using something like cp -R /Users/me/directory/*.aiff /Users/me/newdirectory but I get a "no such file or directory" error. I don't know how to specify that the files I want copied in the subdirectories must be .aiff files.
Try this:
cp -R /Users/me/directory/*/*.aiff /Users/me/newdirectory
But probably the destination /Users/me/newdirectory is missing.
You could verify this by doing:
file /Users/me/newdirectory
If the directory doesn't exist will print an error like:
Users/me/newdirectory: cannot open `/Users/me/newdirectory' (No such file or directory)
Create the directory with:
mkdir /Users/me/newdirectory
Next, try to copy the files again, if you want to move them use mv instead of cp
Another way is to use the command find, for example:
find /Users/me/directory -type f -iname "*.aiff" -exec mv {} /Users/me/newdirectory \;
In this example, the command find is going to search for in directory /Users/me/directory/ only for files -type f that end (case insensitive) in *.aiff for each file found it will execute the command mv exec mv {} /Users/me/newdirectory. The {} is a placeholder.
Before moving you could test the command by just finding the desired types:
find . -iname "*.aiff"
This will search for files within the directory the command is executed, notice the . instead of a /Users/me/directory/
I have a directory that has many folders in it that have sub folders again and then there are some files. Can I write a bash script that copies all the files from the given directory into one single folder? So that I do not have to navigate through ever single folder and copy the content to another folder.
In the topmost dir under which you want the files to be copied:
find . -type f -exec cp {} /some/new/location \;
Finds all the normal files and then copies them to /some/new/location
You could use find to list all files inside the folder:
find ./source -type f
and then use the output as argument for cp, it would look like this:
cp $(find ./source -type f) destination
There would be problem if there are some files within the original directory tree with conflicting name. In that case cp would refuse to copy additional files with the same name with error like:
cp: will not overwrite just-created destination/t22' with./source/test/t2/t22'
To make copies of files with same name, you can use backup option, like this:
cp --backup=numbered $(find ./source -type f) destination
If you want to see what is happening use -v (verbose) option
cp -v --backup=numbered $(find ./source -type f) destination
I need a batch script for unix but I don't know it very well.
I have folder A and his subfolder
A\a1\b\c\file.zip
A\a2\b\c\otherFile.zip
A\a3\b\c\thirdFile.zip
Each zip file contains a xml file and a text file
The script have to do 2 things:
unzip all the zip files that are in all folder named 'c' of all sub
folder of 'A' ; the unzipped files should stay in the same folder in
which was the zip
all the unzipped files that have xml extension have to been renamed
someone can help me?
Thank you very much
You can do like this.
#find the folder 'c' and unzip all zip files
for folder in `find ./A -name c -type d`; do unzip $folder/data.zip -d $foler; done
#find all .xml files and change the extension to .edefg
for file in `find ./A -name *.xml -type f`; do mv "$file" "${file%.xml}.edefg"; done
You must go to all dir's.
Would be something like
find A -type d -name c | while read dir; do
cd ${dir} || continue
unzip -u *.zip
call_rename_xml_function
cd - # not needed in bash where this code is performed in a subshell
done
EDIT: Added -u flag for when the script is called twice.