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"
Related
I have folder structure like this:
/home/
/folder1/
/backup/
/folder2/
/backup/
/folder3/
/folder4/
/backup/
/folder5/
(As you can see, no all directories "folder" have a directory "backup")
I need to copy the script "checker.php" to all "backup" directories only.
"checker.php" is at:
/home/checker.php
I am using this command:
cp /home/checker.php /home/*/backup/checker.php
But it is not working. Please help.
The cp command doesn't allow multiple destination directories.
A way forward is to loop through the folders:
for d in /home/*/backup; do
cp /home/checker.php "$d"
done
I have a set of banners that include .jpg and .psd. I need to create folder for them and move them into it.
example:
Banner-A.jpg
Banner-A.psd
Banner-B.jpg
Banner-B.psd
Banner-C.jpg
Banner-C.psd
Create folder and move them:
Banner-A/Banner-A.jpg Banner-A.psd
Banner-B/Banner-B.jpg Banner-B.psd
Banner-C/Banner-C.jpg Banner-C.psd
I manage to find a script here that work for the first part but I can't get the .psd to move as well.
for f in "$#"; do
cd "$f"
for file in *.jpg; do
folder=$(basename "$file" ".jpg")
mkdir -p "$folder" && mv "$file" "$folder"
done
done
Change your mv command to use the * wildcard as follows:
for file in *.jpg; do
folder=$(basename "$file" ".jpg")
mkdir -p "${folder}" && mv "${folder}".* "${folder}"
done
Make sure the .* is outside the quotes and it should work.
Example script:
#!/bin/bash
set -uo pipefail
doWork() {
dir="${1}"
cd "${dir}" || return
for file in *.jpg; do
folder=$(basename "$file" ".jpg")
mkdir -p "${folder}" && mv "${folder}".* "${folder}"
done
}
doWork "$#"
Example data directory: (before executing script)
$ ls data
Banner-A.jpg Banner-A.psd Banner-B.jpg Banner-B.psd Banner-C.jpg Banner-C.psd
Run script:
./script.sh ./data
data directory after script:
$ ls data
Banner-A Banner-B Banner-C
data directory subdirectories:
$ ls data/Banner-*
data/Banner-A:
Banner-A.jpg Banner-A.psd
data/Banner-B:
Banner-B.jpg Banner-B.psd
data/Banner-C:
Banner-C.jpg Banner-C.psd
I'd probably just use
for f in Banner*.jpg Banner*.psd; do
mkdir -p "${f%.???}/"
mv "$f" "${f%.???}/"
done
It will execute the mkdir -p for the *.psg files after the *.jpg has already created the folder, but covers the odd cases where there might be one of the files but not the other.
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
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
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