I have a dir called 0_400x24_multi_newton20 in the current directory. And I want to copy it as 2_400x24_multi_newton20, 4_400x24_multi_newton20, ....
so I use below code
for((i=2;i<21;i=i+2)); do cp -r "0_400x24_multi_newton20/" "$i_400x24_multi_newton20/"; done
however it warns me that
cp: cannot create directory `/0_400x24_multi_newton20': Permission
denied
On the other hand
cp -r "0_400x24_multi_newton20/" "2_400x24_multi_newton20/"
works
So how to do it correctly?
May by ;
for((i=2;i<21;i=i+2)); do cp -r "0_400x24_multi_newton20/" "${i}_400x24_multi_newton20/"; done
Related
I have write shell script to find the particular filename in the directory and create list file to copy the filenames automatically in that list file. But my script is not working list file is not created automatically. I don't know the issue in my script.
Scripts='/app/file'
SrcFiles='/app/file/Mainfiles'
cd "$SrcFiles"
touch SOURCE.LIST
chmod 777 SOURCE.LIST
cd "$Scripts"
cd "$SrcFiles"
for f in *.csv
do
cp -v "$f" /app/file/Mainfiles/SOURCE.LIST/"${f%.csv}"
done
Please try below
search_dir="/app/file/Mainfiles"
for entry in "$search_dir"/*
do echo "$(basename $entry)" >> "/app/file/Mainfiles/SOURCE.LIST"
done
Im trying to run this script which basically copies an uploaded file to another directory - when I run it, the file gets copied ok but the ownership of the file does not get changed to sales1upload.dba as I expected while it produces the following error on output:
chown: cannot access `test1.txt': No such file or directory
#!/bin/bash
BASE_DIR="/home/sales1upload/upload"
NEW_BASE_DIR="/bbc/prod/today"
current_time=$(date "+%Y.%m.%d-%H.%M.%S")
for file in $(ls ${BASE_DIR});
do
filename=${file}
new_filename=$filename.$current_time
#set user permissions as desired
chown sales1upload.dba "$filename"
cp -prf ${BASE_DIR}/${filename} ${NEW_BASE_DIR}/"moved_files"/$new_filename
cp -prf ${BASE_DIR}/${filename} ${NEW_BASE_DIR}
rm ${BASE_DIR}/${filename}
done
Where am I going wrong with the file ownership in the script?
My quick guess: You're not running this in your Base directory, thus you cannot reference the file without specifying the base in the chmod argument. Change to:
chown sales1upload.dba "${BASE_DIR}/${filename}"
I'd like to add that though mine is the straightforward solution to your issue, getting rid of that ls as the other answers suggest is the way to go here.
You are asking ls to return a list of files in a directory, but they exist relative to that directory, not relative to the current directory.
As pointed out in comments, you should not be using ls for this at all. Fixing the ls to a simple wildcard will also incidentally solve your problem, but now you need to refactor the body of the loop to cope with a full path instead of just a plain file name. (You were already doing the opposite in a couple of places, so this should have been a simple bug to troubleshoot yourself.)
for file in "$BASE_DIR"/*; do
filename=$(basename "$file")
new_filename=$filename.$current_time
chown sales1upload.dba "$file"
cp -prf "$file" "$NEW_BASE_DIR/moved_files/$new_filename"
cp -prf "$file" "$NEW_BASE_DIR"
rm "$file"
done
Find files with find and not with ls. If you use find, you have the correct path. In your example you iterate over the relative path and not the absolute path.
I am writing a script and I want to apply the permissions of a given file to another, given that the owner of both files is the same. The files can be huge, so moving the file contents using
cp dest tmp
rm dest
cp source dest
echo tmp > dest
rm tmp
is not an option.
Does your chmod support the --reference option? It does exactly what you need.
FILE1="$HOME/.bashrc"
FILE2="$HOME/.profile"
FPERM=`stat -c "%a" "$FILE1"`
chmod $FPERM "$FILE2"
PS. If also ownership:
FUID=`stat -c "%U" "$FILE1"`
FGID=`stat -c "%G" "$FILE1"`
chown $FUID:$FGID "$FILE2"
my first question + here we go....
this is a simple script, i had it working before, but now its dead.
when a file comes into a dropbox folder, it appears on the server.
this simple script has inotifywait watching for appends
and doing things i need done with the incoming files, in this case, a simple move
to another folder.
inotifywait -r -m -e attrib /path/to/watched/directory/
while read dir ev file;
do
cp $file ../123
done
I get this error
cp: cannot stat `121013_0005.jpg': No such file or directory
I'm missing something simple, pls school me.
you need a pipe on the first line
you should quotes all variables
So finally :
inotifywait -r -m -e attrib /path/to/watched/directory |
while read dir ev file; do
cp "$file" ../123
done
I have script:
find ./SourceFolder/ -maxdepth 4 -exec cp -R '{}' ./DestFolder/ \;
SourceDir contains also sub-folders.
Problem that in DestFolder not only all tree, but in up level all another levels and files.
How to fix ?
cp -r ./SourceFolder ./DestFolder
code for a simple copy.
cp -r ./SourceFolder ./DestFolder
code for a copy with success result
cp -rv ./SourceFolder ./DestFolder
code for Forcefully if source contains any readonly file it will also copy
cp -rf ./SourceFolder ./DestFolder
for details help
cp --help
also try this cp -r ./dist/* ./out;
this command will copy dist/* files to out dir;
You might find it handy to keep your attributes set
cp -arf ./SourceFolder ./DestFolder