renaming files in different folders - bash

i have 1000 folders and each folders contain 5 files with same name that i want to copy and rename in different name.
For example:
My folders path
/home/yuan/data/foldera1/*
/home/yuan/data/foldera2/*
/home/yuan/data/foldera3/*
/home/yuan/data/foldera4/*
.........................
/home/yuan/data/foldera1000/*
And the files i want to rename is
data1.ax.ze to data.a1.z1
datay.ax.z1 to data.a2.z2
dataw.ac.zt to data.a3.z3
data4.an.z5 to data.a4.z4
datax.aa.zq to data.a5.z5
i tried script below: but it doesnot rename files inside each folder
for file in /home/yuan/data/foldera?/*
do
cp data1.ax.ze data.a1.z1
cp datay.ax.z1 data.a2.z2
cp dataw.ac.zt data.a3.z3
cp data4.an.z5 data.a4.z4
cp datax.aa.zq data.a5.z5
done
Hope experts will help me solving this problem.Thanks in advance.

for dir in /home/yuan/data/foldera*; do
cd "$dir"
cp data1.ax.ze data.a1.z1
cp datay.ax.z1 data.a2.z2
cp dataw.ac.zt data.a3.z3
cp data4.an.z5 data.a4.z4
cp datax.aa.zq data.a5.z5
done

Related

Unix command CP to copy a file to multiple directories

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

In bash, cp does copy dot files while using wildcard? Which option should I use

When I use
cp -r source dest,
the dot files as in .gitIgnore or any .xyz are ignored. When I use
cp -r source/.xyz dest,
then they are copied. Which option with cp can copy all the files including the dot files while using the wildcard *?
You can use the wildcard like cp -r .[^.]*
Thats because your bash is built to ignore the hidden files and cp command just don't get the hidden arguments. Thats how its supposed to work .. :)
As for me the most universal way:
cp -a /source/. /destination/
Or if you are inside of a source folder:
cp -a . /destination/
While using a wildcard *, you can try the command cp source/*.* destination/ to copy all the hidden files too.
If you want to include unhidden directories in the cp command you can try the below command
cp -r source/. destination/
Note the . at the end of source. That includes all the files and directories in the source including hidden ones.
Or
cp -r source/{.,}* destination/
x{.,}y is converted to x.y and xy. In your case it will be source/.* and source/*

Shell script to copy specific files in directory using loops

I'm trying to copy only specific files from a directory which has n number of files? Can I try that using loop?
Assuming Unix/Linux as you asked for a shell script
cd somedir
for F in *.dat
do
echo about to copy $F
cp $F /home/backup
done
would copy all files in somedir that ended in .dat to the directory /home/backup

Shell Script for comparing two folders

I want to write a shell script to compare two folders and save the difference in a different folder.
Suppose FolderA has five sub folders as A1,A2,A3,A4,A5 and FolderB has five sub folders as A1,A2,A3,A4,A5. Both folders A & B are same in structure but the files inside folders A1,A2 and so on have some differences.
I want to compare the difference of the files in FolderA-> A1 with FolderB-> A1 and save the result in a folder Difference/A1. I tried to write the script but it's not working — please guide me?
#!/bin/bash
# setup folders for our different stages
DIST=/app/webmcore1/Demo/FolderA
DIST_OLD=/app/webmcore1/Demo/FolderB
mkdir -p Difference/newAddition
mkdir -p Difference/DifferenceIs
DIST_UPGRADE=/app/webmcore1/Demo/Difference
cd $DIST
find . -type f | while read filename
do
if [ ! -f "$DIST_OLD$filename" ]; then
cp --parents "$filename" $DIST_UPGRADE/newAddition
continue
fi
diff "$filename" "$DIST_OLD$filename" > /app/webmcore1/Demo/DIST_UPGRADE/DifferenceIs
done
Try Using ls command and after that get the difference.
cd FolderA
ls -R >/tmp/list1
cd FolderB
ls -R >/tmp/list2
diff /tmp/list1 /tmp/list2 >/tmp/difference.list
This logic you can customize as per your requirement.

Preserve directory tree while copying files with cp

I have about 1000 folders that I want to extract a single file from to upload to a server but I need to preserve the directory tree.
cp */myFile.txt ../newTree
Is what I basically want to do but instead of each file being saved to ../newTree/myFile.txt I want it to be ../newTree/*/myFile.txt where the * is the wildcard from the cp command.
I couldn't find a flag in the man file for this so I'm thinking I may need another utility besides cp
With rsync:
find ./ -name myFile.txt -print0|rsync -0adv --files-from=- ./ ../newTree/
Without rsync:
You can find all files, for each file you create the directory in the newTree, and copy the file to it.
for file in */myFile.txt; do
dir=$(dirname "$file")
mkdir -p "../newTree/$dir"
cp "$file" "../newTree/$dir"
done
Store all the files in a tar archive , then extract it on the server.

Resources