Shell cp: cannot stat no such file or directory - linux

I was trying to use cp to copy files from one directory to another by globing
for files in index/*
do
file=$(echo $files|cut -d'/' -f2)
cp -r "$files" ".target/file"
done
However, cp will give this warning if the directory is empty. I tried 2>/dev/null to mute this message but it did not work. I wonder how I could fix it.

What about this: (not tested)
find /index -maxdepth 1 -type f -exec cp {} .target/ \;
-maxdepth 1 : only look in this directory
-type f : only take the files
-exec cp {} .target/ \; : execute a "file copy" action

Related

Error find: paths must precede expression with Linux command

I am getting below error:
"find: paths must precede expression: zip"
I am trying to execute below:
cd /ohi/oraBase/Extract_Files; BACKUPFILE=OIG_EXTRACT_FILES-$(date); ERR_LOG_FILE=`echo "ODI_Readable_File.log" | cut -f 1 -d '.'`; mkdir "$BACKUPFILE"; find . -maxdepth 1 -type f -not -name "ERR_LOG_FILE".* -exec mv {} "/ohi/oraBase/Extract_Files/$BACKUPFILE" \; zip -r "$BACKUPFILE".zip "$BACKUPFILE";
Below is working fine:
cd /ohi/oraBase/Extract_Files; BACKUPFILE=OIG_EXTRACT_FILES-$(date); ERR_LOG_FILE=`echo "ODI_Readable_File.log" | cut -f 1 -d '.'`; mkdir "$BACKUPFILE"; find . -maxdepth 1 -type f -not -name "ERR_LOG_FILE".* -exec mv {} "/ohi/oraBase/Extract_Files/$BACKUPFILE" \;
Looks like below is culprit:
zip -r "$BACKUPFILE".zip "$BACKUPFILE";
If I execute each command one time, even zip is working.
why error in single line execution?
This is solved. I needed second ; after zip command zip -r "$BACKUPFILE".zip "$BACKUPFILE";
some how it was not seprating zip command with find.

add check file existences function while search and copy file

I have use sudo find all files which match the file type and copy it to a file directory then tee the report.
I would like to include function to check file existence so that file from different folder will not overwrite the wrong file.
Please suggest an idea to fix my function also. Thank you.
sudo find / -iname "*.#fileType" -cp -v {} $directory \; | tee report.txt
function checkFile(){
if [ -f #filename];
then
# add number
fi
}
You can do this using the -exec option of find:
find / -type f -iname "*.#filetype" -exec cp --backup=numbered {} $directory \; -exec echo {} >> report.txt \;
With:
-exec cp --backup=numbered Will execute cp for each found file, with option --backup=numbered, which will prevent overwritting
-exec {} >> report.txt Which will add the copied file name to the report file.

how to find and copy files in a sub directory from parent directory linux

I have several parent folders like GJ1, GJ2 etc. Each of these folders contain three images like GJ11_F.jpg, GJ11_P.jpg. I need to only display all the GJ11_F.jpg files including their respective parent directories.
find . -type f -name "*_F.jpg" | xargs cp -t ~/home/ubuntu/
but the above command will only copy the *_F.jpg files and not their respective parent directories GJ1.
Is xargs not the one im supposed to try?
I have also tried -
find . -name "*_F.jpg" -exec sh -c 'rsync -a "${0%/*}" ~/home/ubuntu/' {} \;
One easy way is to use tar which will deal with the directories automatically:
find . -type f -name "*_F.jpg" -print0 | tar c --null -T - | tar xC ~/home/ubuntu/
And here's a solution with a while loop:
find . -type f -name "*_F.jpg" -print0 |
while IFS= read -r -d '' file; do
mkdir -p ~/home/ubuntu/"$(dirname -- "$file")"
cp -ai -- "$file" ~/home/ubuntu/"$file"
done

How can I move many files without having Argument list too long?

I am trying to move about 700,000 .jpg files from one directory to another in my Ubuntu server. I tried the following:
xargs mv * -t /var/www/html/
and
echo (*.jpg|*.png|*.bmp) | xargs mv -t /var/www/html/
and
echo (*.jpg) | xargs mv -t /var/www/html/
and
find . -name "*.jpg" -print0 | xargs mv * ../
and they all give me the same error: /usr/bin/xargs: Argument list too long
what should I do? Please help me out. Thanks :)
If you use find I would recommend you to use the -exec attribute. So your result should be find . -name "*.jpg" -exec mv {} /home/new/location \;.
However I would recommend to check what the find command returns you, replacing the exec part with: -exec ls -lrt {} \;
Try:
find /path/to/old-directory -type f | xargs -i mv "{}" /path/to/new-directory
You could have tried:
for f in *.jpg do;
mv -tv $f /var/www/html/
done
for f in *.png do;
mv -tv $f /var/www/html/
done
for f in *.bmp do;
mv -tv $f /var/www/html/
done
also, you should carefully read xargs(1); I strongly suspect that
find . -name "*.jpg" -print0 | xargs -n 1000 -I '{}' mv '{}' ../
should work for you
At last, learn more about rename(1). It is probably enough for the job.

Find files in multiple directories taken from list in a file?

FreeBSD 9.2 RELEASE p2
I have a file fromdirs.txt. In this file is a new line separated directory list like so:
/etc
/home
/home/goods/
I need to first find in all directory's files which have names like "good" or contain string "(NODES_'TASK')" and then copy all these files into the directory /tmp.
2.sh file chmod +x and is 755
fromDirs.txt file chmod +x and is 755
This code give me error
IFS=$'\n' read -d '' -r -a dirs < fromDirs.txt
find "${dirs[#]}" -type f \( -name '*good*' -o -exec grep -F "(NODES_'TASK')" {} \; \) -exec cp {} /tmp/ \;
2.sh: cannot open fromDirs.txt : No such file or directory
2.sh: ${dirs[...}: Bad substitution
But File fromDirs.txt exist and 2.sh running from it allocation directory, also i trying to provide full path instead of fromDirs.txt and error the same
This code give me error
FILE=fromDirs.txt
IFS='\n'
while read -r dirs
do
find "$dirs" -type f \( -name '*good*' -o -exec grep -F "(NODES_'TASK')" {} \; \) -exec cp {} /tmp/ \;
done < "$FILE"
2.sh: 6: Syntax error: "done" unexpected (expecting "do")
This code give me error too
FILENAME=fromDirs.txt
awk '{kount++;print kount, $0}
END{print "\nTotal " kount " lines read"}' $FILENAME
2.sh: : not found awk: can't open file fromDirs.txt source line number 2
So how to read file line by line and do what i need?
This works for me
for line in "`cat fromDirs.txt`"; do find "$line" -type f \( -name '*good*' -o -exec grep -F "(NODES_'TASK')" {} \; \) -exec cp {} /tmp/ \;; done

Resources